Python 翻譯詞典小程序功能說明
一、概述
本工具是基于Python開發(fā)的智能翻譯系統(tǒng),采用有道詞典進行翻譯,并具有本地詞典緩存以及單詞本功能。 版本號:v1.0 (2025-05-15)
二、核心功能說明
1. 基礎(chǔ)翻譯功能
- 即時翻譯:輸入英文單詞自動獲取中文釋義
- 詞性識別:自動標(biāo)注單詞詞性(名詞/動詞等)
- 網(wǎng)絡(luò)查詢:實時獲取最新詞典數(shù)據(jù)
- 離線查詢: 對以查過的單詞,首先在本地SQLITE數(shù)據(jù)庫查找
2. 數(shù)據(jù)存儲系統(tǒng)
- 翻譯歷史:
- 自動存儲所有查詢記錄
- 字段包含:英文單詞、中文釋義、詞性、查詢時間
- 生詞本管理:
- 支持手動添加/移除生詞
- 按添加時間倒序排列
- 獨立數(shù)據(jù)庫表存儲收藏關(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("請先查詢該單詞確保其存在于數(shù)據(jù)庫") 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): # 先查本地數(shù)據(jù)庫 db_result = check_in_db(word) if db_result: print(f"該單詞已在本地數(shù)據(jù)庫查找到,翻譯解釋如下:") print(f"{db_result[0]} ({db_result[2]}): {db_result[1]}") choice = input("繼續(xù)網(wǎng)絡(luò)查詢請輸入w,直接退出請按回車:").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退出): ").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}")
運行實例:
(.venv) D:\sanxia-src>translate.py 命令: \q 退出;\w 加入生詞本 \s 查看生詞本 \h 查看幫助 請輸入英文單詞或命令(輸入\q退出): \s === 我的生詞本 === 1. water (n.): n. 水,雨水;水域,(江、河、湖、海等)大片的水;(某個國家的)領(lǐng)海,海域(waters);不明朗(或未知的、困難、危險等)局面(waters);羊水(waters);(湖、海的)水面;水位;乘船,走水路v. 給……澆水,灌溉;給…...水喝,飲(動物);(風(fēng)等使眼睛)流淚;流口水;(江河)流經(jīng)并給(某地區(qū))供水;加水沖淡,稀釋【名】 (Water)(英)沃特(人名)[ 復(fù)數(shù) waters 第三人稱單數(shù) waters 現(xiàn)在分詞 watering 過去式 watered 過去分詞 watered ] 請輸入英文單詞或命令(輸入\q退出): yes - yes (n.): adv. 是,是的n. 是(表示肯定)[ 復(fù)數(shù) yesses或yeses 第三人稱單數(shù) yesses或yeses 現(xiàn)在分詞 yessing 過去式 yessed 過去分詞 yessed ] 請輸入英文單詞或命令(輸入\q退出): level - level (n.): n. 數(shù)量,程度;標(biāo)準(zhǔn),水平;層次,級別;看待(或應(yīng)對、理解)事物的方式;水平高度,相對高度;樓層;平地;水平儀adj. 平坦的,水平的;相同價值的,相同地位的;比分相同的;平靜的,冷靜的v. 使平整;推倒,夷平;(使)比分相同;(尤指用槍)瞄準(zhǔn);針對……(進行批評等);穩(wěn)定下來,達到平衡(level off);坦誠相見;作水準(zhǔn)測量【名】 (Level)(法)勒韋爾(人名)[ 復(fù)數(shù) levels 第三人稱單數(shù) levels 現(xiàn)在分詞 levelling或leveling 過去式 levelled或leveled 過去分詞 levelled或leveled ] 請輸入英文單詞或命令(輸入\q退出): jackfruit - jackfruit (n.): n. 木菠蘿;菠蘿蜜
到此這篇關(guān)于Python 翻譯詞典小程序功能說明的文章就介紹到這了,更多相關(guān)Python 翻譯詞典內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python2.x版本中maketrans()方法的使用介紹
這篇文章主要介紹了Python2.x版本中maketrans()方法的使用介紹,是Python學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05python 利用PyAutoGUI快速構(gòu)建自動化操作腳本
我們經(jīng)常遇到需要進行大量重復(fù)操作的時候,比如:網(wǎng)頁上填表,對 web 版本 OA 進行操作,自動化測試或者給新系統(tǒng)首次添加數(shù)據(jù)等,今天就利用PyAutoGUI構(gòu)建自動化操作腳本完成這些重復(fù)的需求2021-05-05python實現(xiàn)學(xué)生信息管理系統(tǒng)源碼
這篇文章主要為大家詳細介紹了python實現(xiàn)學(xué)生信息管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02python數(shù)據(jù)庫操作指南之PyMysql使用詳解
PyMySQL是在Python3.x版本中用于連接MySQL服務(wù)器的一個庫,Python2 中則使用mysqldb,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)庫操作指南之PyMysql使用的相關(guān)資料,需要的朋友可以參考下2023-03-03Python獲取多進程執(zhí)行的返回值實現(xiàn)
本文主要介紹了Python獲取多進程執(zhí)行的返回值實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03