Spaces:
Running
Running
| 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 # 找不到單字 | |