Очередной коммит. Стараюсь пофиксить баги.
[nia.git] / test.py
blobaaa3cc474c5020b62075f78c7e628db29844a5ba
1 # -*- coding: utf-8 -*-
2 import urllib,simplejson
4 def translate(word, from_l, to_l):
5 word = urllib.quote(word)
6 url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s'%(word, from_l, to_l)
7 src = urllib.urlopen(url).read()
8 convert = simplejson.loads(src)
9 results = convert['responseData']['translatedText']
10 return results
11 def detect_lang(word):
12 word = urllib.quote(word)
13 url = 'http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=%s'%word
14 # http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=Ciao%20mondo
15 src = urllib.urlopen(url).read()
16 convert = simplejson.loads(src)
17 results = convert['responseData']['language']
18 return results
20 word = 'Как жизнь, унылые люди?'
21 from_l = 'en'
22 to_l = 'ja'
24 print translate(word,from_l,to_l)
25 print translate(word, detect_lang(word), 'ja')
26 #http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_fonje_detect