summaryrefslogtreecommitdiffstats
path: root/modules/translate.py
blob: 23ce7e90be6563fae67cec64244e3773142b93a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# coding=utf-8
"""
translate.py - Phenny Translation Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.

http://inamidst.com/phenny/
"""

import re, time
import web

r_translation = re.compile(r'<div style=padding:10px;>([^<]+)</div>')

def guess_language(phrase): 
   languages = {
      'english': 'en', 
      'french': 'fr', 
      'spanish': 'es', 
      'portuguese': 'pt', 
      'german': 'de', 
      'italian': 'it', 
      'korean': 'ko', 
      'japanese': 'ja', 
      'chinese': 'zh', 
      'dutch': 'nl', 
      'greek': 'el', 
      'russian': 'ru'
   }

   uri = 'http://www.xrce.xerox.com/cgi-bin/mltt/LanguageGuesser'
   form = {'Text': phrase}
   bytes = web.post(uri, form)
   for line in bytes.splitlines(): 
      if '<listing><font size=+1>' in line: 
         i = line.find('<listing><font size=+1>')
         lang = line[i+len('<listing><font size=+1>'):].strip()
         lang = lang.lower()
         if '_' in lang: 
            j = lang.find('_')
            lang = lang[:j]
         try: return languages[lang]
         except KeyError: 
            return lang
   return 'Moon Language'

def translate(phrase, lang, target='en'): 
   babelfish = 'http://world.altavista.com/tr'
   form = {
      'doit': 'done', 
      'intl': '1', 
      'tt': 'urltext', 
      'trtext': phrase, 
      'lp': lang + '_' + target
   }

   bytes = web.post(babelfish, form)
   m = r_translation.search(bytes)
   if m: 
      translation = m.group(1)
      translation = translation.replace('\r', ' ')
      translation = translation.replace('\n', ' ')
      while '  ' in translation:
         translation = translation.replace('  ', ' ')
      return translation.lower()
   return None

def tr(phenny, input): 
   """Translates a phrase, with an optional language hint."""
   original_input = input
   input, output, phrase = original_input.groups()
   phrase = phrase.encode('utf-8')
   if (len(phrase) > 350) and (not original_input.admin): 
      return phenny.reply('Phrase must be under 350 characters.')

   input = input or guess_language(phrase)
   if not input: 
      return phenny.reply('Unable to guess the language, sorry.')
   input = input.encode('utf-8')
   output = (output or 'en').encode('utf-8')

   if not ((input == 'en') and (output == 'en')): 
      translation = translate(phrase, input, output)
      if translation is not None: 
         translation = translation.decode('utf-8').encode('utf-8')
         if output == 'en': 
            return phenny.reply('"%s" (%s)' % (translation, input))
         else: return phenny.reply('"%s" (%s -> %s)' % \
                                   (translation, input, output))

      error = "I think it's %s, which I can't translate."
      return phenny.reply(error % input.title())

   # Otherwise, it's English, so mangle it for fun
   for other in ['de', 'ja', 'de', 'ja', 'de', 'ja', 'de', 'ja', 'de', 'ja']: 
      phrase = translate(phrase, 'en', other)
      phrase = translate(phrase, other, 'en')
      time.sleep(0.1)

   if phrase is not None: 
      return phenny.reply(u'"%s" (en-unmangled)' % phrase)
   return phenny.reply("I think it's English already.")
   # @@ or 'Why but that be English, sire.'
tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
tr.priority = 'low'

# @@ mangle

if __name__ == '__main__': 
   print __doc__.strip()