python翻譯軟件實(shí)現(xiàn)代碼(使用google api完成)
# -*- coding: utf-8 -*-
import httplib
from urllib import urlencode
import re
def out(text):
p = re.compile(r'","')
m = p.split(text)
print m[0][4:].decode('UTF-8').encode('GBK')
if __name__=='__main__':
while True:
word=raw_input('Input the word you want to search:')
text=urlencode({'text':word})
h=httplib.HTTP('translate.google.cn')
h.putrequest('GET', '/translate_a/t?client=t&hl=zh-CN&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&'+text)
h.endheaders()
h.getreply()
f = h.getfile()
lines = f.readlines()
out(lines[0])
f.close()
haskell版
module Main where
import Network.HTTP
import Text.Regex.Posix
main = do
putStrLn "Input the word you want to search:"
word <- getLine
handle <- simpleHTTP (getRequest $ "http://translate.google.cn/translate_a/t?client=t&hl=zh-CN&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&" ++ (text word))
content <- getResponseBody handle
let match = (content =~ "\",\""::(String,String,String))
putStrLn $ drop 4 $ first match
main
text word = urlEncodeVars [("text",word)]
first::(String,String,String)->String
first (x,_,_) = x
作者:Hevienz
相關(guān)文章
淺析Python 簡單工廠模式和工廠方法模式的優(yōu)缺點(diǎn)
這篇文章主要介紹了Python 工廠模式的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
python django事務(wù)transaction源碼分析詳解
這篇文章主要介紹了python django事務(wù)transaction源碼分析詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
python神經(jīng)網(wǎng)絡(luò)tfrecords文件的寫入讀取及內(nèi)容解析
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)tfrecords文件的寫入讀取及內(nèi)容解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python django框架 web端視頻加密的實(shí)例詳解
這篇文章主要介紹了Python django框架 web端視頻加密,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Python Pandas學(xué)習(xí)之Pandas數(shù)據(jù)結(jié)構(gòu)詳解
Pandas中一共有三種數(shù)據(jù)結(jié)構(gòu),分別為:Series、DataFrame和MultiIndex(老版本中叫Panel )。其中Series是一維數(shù)據(jù)結(jié)構(gòu),DataFrame是二維的表格型數(shù)據(jù)結(jié)構(gòu),MultiIndex是三維的數(shù)據(jù)結(jié)構(gòu)。本文將詳細(xì)為大家講解這三個數(shù)據(jù)結(jié)構(gòu),需要的可以參考一下2022-02-02
beam search及pytorch的實(shí)現(xiàn)方式
這篇文章主要介紹了beam search及pytorch的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05

