存档

文章标签 ‘sogou’

Python调用搜狗云输入法的小例子

2010年1月22日 dipplum 没有评论

写了个Python的小例子,调用搜狗云输入法获取汉字。感谢xzap在搜狗论坛发布的bash脚本,但觉得bash还是略微晦涩了点,用python说明搜狗云输入法的API更加清晰一点。

#!/usr/bin/python                                                                                                                                           
from urllib import urlopen
from cgi import parse_qs
 
url = 'http://web.pinyin.sogou.com/web_ime/get_ajax/%s.key'
 
py = raw_input('input pinyin: ')
res = urlopen(url % py).read()
dic = parse_qs(res)
lst = eval(dic['ime_query_res'][0]).split('\t ')
words = map(lambda x: x.split('\xef\xbc\x9a')[0], lst)
counts = map(lambda x: x.split('\xef\xbc\x9a')[1], lst)
 
for i in range(len(words)):
    print words[i], counts[i]
 
exit

运行该脚本的输出结果如下,每行前面是候选词/句,后面是该候选词/句消耗的英文单词个数:

$ ./sogou.py
input pinyin: nihaosgysrf
你好搜狗云输入法 11
你好是 6
倪豪士 6
你好 5
拟好 5
倪浩 5
泥蒿 5
倪昊 5
倪皓 5
匿号 52222222222
分类: 中文化 标签: , , ,
This work by dipplum is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 China Mainland.