Spaces:
Sleeping
Sleeping
Update vocab.py
Browse files
vocab.py
CHANGED
@@ -5,44 +5,43 @@ DATA_DIR = "./data"
|
|
5 |
DB_PATH = os.path.join(DATA_DIR, "sentences.db")
|
6 |
|
7 |
|
8 |
-
#
|
9 |
def init_db():
|
10 |
conn = sqlite3.connect(DB_PATH)
|
11 |
c = conn.cursor()
|
12 |
c.execute('''
|
13 |
CREATE TABLE IF NOT EXISTS sentences (
|
14 |
-
word TEXT
|
15 |
phonetic TEXT,
|
16 |
sentence TEXT,
|
17 |
-
|
|
|
|
|
|
|
18 |
)
|
19 |
''')
|
20 |
conn.commit()
|
21 |
conn.close()
|
22 |
|
23 |
|
24 |
-
#
|
25 |
-
def
|
26 |
conn = sqlite3.connect(DB_PATH)
|
27 |
c = conn.cursor()
|
28 |
-
c.execute('SELECT word, phonetic, sentence FROM sentences WHERE word=?', (word,))
|
29 |
-
|
30 |
conn.close()
|
31 |
-
return
|
32 |
|
33 |
|
34 |
-
#
|
35 |
-
def save_sentence(word, phonetic, sentence):
|
36 |
conn = sqlite3.connect(DB_PATH)
|
37 |
c = conn.cursor()
|
38 |
c.execute('''
|
39 |
-
INSERT INTO sentences (word, phonetic, sentence)
|
40 |
-
VALUES (?, ?, ?)
|
41 |
-
ON CONFLICT(word) DO UPDATE SET sentence=excluded.sentence, phonetic=excluded.phonetic
|
42 |
-
''', (word, phonetic, sentence))
|
43 |
conn.commit()
|
44 |
conn.close()
|
45 |
-
|
46 |
-
|
47 |
-
# 初始化資料表
|
48 |
-
init_db()
|
|
|
5 |
DB_PATH = os.path.join(DATA_DIR, "sentences.db")
|
6 |
|
7 |
|
8 |
+
# 初始化資料表(保險起見,但你本地應該已建過了)
|
9 |
def init_db():
|
10 |
conn = sqlite3.connect(DB_PATH)
|
11 |
c = conn.cursor()
|
12 |
c.execute('''
|
13 |
CREATE TABLE IF NOT EXISTS sentences (
|
14 |
+
word TEXT,
|
15 |
phonetic TEXT,
|
16 |
sentence TEXT,
|
17 |
+
source TEXT,
|
18 |
+
model TEXT,
|
19 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
20 |
+
PRIMARY KEY (word, source, model)
|
21 |
)
|
22 |
''')
|
23 |
conn.commit()
|
24 |
conn.close()
|
25 |
|
26 |
|
27 |
+
# 查詢某個單字的所有例句,包含來源 & 模型
|
28 |
+
def get_sentences_by_word(word):
|
29 |
conn = sqlite3.connect(DB_PATH)
|
30 |
c = conn.cursor()
|
31 |
+
c.execute('SELECT word, phonetic, sentence, source, model FROM sentences WHERE word=?', (word,))
|
32 |
+
results = c.fetchall()
|
33 |
conn.close()
|
34 |
+
return results
|
35 |
|
36 |
|
37 |
+
# 儲存句子(GPT 生成 or 句庫)
|
38 |
+
def save_sentence(word, phonetic, sentence, source, model):
|
39 |
conn = sqlite3.connect(DB_PATH)
|
40 |
c = conn.cursor()
|
41 |
c.execute('''
|
42 |
+
INSERT INTO sentences (word, phonetic, sentence, source, model)
|
43 |
+
VALUES (?, ?, ?, ?, ?)
|
44 |
+
ON CONFLICT(word, source, model) DO UPDATE SET sentence=excluded.sentence, phonetic=excluded.phonetic
|
45 |
+
''', (word, phonetic, sentence, source, model))
|
46 |
conn.commit()
|
47 |
conn.close()
|
|
|
|
|
|
|
|