python實現簡易的翻譯機器

# -*-coding: UTF-8 -*-
# @Time:2019/9/818:54
# @author superxjz
# @func 簡單的機器翻譯實戰
"""
實現一個簡單的翻譯功能
"""

import requests
from tkinter import Tk,Button,Entry,Label,Text,END

#self其實相當於this關鍵字

#將代碼進行封裝
class YouDao(object):
    def __init__(self):
        pass
    def crawl(self,word):
        #url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
        url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
        #表單數據如下
        data={
            "i":word,
            "from":"AUTO",
            "to":"AUTO",
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTIME',
            'typoResult': 'false'

        }

        #將需要post的數據椅子墊的形式存儲在data中
        r = requests.post(url,data=data)
        #post需要提交兩個參數,一個是url,一個是data返回一個response對象
        answer = r.json()
        result = answer["translateResult"][0][0]["tgt"]
       # print(" "+word+"的翻譯結果爲:"+result+"\n")
        return result

class APP(object):
    def __init__(self):
        self.window=Tk()
        self.fanyi=YouDao()

        #窗體的名稱
        self.window.title(u"在線翻譯")
        #調整窗口的大小
        self.window.geometry('300x400+500+300')
        self.window.minsize(300,400)
        self.window.maxsize(300,400)

        #設置文本輸入框
        self.entry=Entry(self.window)
        self.entry.place(x=10,y=10,width=200,height=25)
        self.entry.bind("<Key-Return>",self.submit1)

        #設置輸入顯示文本框
        self.result_text1=Text(self.window,background="azure")
        self.result_text1.place(x=10,y=10,width=285,height=155)
        self.result_text1.bind("<Key-Return>",self.submit1)

        #設置提交按鈕
        self.submit_btn = Button(self.window, text=u'翻譯', command=self.submit)
        self.submit_btn.place(x=205, y=165, width=35, height=25)
        self.submit_btn2 = Button(self.window, text=u'清空', command=self.clean)
        self.submit_btn2.place(x=250, y=165, width=35, height=25)

        #設置結果之前的標題
        self.title_label = Label(self.window, text=u'翻譯結果:')
        self.title_label.place(x=10, y=165)

        self.result_text = Text(self.window, background='light cyan')
        self.result_text.place(x=10, y=190, width=285, height=165)

    #這個提交的是輸入文本的內容
    def submit1(self, event):
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
        result = self.fanyi.crawl(content)
        self.result_text.delete(0.0, END)
        # self.result_text.insert(END, result)
        self.result_text.insert(END, result)

    def submit(self):
        #點擊按鈕得到內容
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
        #爬蟲得到內容
        result = self.fanyi.crawl(content)
        self.result_text.delete(0.0, END)
        self.result_text.insert(END, result)
        print(content)

    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)

    def run(self):
        self.window.mainloop()


if __name__=="__main__":
    app = APP()
    app.run()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章