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

yipeiwu_com6年前Python实例


# -*- 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

相关文章

浅谈tensorflow中Dataset图片的批量读取及维度的操作详解

三维的读取图片(w, h, c):import tensorflow as tf   import glob import ...

tensorflow模型保存、加载之变量重命名实例

话不多说,干就完了。变量重命名的用处?简单定义:简单来说就是将模型A中的参数parameter_A赋给模型B中的parameter_B使用场景:当需要使用已经训练好的模型参数,尤其是使用别...

tensorflow模型继续训练 fineturn实例

tensorflow模型继续训练 fineturn实例

解决tensoflow如何在已训练模型上继续训练fineturn的问题。训练代码任务描述: x = 3.0, y = 100.0, 运算公式 x×W+b = y,求 W和b的最优解。# -*...

Python实现FLV视频拼接功能

文章摘要本文简单说明了FLV文件的格式,以此为出发点,使用 Python 实现FLV视频的拼接。一.FLV文件格式关于FLV文件格式的解析网上有诸多文章,在这里就简单介绍一下需要了解的部分...

python wav模块获取采样率 采样点声道量化位数(实例代码)

安装:pip install wave在wav 模块中 ,主要介绍一种方法:getparams(),该方法返回的结果如下:_wave_params(nchannels=1, sa...