Created
September 30, 2010 21:17
-
-
Save vim13/605340 to your computer and use it in GitHub Desktop.
Google CGI API for Japanese Input
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/lib/python | |
#vim:fileencoding=utf-8 | |
''' | |
http://www.google.com/intl/ja/ime/cgiapi.html | |
>>>import google | |
>>>api = google.Api() | |
>>>result = api.ja('こんにちは') | |
>>>print result[0][1][1] | |
今日は | |
''' | |
import urllib | |
import urllib2 | |
import re | |
import json | |
class Api: | |
def __init__(self): | |
self.lang = 'ja-Hira|ja' | |
self.uri = 'http://www.google.com/transliterate?' | |
def ja(self, text): | |
params = urllib.urlencode({ | |
'langpair' : self.lang, | |
'text' : text | |
}) | |
result = urllib2.urlopen(self.uri, params).read() | |
temp = re.sub('\n', '', result) | |
line = re.sub(',\]', ']', temp) | |
lis = json.loads(line) | |
return lis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment