Skip to content

Instantly share code, notes, and snippets.

@nus
Created September 28, 2010 15:44
Show Gist options
  • Save nus/601225 to your computer and use it in GitHub Desktop.
Save nus/601225 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License
#
# Copyright (c) 2010 Yota Ichino
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import urllib
class TranslateCandicates(object):
'''container class for text and translated
Example Use:
>>> word = TranslateCandicates("にほん", ("日本","二本","ニホン","弐奔","にほん"))
>>> print word.text
にほん
>>> for element in word.candicates:
... print element,
日本 二本 ニホン 弐奔 にほん
'''
# Translated cardicates
candicates = ()
# Translate target text
text = ''
# for iterator temporary value
value = 0
def __init__(self, text, candicates):
self.text = text
self.candicates = tuple(candicates)
def __iter__(self):
return self
def next(self):
''' yield self.candicates
'''
if self.value == (len(self.candicates) - 1):
raise StopIteration
self.value += 1
return self.candicates[self.value]
class GoogleJapanseIME(object):
'''This module is Google CGI API fo Japanse Input in Python
Example Use:
>>> ime = GoogleJapanseIME()
>>> answer = ime.trans("にほん")
>>> for tmp in answer[0]:
... print tmp,
日本 二本 ニホン 弐奔 にほん
About Google CGI API for Japanse Input:
See http://www.google.co.jp/intl/ja/ime/cgiapi.html
'''
_url = 'http://www.google.com/transliterate?langpair=ja-Hira|ja&text='
def _request(self, hiragana):
# query = self._url + urllib.quote_plus(hiragana)
query = self._url + urllib.quote(hiragana)
response = urllib.urlopen(query).read()
return response.decode('raw_unicode_escape')
def _json_parser(self, string):
return tuple(eval(string))
def _arrange(self, parsed_string):
ret = []
for item in parsed_string:
w = TranslateCandicates(item[0], item[1])
ret.append(w)
return tuple(ret)
def trans(self, hiragana):
response = self._request(hiragana)
parsed = self._json_parser(response)
ret = self._arrange(parsed)
return ret
def main():
henkan = 'へんかん,ぐーぐる'
ime = GoogleJapanseIME()
answers = ime.trans(henkan)
for ans in answers:
print 'target text:', ans.text
print 'candicates :',
for item in ans:
print item,
print ''
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment