Skip to content

Instantly share code, notes, and snippets.

@vim13
Created September 30, 2010 21:17
Show Gist options
  • Save vim13/605340 to your computer and use it in GitHub Desktop.
Save vim13/605340 to your computer and use it in GitHub Desktop.
Google CGI API for Japanese Input
#!/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