欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 翻譯詞典小程序功能說(shuō)明

 更新時(shí)間:2025年05月17日 10:39:25   作者:龍泉寺天下行走  
本工具是基于Python開(kāi)發(fā)的智能翻譯系統(tǒng),采用有道詞典進(jìn)行翻譯,并具有本地詞典緩存以及單詞本功能,這篇文章主要介紹了Python 翻譯詞典小程序,需要的朋友可以參考下

一、概述

本工具是基于Python開(kāi)發(fā)的智能翻譯系統(tǒng),采用有道詞典進(jìn)行翻譯,并具有本地詞典緩存以及單詞本功能。 版本號(hào):v1.0  (2025-05-15)

二、核心功能說(shuō)明

1. 基礎(chǔ)翻譯功能

  • 即時(shí)翻譯:輸入英文單詞自動(dòng)獲取中文釋義
  • 詞性識(shí)別:自動(dòng)標(biāo)注單詞詞性(名詞/動(dòng)詞等)
  • 網(wǎng)絡(luò)查詢(xún):實(shí)時(shí)獲取最新詞典數(shù)據(jù)
  • 離線(xiàn)查詢(xún): 對(duì)以查過(guò)的單詞,首先在本地SQLITE數(shù)據(jù)庫(kù)查找

2. 數(shù)據(jù)存儲(chǔ)系統(tǒng)

  • 翻譯歷史
    • 自動(dòng)存儲(chǔ)所有查詢(xún)記錄
    • 字段包含:英文單詞、中文釋義、詞性、查詢(xún)時(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)先查詢(xún)?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ò)查詢(xún)請(qǐng)輸入w,直接退出請(qǐng)按回車(chē):").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 "無(wú)詞性標(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
                     第三人稱(chēng)單數(shù)
        waters
                     現(xiàn)在分詞
        watering
                     過(guò)去式
        watered
                     過(guò)去分詞
        watered
                   ]
請(qǐng)輸入英文單詞或命令(輸入\q退出): yes
-    yes (n.): adv. 是,是的n. 是(表示肯定)[
                    復(fù)數(shù)
        yesses或yeses
                     第三人稱(chēng)單數(shù)
        yesses或yeses
                     現(xiàn)在分詞
        yessing
                     過(guò)去式
        yessed
                     過(guò)去分詞
        yessed
                   ]
請(qǐng)輸入英文單詞或命令(輸入\q退出): level
-    level (n.): n. 數(shù)量,程度;標(biāo)準(zhǔn),水平;層次,級(jí)別;看待(或應(yīng)對(duì)、理解)事物的方式;水平高度,相對(duì)高度;樓層;平地;水平儀adj. 平坦的,水平的;相同價(jià)值的,相同地位的;比分相同的;平靜的,冷靜的v. 使平整;推倒,夷平;(使)比分相同;(尤指用槍?zhuān)┟闇?zhǔn);針對(duì)……(進(jìn)行批評(píng)等);穩(wěn)定下來(lái),達(dá)到平衡(level off);坦誠(chéng)相見(jiàn);作水準(zhǔn)測(cè)量【名】 (Level)(法)勒韋爾(人名)[
                    復(fù)數(shù)
        levels
                     第三人稱(chēng)單數(shù)
        levels
                     現(xiàn)在分詞
        levelling或leveling
                     過(guò)去式
        levelled或leveled
                     過(guò)去分詞
        levelled或leveled
                   ]
請(qǐng)輸入英文單詞或命令(輸入\q退出): jackfruit
-    jackfruit (n.): n. 木菠蘿;菠蘿蜜

到此這篇關(guān)于Python 翻譯詞典小程序功能說(shuō)明的文章就介紹到這了,更多相關(guān)Python 翻譯詞典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論