Created
January 2, 2011 22:09
-
-
Save sbp/762867 to your computer and use it in GitHub Desktop.
Python CGI to show the gender of words in various languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cgitb | |
cgitb.enable() | |
import os, re, urllib, json | |
from htmlentitydefs import name2codepoint | |
def GET(uri): | |
u = urllib.urlopen(uri) | |
bytes = u.read() | |
u.close() | |
return bytes | |
r_entity = re.compile(r'&([^;\s]+);') | |
def entity(match): | |
value = match.group(1).lower() | |
if value.startswith('#x'): | |
return unichr(int(value[2:], 16)) | |
elif value.startswith('#'): | |
return unichr(int(value[1:])) | |
elif name2codepoint.has_key(value): | |
return unichr(name2codepoint[value]) | |
return '[' + value + ']' | |
def decode(html): | |
return r_entity.sub(entity, html) | |
def translate(text, input, output): | |
if not text: return '' | |
uri = 'http://ajax.googleapis.com/ajax/services/language/translate' | |
q = urllib.quote(text) | |
pair = input + '%7C' + output | |
bytes = GET(uri + '?q=' + q + '&v=1.0&langpair=' + pair) | |
result = json.loads(bytes) | |
try: s = result['responseData']['translatedText'] | |
except Exception: return '' | |
return decode(s).encode('utf-8') | |
word = os.environ.get('PATH_INFO', '/') | |
if word.startswith('/'): | |
word = word[1:] | |
fr = translate('the ' + word, 'en', 'fr') | |
es = translate('the ' + word, 'en', 'es') | |
it = translate('the ' + word, 'en', 'it') | |
de = translate('the ' + word, 'en', 'de') | |
# cy = translate('the ' + word, 'en', 'cy') | |
if ' ' in fr: | |
fr, fr_word = fr.split(' ', 1) | |
else: fr, fr_word = '', fr | |
if ' ' in es: | |
es, es_word = es.split(' ', 1) | |
else: es, es_word = '', es | |
if ' ' in it: | |
it, it_word = it.split(' ', 1) | |
else: it, it_word = '', it | |
if ' ' in de: | |
de, de_word = de.split(' ', 1) | |
else: de, de_word = '', de | |
fr = fr.lower() | |
es = es.lower() | |
it = it.lower() | |
de = de.lower() | |
# cy = cy.split(' ')[0].lower() | |
print 'Content-Type: text/plain; charset=utf-8' | |
print 'fr(' + {'le': 'M', 'la': 'F', | |
'un': 'M', 'une': 'F', | |
'des': 'P', 'les': 'P'}.get(fr, fr + '?') + ')', | |
print 'es(' + {'el': 'M', 'la': 'F', | |
'los': 'M', 'las': 'F'}.get(es, es + '?') + ')', | |
print 'it(' + {'il': 'M', 'la': 'F'}.get(it, it + '?') + ')', | |
print 'de(' + {'der': 'M', 'die': 'F', | |
'das': 'N', 'des': 'M'}.get(de, de + '?') + ')', | |
print '-', | |
print ', '.join([fr_word, es_word, it_word, de_word]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment