Skip to content

Instantly share code, notes, and snippets.

@xhinking
Created July 2, 2011 13:26
Show Gist options
  • Save xhinking/1060199 to your computer and use it in GitHub Desktop.
Save xhinking/1060199 to your computer and use it in GitHub Desktop.
Google Language Detect Ajax Web Service
From -> http://www.cnblogs.com/zhengyun_ustc/archive/2010/10/26/1860897.html
#############
import urllib
import httplib2
try:
from base import easyjson
except:
pass
class Detect():
google_api_prefix = 'http://ajax.googleapis.com/ajax/services/language/detect'
def __init__(self, httplib2_inst=None):
"""you can use httplib,set the proxy"""
self.http = httplib2_inst or httplib2.Http()
def post_sentence(self, q):
return self._fetch(
self.google_api_prefix,
{'v':"1.0",'q':q}
)
def _fetch(self, url, params):
request = url +"?"+ urllib.urlencode(params)
resp, content = self.http.request(request, "GET")
return easyjson.parse_json_func(content)
def detectZHCN(self, text):
"""if zh-CN,True,or False"""
data = self.post_sentence(text)['responseData']
if(data):
language = data['language']
if(language=='zh-CN'):
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment