|
|
|
|
@ -0,0 +1,100 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Created on Sat Jan 3 18:40:21 2026
|
|
|
|
|
|
|
|
|
|
@author: Admin
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# 单词本文件名
|
|
|
|
|
WORD_BOOK_FILE = "word_book.json"
|
|
|
|
|
|
|
|
|
|
def load_word_book():
|
|
|
|
|
"""加载单词本,若文件不存在则创建空字典"""
|
|
|
|
|
if os.path.exists(WORD_BOOK_FILE):
|
|
|
|
|
with open(WORD_BOOK_FILE, "r", encoding="utf-8") as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def save_word_book(word_book):
|
|
|
|
|
"""保存单词本到文件"""
|
|
|
|
|
with open(WORD_BOOK_FILE, "w", encoding="utf-8") as f:
|
|
|
|
|
json.dump(word_book, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
|
def add_word(word, translation):
|
|
|
|
|
"""添加新单词,初始复习次数0,下次复习时间为当前时间"""
|
|
|
|
|
word_book = load_word_book()
|
|
|
|
|
if word not in word_book:
|
|
|
|
|
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
# 单词信息:翻译、复习次数、下次复习时间
|
|
|
|
|
word_book[word] = {
|
|
|
|
|
"translation": translation,
|
|
|
|
|
"review_count": 0,
|
|
|
|
|
"next_review": now
|
|
|
|
|
}
|
|
|
|
|
save_word_book(word_book)
|
|
|
|
|
print(f"单词【{word}】添加成功!")
|
|
|
|
|
else:
|
|
|
|
|
print(f"单词【{word}】已存在!")
|
|
|
|
|
|
|
|
|
|
def get_review_words():
|
|
|
|
|
"""获取当前需要复习的单词"""
|
|
|
|
|
word_book = load_word_book()
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
review_list = []
|
|
|
|
|
for word, info in word_book.items():
|
|
|
|
|
next_review = datetime.datetime.strptime(info["next_review"], "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
if now >= next_review:
|
|
|
|
|
review_list.append((word, info["translation"]))
|
|
|
|
|
return review_list
|
|
|
|
|
|
|
|
|
|
def review_word(word):
|
|
|
|
|
"""完成单词复习,更新复习次数和下次复习时间(基于遗忘曲线调整间隔)"""
|
|
|
|
|
word_book = load_word_book()
|
|
|
|
|
if word in word_book:
|
|
|
|
|
count = word_book[word]["review_count"] + 1
|
|
|
|
|
intervals = [0.5, 2, 8, 24, 48, 168]
|
|
|
|
|
interval = intervals[min(count, len(intervals)-1)]
|
|
|
|
|
next_time = datetime.datetime.now() + datetime.timedelta(hours=interval)
|
|
|
|
|
|
|
|
|
|
word_book[word]["review_count"] = count
|
|
|
|
|
word_book[word]["next_review"] = next_time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
save_word_book(word_book)
|
|
|
|
|
print(f"单词【{word}】复习完成!下次复习时间:{next_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
|
else:
|
|
|
|
|
print(f"单词【{word}】不存在!")
|
|
|
|
|
|
|
|
|
|
# 主函数:简易交互界面
|
|
|
|
|
def main():
|
|
|
|
|
while True:
|
|
|
|
|
print("\n===== 基于遗忘曲线的单词本 =====")
|
|
|
|
|
print("1. 添加单词")
|
|
|
|
|
print("2. 复习单词")
|
|
|
|
|
print("3. 退出")
|
|
|
|
|
choice = input("请输入操作序号:")
|
|
|
|
|
|
|
|
|
|
if choice == "1":
|
|
|
|
|
word = input("请输入单词:")
|
|
|
|
|
translation = input("请输入翻译:")
|
|
|
|
|
add_word(word, translation)
|
|
|
|
|
elif choice == "2":
|
|
|
|
|
review_words = get_review_words()
|
|
|
|
|
if not review_words:
|
|
|
|
|
print("暂无需要复习的单词!")
|
|
|
|
|
continue
|
|
|
|
|
print("\n需要复习的单词:")
|
|
|
|
|
for idx, (word, trans) in enumerate(review_words, 1):
|
|
|
|
|
print(f"{idx}. {word} - {trans}")
|
|
|
|
|
target_word = input("请输入要复习的单词:")
|
|
|
|
|
review_word(target_word)
|
|
|
|
|
elif choice == "3":
|
|
|
|
|
print("退出程序,下次再见!")
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
print("输入错误,请重新选择!")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|