File size: 3,087 Bytes
1e165e2
 
cf14f69
1e165e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f744dbb
 
 
 
 
 
1e165e2
 
 
 
 
 
 
 
 
 
 
 
 
cf14f69
 
9a2c62d
 
 
 
cf14f69
 
 
 
 
 
 
 
9a2c62d
cf14f69
 
 
 
 
 
 
 
 
 
 
 
 
 
260ca39
 
 
 
 
 
cf14f69
 
3c2b9b2
 
9a2c62d
 
 
cf14f69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sqlite3
import os
from vocab import get_word_info
from ai_sentence import generate_sentence
from tqdm import tqdm

DATA_DIR = "./data"
DB_PATH = os.path.join(DATA_DIR, "sentences.db")


def init_db():
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    c.execute('''
    CREATE TABLE IF NOT EXISTS sentences (
        word TEXT,
        phonetic TEXT,
        sentence TEXT,
        source TEXT,
        model TEXT,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (word, source, model)
    )
    ''')
    conn.commit()
    conn.close()


def get_sentences_by_word(word):
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    c.execute('SELECT word, phonetic, sentence, source, model FROM sentences WHERE word=?', (word,))
    results = c.fetchall()
    conn.close()
    return results

def get_sentence(word):
    result = get_sentences_by_word(word)
    if result:
        return result[0]  # 取第一筆資料
    return None


def save_sentence(word, phonetic, sentence, source, model):
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    c.execute('''
    INSERT INTO sentences (word, phonetic, sentence, source, model)
    VALUES (?, ?, ?, ?, ?)
    ON CONFLICT(word, source, model) DO UPDATE SET sentence=excluded.sentence, phonetic=excluded.phonetic
    ''', (word, phonetic, sentence, source, model))
    conn.commit()
    conn.close()


def generate_sentences(words, source, use_ai, model_name):
    result_display = ""
    current_status = ""

    for i, word in enumerate(words):
        current_status = f"⏳ 處理中:{i + 1}/{len(words)} - [{word}] 正在查詢句子..."

        word_info = get_word_info(source, word)
        phonetic = word_info['phonetic'] if word_info else "無"

        sentence_records = get_sentences_by_word(word)

        if use_ai or not sentence_records:
            try:
                current_status = f"🤖 使用 AI 生成 [{word}] 的句子中... (模型:{model_name})"
                sentence = generate_sentence(word, model_name)
                save_sentence(word, phonetic, sentence, 'ai', model_name)
                source_used = 'ai'
                model_used = model_name
            except Exception as e:
                sentence = f"[AI生成失敗:{e}]"
                source_used = "error"
                model_used = None
        else:
            sentence = sentence_records[0][2]
            source_used = sentence_records[0][3]
            model_used = sentence_records[0][4]

        result_display += f"""
            <div style="margin-bottom: 10px; padding: 8px; border-left: 4px solid #4CAF50; background-color: transparent;">
                <strong>單字:</strong> {word} <br>
                <strong>音標:</strong> {phonetic} <br>
                <strong>句子:</strong> {sentence} <br>
                <strong>來源:</strong> {source_used} {f"({model_used})" if model_used else ""}
            </div>
        """

       

    current_status = "✅ 所有單字處理完成!"

    return result_display, current_status