import csv
import json
import os
import tkinter
import requests
import pandas as pd
from bs4 import BeautifulSoup
def loading():
# 加载本地词条
local_dict = {}
with open("local_dict.csv", 'r', encoding="utf-8-sig") as file:
for line in file:
line = line.rstrip().split(',')
local_dict[line[0]] = line[1]
return local_dict
def net_search(word):
# 本地词库搜索失败后的备用在线搜索:有道词典
global res
content = word
# 得到要翻译的内容
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionForm=http://www.youdao.com/"
# 传输参数
key = {
'from': 'AUTO',
'to': 'AUTO',
'i': content,
"smartresult": "dict",
"client": "fanyideskweb",
"version": "2.1",
"doctype": "json",
"keyform": "fanyi.web",
"ue": "UTF-8",
"action": "FY_BY_REALTIME",
}
# key字典发送给有道服务器
response = requests.post(url, data=key)
if response.status_code == 200:
response = response.text
# 通过json.loads加载成json格式
result = json.loads(response)
# print(result['translateResult'][0][0]['tgt'])
if str(result['translateResult'][0][0]['tgt']) == word:
res = "词典里没有该词"
else:
res = str(result['translateResult'][0][0]['tgt'])
return res
def search(word, local_dict):
count_key = 0
count_val = 0
result = '本地部分匹配结果:\n'
for key, val in local_dict.items():
if word.upper() == key.upper(): # 大小写不敏感
return '本地查询结果:\n' + key + ': ' + val + '\n'
elif word.upper() in key.upper():
result = result + key + ': ' + val + '\n'
count_key = count_key + 1
if word == val:
return '本地查询结果:\n' + val + ': ' + key + '\n'
elif word in val:
result = result + val + ': ' + key + '\n'
count_val = count_val + 1
if count_key != 0 or count_val != 0:
return net_search(word) + '\n' + '-' * 45 + '\n' + result
else:
return net_search(word) + '-' * 45 + '\n本地词库匹配无结果\n'
def search_word():
word = entry.get().strip()
if len(word) != 0:
local_dict = loading() # 加载本地词条
result = search(word, local_dict)
doc.delete(1.0, 'end')
doc.insert('end', result)
else:
doc.delete(1.0, 'end')
doc.insert('end', "请输入查询词条,按回车或点击查询...\n")
def search_word_enter(self):
search_word()
def add_word(el1, el2):
el1 = el1.get().strip()
el2 = el2.get().strip()
li = []
li.append(el1)
li.append(el2)
with open(r'./local_dict.csv', mode='a', newline='', encoding='utf-8') as cfa:
wf = csv.writer(cfa)
wf.writerow(li)
def del_word(de1, de2):
de1 = de1.get().strip()
de2 = de2.get().strip()
sum = -1
with open('local_dict.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)
if de1 in row and de2 in row:
print(sum)
df = pd.read_csv("local_dict.csv")
df.head()
df.drop(df.index[sum], inplace=True)
df.to_csv("local_dict.csv", index=False, encoding="utf-8")
break
else:
sum = sum + 1
if __name__ == '__main__':
# 主窗口
body = tkinter.Tk()
body.title("英汉小词典")
body.resizable(0, 0)
body.geometry("400x500")
# 输入块
frame_in = tkinter.Frame(body, width=300, height=30)
frame_in.place(x=50, y=10)
entry = tkinter.Entry(frame_in, width=30)
entry.pack(side="left")
# button
btn = tkinter.Button(frame_in, text="Search", width=10, command=search_word)
btn.pack(side="right", padx=10)
entry.bind("<Return>", search_word_enter)
# 文本块
frame_doc = tkinter.Frame(body, width=350, height=200)
frame_doc.place(x=20, y=40)
bar = tkinter.Scrollbar(frame_doc)
bar.pack(side="right", fill=tkinter.Y)
doc = tkinter.Text(frame_doc, width=50, height=9.2)
doc.pack(side="bottom", pady=15)
doc.config(yscrollcommand=bar.set)
bar.config(command=doc.yview)
doc.insert('end', "请输入查询词条,按回车或点击查询...\n")
# 显示
tkinter.Label(body, text='要增加的英文:').place(x=30, y=200)
tkinter.Label(body, text='要增加的汉文:').place(x=30, y=240)
# 设置焦点
el1 = tkinter.Entry(body)
el1.focus()
el1.place(x=130, y=200, width=100, height=30)
# 设置焦点
el2 = tkinter.Entry(body)
el2.focus()
el2.place(x=130, y=240, width=100, height=30)
tkinter.Button(body, text='添加', width=10, command=lambda: add_word(el1, el2)).place(x=280, y=220)
tkinter.Label(body, text='要删除的英文:').place(x=30, y=300)
tkinter.Label(body, text='要删除的汉文:').place(x=30, y=340)
# 设置焦点
de1 = tkinter.Entry(body)
de1.focus()
de1.place(x=130, y=300, width=100, height=30)
# 设置焦点
de2 = tkinter.Entry(body)
de2.focus()
de2.place(x=130, y=340, width=100, height=30)
tkinter.Button(body, text='删除', width=10, command=lambda: del_word(de1, de2)).place(x=280, y=320)
tkinter.Button(body, text='退出', width=20, command=body.quit).place(x=120, y=420)
body.mainloop()
- 1
- 2
- 3
- 4
- 5
- 6
前往页