Created
November 29, 2010 15:14
-
-
Save hktechn0/720059 to your computer and use it in GitHub Desktop.
Google CGI API for Japanese Input sample script
This file contains hidden or 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 | |
#-*- coding: utf-8 -*- | |
import urllib, urllib2 | |
import json | |
class GoogleIMECGI(object): | |
request_url = 'http://www.google.com/transliterate' | |
langpair = ("ja-Hira", "ja") | |
def __init__(self, lang_from = None, lang_to = None): | |
if lang_from or lang_to: | |
self.langpair = (lang_from, lang_to) | |
def transliterate(self, text = None, text_tuple = None): | |
if text_tuple != None: | |
text = ",".join(text_tuple) | |
if not text: return | |
params = (("langpair", "|".join(self.langpair)), | |
("text", text)) | |
url = "%s?%s" % (self.request_url, urllib.urlencode(params)) | |
r = urllib2.urlopen(url) | |
response = r.read().replace("\n", "").replace(",]", "]") | |
return json.loads(response) | |
if __name__ == "__main__": | |
text = raw_input("Hiragana: ") | |
for word in GoogleIMECGI().transliterate(text): | |
print "%s:" % word[0] | |
for w in word[1]: | |
print "\t%s" % w | |
# $ python googleime-cgi.py | |
# Hiragana: ここではきものをぬぐ | |
# ここでは: | |
# ここでは | |
# ココでは | |
# 個々では | |
# 此処では | |
# ここでは | |
# きものを: | |
# 着物を | |
# きものを | |
# キモノを | |
# KIMONOを | |
# kimonoを | |
# ぬぐ: | |
# 脱ぐ | |
# ぬぐ | |
# ぬぐ | |
# ヌグ | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment