diff --git a/app/vocabulary.py b/app/vocabulary.py new file mode 100644 index 0000000..8d90f6e --- /dev/null +++ b/app/vocabulary.py @@ -0,0 +1,163 @@ +import json +import os +from datetime import datetime + + +class VocabularyManager: + def __init__(self, data_file="vocabulary_data.json"): + """ + 初始化词汇管理器 + :param data_file: 词汇数据存储文件 + """ + self.data_file = data_file + self.vocabulary = {} + self._load_data() + + def _load_data(self): + """从文件加载词汇数据""" + if os.path.exists(self.data_file): + try: + with open(self.data_file, 'r', encoding='utf-8') as f: + self.vocabulary = json.load(f) + except (json.JSONDecodeError, FileNotFoundError): + self.vocabulary = {} + + def _save_data(self): + """保存词汇数据到文件""" + with open(self.data_file, 'w', encoding='utf-8') as f: + json.dump(self.vocabulary, f, ensure_ascii=False, indent=2) + + def add_word(self, word, definition, example=None, tags=None): + """ + 添加新单词 + :param word: 单词 + :param definition: 释义 + :param example: 例句(可选) + :param tags: 标签列表(可选) + :return: 添加结果消息 + """ + if word.lower() in self.vocabulary: + return f"'{word}' already exists in vocabulary" + + word_data = { + 'definition': definition, + 'example': example, + 'tags': tags if tags else [], + 'date_added': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + 'review_count': 0, + 'last_reviewed': None + } + self.vocabulary[word.lower()] = word_data + self._save_data() + return f"'{word}' added successfully" + + def get_word(self, word): + """ + 获取单词详情 + :param word: 要查询的单词 + :return: 单词数据字典(不存在则返回None) + """ + return self.vocabulary.get(word.lower()) + + def update_word(self, word, definition=None, example=None, tags=None): + """ + 更新单词信息 + :param word: 要更新的单词 + :param definition: 新释义(可选) + :param example: 新例句(可选) + :param tags: 新标签列表(可选) + :return: 更新结果消息 + """ + word_key = word.lower() + if word_key not in self.vocabulary: + return f"'{word}' not found in vocabulary" + + if definition: + self.vocabulary[word_key]['definition'] = definition + if example is not None: # 允许空字符串 + self.vocabulary[word_key]['example'] = example + if tags is not None: # 允许空列表 + self.vocabulary[word_key]['tags'] = tags + + self._save_data() + return f"'{word}' updated successfully" + + def delete_word(self, word): + """ + 删除单词 + :param word: 要删除的单词 + :return: 删除结果消息 + """ + word_key = word.lower() + if word_key in self.vocabulary: + del self.vocabulary[word_key] + self._save_data() + return f"'{word}' deleted successfully" + return f"'{word}' not found in vocabulary" + + def list_all_words(self): + """ + 获取所有单词列表 + :return: 按添加时间排序的单词列表 + """ + return sorted( + self.vocabulary.keys(), + key=lambda w: self.vocabulary[w]['date_added'], + reverse=True + ) + + def record_review(self, word): + """ + 记录单词复习 + :param word: 复习的单词 + :return: 复习记录结果 + """ + word_key = word.lower() + if word_key in self.vocabulary: + self.vocabulary[word_key]['review_count'] += 1 + self.vocabulary[word_key]['last_reviewed'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + self._save_data() + return f"Review recorded for '{word}'" + return f"'{word}' not found in vocabulary" + + def search_words(self, keyword): + """ + 搜索包含关键字的单词 + :param keyword: 搜索关键字 + :return: 匹配的单词列表 + """ + keyword = keyword.lower() + return [ + word for word, data in self.vocabulary.items() + if keyword in word or + keyword in data['definition'].lower() or + (data['example'] and keyword in data['example'].lower()) + ] + + +# 示例用法 +if __name__ == "__main__": + vm = VocabularyManager() + + # 添加单词 + print(vm.add_word("ephemeral", "短暂的,瞬息的", + "Beauty is ephemeral, lasting only a few moments.", + ["adjective", "literary"])) + + # 获取单词详情 + print("\nWord details:") + print(vm.get_word("ephemeral")) + + # 更新单词 + print(vm.update_word("ephemeral", tags=["adjective", "poetic"])) + + # 记录复习 + print(vm.record_review("ephemeral")) + + # 列出所有单词 + print("\nAll words:") + print(vm.list_all_words()) + + # 搜索单词 + print("\nSearch results:") + print(vm.search_words("brief")) \ No newline at end of file