Memory module aligned with the paper pipeline. It contains factual memory, short-term memory, long-term memory, KCG-based memory reinforcement, forgetting, and reflection support.
| 10 | |
| 11 | |
| 12 | class Memory: |
| 13 | """Memory module aligned with the paper pipeline. |
| 14 | |
| 15 | It contains factual memory, short-term memory, long-term memory, KCG-based |
| 16 | memory reinforcement, forgetting, and reflection support. |
| 17 | """ |
| 18 | |
| 19 | def __init__(self, student_id: int, data_path: str | Path): |
| 20 | self.student_id = int(student_id) |
| 21 | self.factual: list[list[Any]] = [] |
| 22 | self.short: list[list[Any]] = [] |
| 23 | self.long: dict[str, list[Any]] = { |
| 24 | "significant_facts": [], |
| 25 | "learning_status": [], |
| 26 | "knowledge_proficiency": [], |
| 27 | "practiced_knowledge": [], |
| 28 | } |
| 29 | self.threshold = int(SIM_PARAMS["long_term_thresh"]) |
| 30 | self.short_size = int(SIM_PARAMS["short_term_size"]) |
| 31 | self.forget_lambda = float(SIM_PARAMS["forget_lambda"]) |
| 32 | self.relation_graph = RelationGraph(data_path) |
| 33 | self.knowledge_proficiency = KnowledgeProficiency(data_path) |
| 34 | |
| 35 | def retrieve_short(self) -> list[list[Any]]: |
| 36 | self.short = self.factual[-self.short_size:] |
| 37 | return self.short |
| 38 | |
| 39 | def retrieve_long(self, current_concept: str | None = None, time_step: int = 1) -> dict[str, Any]: |
| 40 | concepts = list(self.long["practiced_knowledge"]) |
| 41 | if current_concept: |
| 42 | concepts.append(normalize_concept(current_concept)) |
| 43 | kp = self._proficiency_context(concepts, time_step) |
| 44 | self.long["knowledge_proficiency"] = kp |
| 45 | return { |
| 46 | "significant_facts": self.long["significant_facts"], |
| 47 | "learning_status": self.long["learning_status"], |
| 48 | "knowledge_proficiency": kp, |
| 49 | "practiced_knowledge": self.long["practiced_knowledge"], |
| 50 | } |
| 51 | |
| 52 | def _proficiency_context(self, concepts: list[str], time_step: int) -> list[dict[str, Any]]: |
| 53 | seen: set[str] = set() |
| 54 | context: list[dict[str, Any]] = [] |
| 55 | for concept in concepts: |
| 56 | norm = normalize_concept(concept) |
| 57 | if not norm or norm in seen: |
| 58 | continue |
| 59 | seen.add(norm) |
| 60 | value = self.knowledge_proficiency.value(self.student_id, norm, max(0, time_step - 2)) |
| 61 | context.append({"concept": norm, "value": value, "level": self.knowledge_proficiency.tier(value)}) |
| 62 | return context |
| 63 | |
| 64 | def similarity_by_kcg(self, record: list[Any]) -> list[int]: |
| 65 | sim: list[int] = [] |
| 66 | current = normalize_concept(record[1]) |
| 67 | for memory_element in self.factual: |
| 68 | previous = normalize_concept(memory_element[1]) |
| 69 | if self.relation_graph.is_related(current, previous): |