Stylistic updates.

This commit is contained in:
Jeremy Fincher 2003-08-21 12:25:35 +00:00
parent 97c8d4d252
commit 088047aea8
2 changed files with 63 additions and 60 deletions

View File

@ -124,8 +124,10 @@ def translate(phrase, from_lang, to_lang):
html = response.read() html = response.read()
for regex in __where: for regex in __where:
match = regex.search(html) match = regex.search(html)
if match: break if match:
if not match: raise BabelfishChangedError("Can't recognize translated string.") break
if not match:
raise BabelfishChangedError("Can't recognize translated string.")
return clean(match.group(1)) return clean(match.group(1))
def babelize(phrase, from_language, through_language, limit = 12, callback = None): def babelize(phrase, from_language, through_language, limit = 12, callback = None):
@ -139,7 +141,8 @@ def babelize(phrase, from_language, through_language, limit = 12, callback = Non
next = from_language next = from_language
for i in range(limit): for i in range(limit):
phrase = translate(phrase, next, flip[next]) phrase = translate(phrase, next, flip[next])
if seen.has_key(phrase): break if seen.has_key(phrase):
break
seen[phrase] = 1 seen[phrase] = 1
if callback: if callback:
callback(phrase) callback(phrase)
@ -149,7 +152,8 @@ def babelize(phrase, from_language, through_language, limit = 12, callback = Non
if next != from_language: if next != from_language:
phrase = translate(phrase, next, flip[next]) phrase = translate(phrase, next, flip[next])
results.append(phrase) results.append(phrase)
if not callback: return results if not callback:
return results
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys

View File

@ -42,10 +42,11 @@ import random
import urllib import urllib
import urllib2 import urllib2
import babelfish
import utils import utils
import debug import debug
import privmsgs import privmsgs
import babelfish
import callbacks import callbacks
import structures import structures
@ -194,24 +195,6 @@ class Http(callbacks.Privmsg):
search = urllib.unquote(search) search = urllib.unquote(search)
s = 'There appears to be no definition for %s.' % search s = 'There appears to be no definition for %s.' % search
irc.reply(msg, s) irc.reply(msg, s)
'''
_zipcode = re.compile(r'Local Forecast for (.*), (.*?) ')
def zipcode(self, irc, msg, args):
"""<US zip code>
Returns the city and state of a given US Zip code.
"""
zip = privmsgs.getArgs(args)
url = "http://www.weather.com/weather/local/%s?lswe=%s" % (zip, zip)
try:
html = urllib2.urlopen(url).read()
(city, state) = self._zipcode.search(html).groups()
irc.reply(msg, '%s, %s' % (city, state))
except AttributeError:
irc.error(msg, 'the format of the page was odd.')
except urllib2.URLError:
irc.error(msg, 'Couldn\'t open search page.')
'''
_tempregex = re.compile('CLASS=obsTempTextA>(\d+)&deg;F</b></td>',\ _tempregex = re.compile('CLASS=obsTempTextA>(\d+)&deg;F</b></td>',\
@ -269,7 +252,7 @@ class Http(callbacks.Privmsg):
def acronym(self, irc, msg, args): def acronym(self, irc, msg, args):
"""<acronym> """<acronym>
Displays the first acronym matches from acronymfinder.com Displays acronym matches from acronymfinder.com
""" """
acronym = privmsgs.getArgs(args) acronym = privmsgs.getArgs(args)
try: try:
@ -365,55 +348,71 @@ class Http(callbacks.Privmsg):
irc.reply(msg, s) irc.reply(msg, s)
_abbrevs = utils.abbrev(map(str.lower, babelfish.available_languages)) _abbrevs = utils.abbrev(map(str.lower, babelfish.available_languages))
_abbrevs['de'] = 'german'
_abbrevs['fr'] = 'french'
_abbrevs['jp'] = 'japanese'
_abbrevs['ko'] = 'korean'
_abbrevs['es'] = 'spanish'
_abbrevs['pt'] = 'portuguese'
_abbrevs['it'] = 'italian'
_abbrevs['zh'] = 'chinese'
for language in babelfish.available_languages:
_abbrevs[language] = language
def translate(self, irc, msg, args): def translate(self, irc, msg, args):
"""<from-language> <to-language> <phrase> """<from-language> <to-language> <text>
Returns the phrase translated to the new language. One of the Returns the text translated to the new language.
languages must be English.
""" """
(flang, tlang, phrase) = privmsgs.getArgs(args, 3) (fromLang, toLang, text) = privmsgs.getArgs(args, needed=3)
flang = str.lower(flang)
tlang = str.lower(tlang)
if self._abbrevs.has_key(flang):
flang = self._abbrevs[flang]
if self._abbrevs.has_key(tlang):
tlang = self._abbrevs[tlang]
try: try:
trans = babelfish.translate(phrase, flang, tlang) fromLang = self._abbrevs[fromLang.lower()]
irc.reply(msg, trans) toLang = self._abbrevs[toLang.lower()]
except babelfish.LanguageNotAvailableError, e: translation = babelfish.translate(text, fromLang, toLang)
irc.reply(msg, 'Valid languages: %s.' %\ irc.reply(msg, translation)
', '.join(babelfish.available_languages)) except (KeyError, babelfish.LanguageNotAvailableError), e:
irc.error(msg, '%s is not a valid language. Valid languages ' \
'include %s' % \
(e, utils.commaAndify(babelfish.available_languages)))
except babelfish.BabelizerIOError, e: except babelfish.BabelizerIOError, e:
irc.reply(msg, e.args[0]) irc.error(msg, e)
except babelfish.BabelfishChangedError, e: except babelfish.BabelfishChangedError, e:
irc.reply(msg, 'Babelfish has foiled our plans by changing their\ irc.error(msg, 'Babelfish has foiled our plans by changing its ' \
format') 'webpage format')
def babelize(self, irc, msg, args): def babelize(self, irc, msg, args):
"""<from-language> <to-language> <phrase> """<from-language> <to-language> <text>
Returns the phrase translated to the new language. This is done 12 Translates <text> repeatedly between <from-language> and <to-language>
times, or until the output doesn't change anymore. until it doesn't change anymore or 12 times, whichever is fewer. One
of the languages must be English.
""" """
(flang, tlang, phrase) = privmsgs.getArgs(args, 3) (fromLang, toLang, text) = privmsgs.getArgs(args, needed=3)
flang = str.lower(flang)
tlang = str.lower(tlang)
if self._abbrevs.has_key(flang):
flang = self._abbrevs[flang]
if self._abbrevs.has_key(tlang):
tlang = self._abbrevs[tlang]
try: try:
trans = babelfish.babelize(phrase, flang, tlang) fromLang = self._abbrevs[fromLang.lower()]
irc.reply(msg, trans[-1]) toLang = self._abbrevs[toLang.lower()]
except babelfish.LanguageNotAvailableError, e: translations = babelfish.babelize(text, fromLang, toLang)
irc.reply(msg, 'Valid languages: %s.' %\ irc.reply(msg, translations[-1])
', '.join(babelfish.available_languages)) except (KeyError, babelfish.LanguageNotAvailableError), e:
irc.reply(msg, '%s is not a valid language. Valid languages ' \
'include %s' % \
(e, utils.commaAndify(babelfish.available_languages)))
except babelfish.BabelizerIOError, e: except babelfish.BabelizerIOError, e:
irc.reply(msg, e.args[0]) irc.reply(msg, e)
except babelfish.BabelfishChangedError, e: except babelfish.BabelfishChangedError, e:
irc.reply(msg, 'Babelfish has foiled our plans by changing their\ irc.reply(msg, 'Babelfish has foiled our plans by changing its ' \
format') 'webpage format')
def randomlanguage(self, irc, msg, args):
"""[<allow-english>]
Returns a random language supported by babelfish. If <allow-english>
is provided, will include English in the list of possible languages.
"""
allowEnglish = privmsgs.getArgs(args, needed=0, optional=1)
language = random.sample(babelfish.available_languages, 1)[0]
while not allowEnglish and language == 'English':
language = random.sample(babelfish.available_languages, 1)[0]
irc.reply(msg, language)
Class = Http Class = Http