Python 翻譯詞典小程序功能說明
一、概述
本工具是基于Python開發(fā)的智能翻譯系統(tǒng),采用有道詞典進(jìn)行翻譯,并具有本地詞典緩存以及單詞本功能。 版本號(hào):v1.0 (2025-05-15)
二、核心功能說明
1. 基礎(chǔ)翻譯功能
- 即時(shí)翻譯:輸入英文單詞自動(dòng)獲取中文釋義
- 詞性識(shí)別:自動(dòng)標(biāo)注單詞詞性(名詞/動(dòng)詞等)
- 網(wǎng)絡(luò)查詢:實(shí)時(shí)獲取最新詞典數(shù)據(jù)
- 離線查詢: 對(duì)以查過的單詞,首先在本地SQLITE數(shù)據(jù)庫(kù)查找
2. 數(shù)據(jù)存儲(chǔ)系統(tǒng)
- 翻譯歷史:
- 自動(dòng)存儲(chǔ)所有查詢記錄
- 字段包含:英文單詞、中文釋義、詞性、查詢時(shí)間
- 生詞本管理:
- 支持手動(dòng)添加/移除生詞
- 按添加時(shí)間倒序排列
- 獨(dú)立數(shù)據(jù)庫(kù)表存儲(chǔ)收藏關(guān)系
"""
小小詞典 V1.0
Copyright (C) 2025 Yang xiaofan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import sqlite3
import requests
from bs4 import BeautifulSoup
def init_db():
conn = sqlite3.connect('translations.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS translations
(id INTEGER PRIMARY KEY AUTOINCREMENT,
english TEXT UNIQUE NOT NULL,
chinese TEXT NOT NULL,
pos TEXT,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
# 新增生詞本表
c.execute('''CREATE TABLE IF NOT EXISTS vocabulary_book
(id INTEGER PRIMARY KEY AUTOINCREMENT,
word_id INTEGER UNIQUE,
add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(word_id) REFERENCES translations(id))''')
conn.commit()
conn.close()
def add_to_vocabulary(word):
conn = sqlite3.connect('translations.db')
c = conn.cursor()
# 獲取單詞ID
c.execute("SELECT id FROM translations WHERE english=?", (word,))
word_id = c.fetchone()
if word_id:
try:
c.execute("INSERT OR IGNORE INTO vocabulary_book (word_id) VALUES (?)",
(word_id[0],))
conn.commit()
print(f"【{word}】已成功加入生詞本")
except sqlite3.IntegrityError:
print(f"【{word}】已在生詞本中")
else:
print("請(qǐng)先查詢?cè)搯卧~確保其存在于數(shù)據(jù)庫(kù)")
conn.close()
def show_vocabulary():
conn = sqlite3.connect('translations.db')
c = conn.cursor()
c.execute('''SELECT t.english, t.chinese, t.pos
FROM translations t JOIN vocabulary_book v ON t.id = v.word_id
ORDER BY v.add_time DESC''')
print("\n=== 我的生詞本 ===")
for idx, (en, cn, pos) in enumerate(c.fetchall(), 1):
print(f"{idx}. {en} ({pos}): {cn}")
conn.close()
def save_to_db(english, chinese, pos):
conn = sqlite3.connect('translations.db')
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO translations (english, chinese, pos) VALUES (?, ?, ?)",
(english, chinese, pos))
conn.commit()
conn.close()
def check_in_db(word):
conn = sqlite3.connect('translations.db')
c = conn.cursor()
c.execute("SELECT english, chinese, pos FROM translations WHERE english=?", (word,))
result = c.fetchone()
conn.close()
return result if result else None
def translate_with_pos(word):
# 先查本地?cái)?shù)據(jù)庫(kù)
db_result = check_in_db(word)
if db_result:
print(f"該單詞已在本地?cái)?shù)據(jù)庫(kù)查找到,翻譯解釋如下:")
print(f"{db_result[0]} ({db_result[2]}): {db_result[1]}")
choice = input("繼續(xù)網(wǎng)絡(luò)查詢請(qǐng)輸入w,直接退出請(qǐng)按回車:").strip().lower()
if choice != 'w':
return None
url = f"https://dict.youdao.com/w/eng/{word}/"
headers = {'User-Agent': 'Mozilla/5.0'}
try:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 獲取中文釋義
trans = soup.find('div', class_='trans-container').get_text(strip=True)
# 獲取詞性標(biāo)注
pos_tag = soup.find('span', class_='pos')
pos = pos_tag.get_text() if pos_tag else "無詞性標(biāo)注"
save_to_db(word, trans, pos)
return f"{word} ({pos}): {trans}"
except Exception as e:
return f"翻譯失敗: {str(e)}"
if __name__ == "__main__":
init_db()
print("命令: \q 退出;\w 加入生詞本 \s 查看生詞本 \h 查看幫助")
while True:
query = input("請(qǐng)輸入英文單詞或命令(輸入\q退出): ").strip()
if query.lower() == '\q':
break
if query.lower() == '\w':
word = input("輸入要收藏的單詞: ").strip()
add_to_vocabulary(word)
continue
if query.lower() == '\s':
show_vocabulary()
continue
if query.lower() == '\h':
print("命令: \q 退出;\w 加入生詞本 \s 查看生詞本 \h 查看幫助")
continue
trans = translate_with_pos(query)
if trans:
print(f"- {trans}")運(yùn)行實(shí)例:
(.venv) D:\sanxia-src>translate.py
命令: \q 退出;\w 加入生詞本 \s 查看生詞本 \h 查看幫助
請(qǐng)輸入英文單詞或命令(輸入\q退出): \s
=== 我的生詞本 ===
1. water (n.): n. 水,雨水;水域,(江、河、湖、海等)大片的水;(某個(gè)國(guó)家的)領(lǐng)海,海域(waters);不明朗(或未知的、困難、危險(xiǎn)等)局面(waters);羊水(waters);(湖、海的)水面;水位;乘船,走水路v. 給……澆水,灌溉;給…...水喝,飲(動(dòng)物);(風(fēng)等使眼睛)流淚;流口水;(江河)流經(jīng)并給(某地區(qū))供水;加水沖淡,稀釋【名】 (Water)(英)沃特(人名)[
復(fù)數(shù)
waters
第三人稱單數(shù)
waters
現(xiàn)在分詞
watering
過去式
watered
過去分詞
watered
]
請(qǐng)輸入英文單詞或命令(輸入\q退出): yes
- yes (n.): adv. 是,是的n. 是(表示肯定)[
復(fù)數(shù)
yesses或yeses
第三人稱單數(shù)
yesses或yeses
現(xiàn)在分詞
yessing
過去式
yessed
過去分詞
yessed
]
請(qǐng)輸入英文單詞或命令(輸入\q退出): level
- level (n.): n. 數(shù)量,程度;標(biāo)準(zhǔn),水平;層次,級(jí)別;看待(或應(yīng)對(duì)、理解)事物的方式;水平高度,相對(duì)高度;樓層;平地;水平儀adj. 平坦的,水平的;相同價(jià)值的,相同地位的;比分相同的;平靜的,冷靜的v. 使平整;推倒,夷平;(使)比分相同;(尤指用槍)瞄準(zhǔn);針對(duì)……(進(jìn)行批評(píng)等);穩(wěn)定下來,達(dá)到平衡(level off);坦誠(chéng)相見;作水準(zhǔn)測(cè)量【名】 (Level)(法)勒韋爾(人名)[
復(fù)數(shù)
levels
第三人稱單數(shù)
levels
現(xiàn)在分詞
levelling或leveling
過去式
levelled或leveled
過去分詞
levelled或leveled
]
請(qǐng)輸入英文單詞或命令(輸入\q退出): jackfruit
- jackfruit (n.): n. 木菠蘿;菠蘿蜜到此這篇關(guān)于Python 翻譯詞典小程序功能說明的文章就介紹到這了,更多相關(guān)Python 翻譯詞典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python2.x版本中maketrans()方法的使用介紹
這篇文章主要介紹了Python2.x版本中maketrans()方法的使用介紹,是Python學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
python 利用PyAutoGUI快速構(gòu)建自動(dòng)化操作腳本
我們經(jīng)常遇到需要進(jìn)行大量重復(fù)操作的時(shí)候,比如:網(wǎng)頁上填表,對(duì) web 版本 OA 進(jìn)行操作,自動(dòng)化測(cè)試或者給新系統(tǒng)首次添加數(shù)據(jù)等,今天就利用PyAutoGUI構(gòu)建自動(dòng)化操作腳本完成這些重復(fù)的需求2021-05-05
python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
python數(shù)據(jù)庫(kù)操作指南之PyMysql使用詳解
PyMySQL是在Python3.x版本中用于連接MySQL服務(wù)器的一個(gè)庫(kù),Python2 中則使用mysqldb,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)庫(kù)操作指南之PyMysql使用的相關(guān)資料,需要的朋友可以參考下2023-03-03
詳解python實(shí)現(xiàn)線程安全的單例模式
這篇文章主要介紹了python實(shí)現(xiàn)線程安全的單例模式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Python隨機(jī)生成數(shù)模塊random使用實(shí)例
這篇文章主要介紹了Python隨機(jī)生成數(shù)模塊random使用實(shí)例,本文直接給出示例代碼,需要的朋友可以參考下2015-04-04
Python獲取多進(jìn)程執(zhí)行的返回值實(shí)現(xiàn)
本文主要介紹了Python獲取多進(jìn)程執(zhí)行的返回值實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

