107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
import heurams.services.timer as timer
|
|
from heurams.context import config_var
|
|
from heurams.kernel.algorithms import algorithms
|
|
|
|
|
|
class Electron:
|
|
"""电子: 记忆分析元数据及算法"""
|
|
|
|
def __init__(self, ident: str, algodata: dict = {}, algo_name: str = "supermemo2"):
|
|
"""初始化电子对象 (记忆数据)
|
|
|
|
Args:
|
|
ident: 算法的唯一标识符, 用于区分不同的算法实例, 使用 algodata[ident] 获取
|
|
algodata: 算法数据字典, 包含算法的各项参数和设置
|
|
algo: 使用的算法模块标识
|
|
"""
|
|
self.algodata = algodata
|
|
self.ident = ident
|
|
self.algo = algorithms[algo_name]
|
|
|
|
if self.algo not in self.algodata.keys():
|
|
self.algodata[self.algo] = {}
|
|
if not self.algodata[self.algo]:
|
|
self._default_init(self.algo.defaults)
|
|
|
|
def _default_init(self, defaults: dict):
|
|
"""默认初始化包装"""
|
|
self.algodata[self.algo] = defaults.copy()
|
|
|
|
def activate(self):
|
|
"""激活此电子"""
|
|
self.algodata[self.algo]["is_activated"] = 1
|
|
self.algodata[self.algo]["last_modify"] = timer.get_timestamp()
|
|
|
|
def modify(self, var: str, value):
|
|
"""修改 algodata[algo] 中子字典数据"""
|
|
if var in self.algodata[self.algo]:
|
|
self.algodata[self.algo][var] = value
|
|
self.algodata[self.algo]["last_modify"] = timer.get_timestamp()
|
|
else:
|
|
print(f"警告: '{var}' 非已知元数据字段")
|
|
|
|
def is_due(self):
|
|
"""是否应该复习"""
|
|
return self.algo.is_due(self.algodata)
|
|
|
|
def is_activated(self):
|
|
return self.algodata[self.algo]["is_activated"]
|
|
|
|
def rate(self):
|
|
"评价"
|
|
return self.algo.rate(self.algodata)
|
|
|
|
def nextdate(self) -> int:
|
|
return self.algo.nextdate(self.algodata)
|
|
|
|
def revisor(self, quality: int = 5, is_new_activation: bool = False):
|
|
"""算法迭代决策机制实现
|
|
|
|
Args:
|
|
quality (int): 记忆保留率量化参数 (0-5)
|
|
is_new_activation (bool): 是否为初次激活
|
|
"""
|
|
self.algo.revisor(self.algodata, quality, is_new_activation)
|
|
|
|
def __str__(self):
|
|
return (
|
|
f"记忆单元预览 \n"
|
|
f"标识符: '{self.ident}' \n"
|
|
f"算法: {self.algo} \n"
|
|
f"易度系数: {self.algodata[self.algo]['efactor']:.2f} \n"
|
|
f"已经重复的次数: {self.algodata[self.algo]['rept']} \n"
|
|
f"下次间隔: {self.algodata[self.algo]['interval']} 天 \n"
|
|
f"下次复习日期时间戳: {self.algodata[self.algo]['next_date']}"
|
|
)
|
|
|
|
def __eq__(self, other):
|
|
if self.ident == other.ident:
|
|
return True
|
|
return False
|
|
|
|
def __hash__(self):
|
|
return hash(self.ident)
|
|
|
|
def __getitem__(self, key):
|
|
if key == "ident":
|
|
return self.ident
|
|
if key in self.algodata[self.algo]:
|
|
return self.algodata[self.algo][key]
|
|
else:
|
|
raise KeyError(f"键 '{key}' 未在 algodata[self.algo] 中")
|
|
|
|
def __setitem__(self, key, value):
|
|
if key == "ident":
|
|
raise AttributeError("ident 应为只读")
|
|
self.algodata[self.algo][key] = value
|
|
self.algodata[self.algo]["last_modify"] = timer.get_timestamp()
|
|
|
|
def __len__(self):
|
|
"""仅返回当前算法的配置数量"""
|
|
return len(self.algodata[self.algo])
|
|
|
|
@staticmethod
|
|
def placeholder():
|
|
"""生成一个电子占位符"""
|
|
return Electron("电子对象样例内容", {})
|