Spaces:
Running
Running
File size: 764 Bytes
1112df1 4f885c6 1112df1 dd2233b 3ed2d28 f2efbc0 4982f66 3ed2d28 4982f66 3ed2d28 4982f66 3ed2d28 |
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 |
import os
import json
DATA_DIR = "./data"
# 取得單字庫名稱清單
def get_sources():
files = os.listdir(DATA_DIR)
sources = [f.split(".json")[0] for f in files if f.endswith(".json")]
return sources
# 取得單字庫的所有單字(回傳整個物件列表)
def get_words_from_source(source):
data_path = os.path.join(DATA_DIR, f"{source}.json")
with open(data_path, 'r', encoding='utf-8') as f:
words = json.load(f)
return words
# 查詢單字細節(音標等)
def get_word_info(source, word):
words = get_words_from_source(source)
for entry in words:
if entry['word'] == word:
return entry # 回傳 {'id': 1, 'word': 'apple', 'phonetic': '...'}
return None # 找不到單字
|