分享

python翻译软件实现代码(使用google api完成)

 天才白痴书馆 2015-04-14

python翻译软件实现代码(使用google api完成)

作者: 字体:[增加 减小] 类型:转载
这篇文章主要介绍了python结合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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多