星期四, 11月 12, 2009

Bing translate in Python

根據 Microsoft Translator HTTP 介面,然後用Python寫的。程式不長,其實跟 google translate 的方法很相似,最下面有使用範例。

附帶一提的,Bing翻譯網站上完全沒提到要怎麼申請 appid,申請 appid 要去 Bing Developer Center 申請,所以你要自己去申請,取得 appid 之後,把程式裡的 your_app_id 換成你申請的 appid。

#!/usr/bin/env python
# -*= coding: utf-8 -*-
import sys
import os
from urllib import urlencode
import urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
import json
from translate import *

class BingTranslate:
appid = 'your_app_id'
base_uri = "http://api.microsofttranslator.com/V1/Http.svc"

def __read_from_req( self, req ):
try:
response = urllib2.urlopen( req )
result = response.read()
except HTTPError, e:
print e.code
print e.read()
result=""
return result

def detect( self, text ):
uri="%s/Detect?appId=%s" % ( self.base_uri, self.appid )
req = urllib2.Request( uri, text, { 'Content-Type':'text/plain'} )
return self.__read_from_req( req )

def getLanguageNames( self, locale=None ):
"""
Thanks MSDN, I still don't know the value of parameter 'locale'
Don't pass any parameter to getLanguageNames, or you will get error.
"""
uri="%s/GetLanguageNames?appId=%s" % ( self.base_uri, self.appid )
req = urllib2.Request( uri, locale, { 'Content-Type':'text/plain'} )
return self.__read_from_req( req ).split( '\n' )

def getLanguages( self ):
uri="%s/GetLanguages?appId=%s" % ( self.base_uri, self.appid )
req = urllib2.Request( uri, None, { 'Content-Type':'text/plain'} )
return self.__read_from_req( req ).split("\r\n")

def translate( self, text, fr="en", to="zh-CHT" ):
uri="%s/Translate?appId=%s&from=%s&to=%s" % ( self.base_uri,
self.appid, fr, to )
req = urllib2.Request( uri, text, { 'Content-Type':'text/plain'} )
return self.__read_from_req( req )

if __name__ == "__main__":
t = BingTranslate()
print t.translate( "test" )
print t.translate( "鬼", "zh-CHT", "en" )
print t.detect( "中文測試" )
print t.detect( "中文测试" )
names = t.getLanguageNames()
langs = t.getLanguages()
print "Language Names (total %d):" % len(names)
for l in names:
print l
print "Languages (total %d):" % len(langs)
for l in langs:
print l

沒有留言:

您或許對這些文章有興趣

Related Posts Plugin for WordPress, Blogger...