-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules_memory_palace.py
More file actions
292 lines (245 loc) · 10.2 KB
/
modules_memory_palace.py
File metadata and controls
292 lines (245 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
Memory Palace system using Method of Loci
"""
import json
from typing import List, Dict, Tuple, Optional
from dataclasses import dataclass, asdict
from datetime import datetime
from pathlib import Path
import logging
from config import PALACES_DIR, MAX_WORDS_PER_LOCATION
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class MemoryLocation:
"""A location in the memory palace"""
id: int
name: str
description: str
coordinates: Tuple[int, int]
connected_to: List[int]
image_url: Optional[str] = None
cultural_significance: str = ""
@dataclass
class MemoryItem:
"""A word/phrase stored in the memory palace"""
bashkir_word: str
english_translation: str
phonetic: str
major_system_code: str
image_mnemonic: str
location_id: int
cultural_context: str = ""
audio_path: Optional[str] = None
review_count: int = 0
last_reviewed: Optional[str] = None
easiness_factor: float = 2.5
interval: int = 0
next_review: Optional[str] = None
created_date: str = ""
def __post_init__(self):
if not self.created_date:
self.created_date = datetime.now().isoformat()
class MemoryPalace:
"""Memory Palace for organizing vocabulary using Method of Loci"""
def __init__(self, name: str, theme: str = "Bashkir Village"):
self.name = name
self.theme = theme
self.locations: Dict[int, MemoryLocation] = {}
self.items: Dict[str, MemoryItem] = {}
self._create_default_locations()
logger.info(f"Memory Palace created: {name}")
def _create_default_locations(self):
"""Create a traditional Bashkir village layout"""
default_locations = [
MemoryLocation(
1, "Village Gate (Ҡала Ҡапҡаһы)",
"Wooden entrance gate with traditional carvings",
(0, 0), [2],
cultural_significance="The gate represents the boundary between worlds"
),
MemoryLocation(
2, "Central Square (Урта мәйҙан)",
"Village gathering place with well and benches",
(5, 0), [1, 3, 4],
cultural_significance="Center of community life and celebrations"
),
MemoryLocation(
3, "Yurt (Тирмә)",
"Traditional felt dwelling with wooden frame",
(10, 5), [2, 5],
cultural_significance="Symbol of nomadic heritage"
),
MemoryLocation(
4, "Stable (Ат ҡотоһо)",
"Horse shelter - horses are sacred in Bashkir culture",
(10, -5), [2, 6],
cultural_significance="Horses represent freedom and nobility"
),
MemoryLocation(
5, "Kitchen House (Аш йорто)",
"Outdoor cooking area with traditional oven",
(15, 5), [3, 7],
cultural_significance="Where traditional dishes are prepared"
),
MemoryLocation(
6, "Workshop (Осталыҡ)",
"Craft-making space for traditional arts",
(15, -5), [4, 8],
cultural_significance="Preserves ancient crafts and skills"
),
MemoryLocation(
7, "Garden (Баҡса)",
"Herb and vegetable garden with medicinal plants",
(20, 5), [5],
cultural_significance="Connection to nature and healing traditions"
),
MemoryLocation(
8, "Forest Edge (Урманситендә)",
"Sacred gathering place near ancient trees",
(20, -5), [6],
cultural_significance="Place of spiritual connection and folk tales"
)
]
for loc in default_locations:
self.locations[loc.id] = loc
logger.info(f"Created {len(default_locations)} locations")
def add_word(self,
bashkir: str,
english: str,
phonetic: str,
location_id: int,
cultural_context: str = "",
audio_path: Optional[str] = None,
image_mnemonic: Optional[str] = None) -> MemoryItem:
"""
Add a word to the memory palace
Args:
bashkir: Bashkir word
english: English translation
phonetic: Phonetic transcription
location_id: Location to place the word
cultural_context: Cultural information
audio_path: Path to audio file
image_mnemonic: Custom mnemonic (auto-generated if None)
Returns:
Created MemoryItem
"""
from .major_system import MajorSystemEncoder
# Check location capacity
words_at_location = sum(
1 for item in self.items.values()
if item.location_id == location_id
)
if words_at_location >= MAX_WORDS_PER_LOCATION:
logger.warning(f"Location {location_id} is at capacity")
# Generate major system code
major_code = MajorSystemEncoder.create_mnemonic_code(bashkir, english)
# Generate or use provided mnemonic
if image_mnemonic is None:
image_mnemonic = self._generate_image_mnemonic(
bashkir, english, self.locations[location_id]
)
item = MemoryItem(
bashkir_word=bashkir,
english_translation=english,
phonetic=phonetic,
major_system_code=major_code,
image_mnemonic=image_mnemonic,
location_id=location_id,
cultural_context=cultural_context,
audio_path=audio_path
)
self.items[bashkir] = item
logger.info(f"Added word: {bashkir} → {english} at location {location_id}")
return item
def _generate_image_mnemonic(self,
bashkir: str,
english: str,
location: MemoryLocation) -> str:
"""Generate a vivid mnemonic combining word and location"""
return (
f"At the {location.name}, imagine a vivid scene with '{english}' "
f"({bashkir}). {location.description}. "
f"Make it absurd, colorful, and emotionally engaging."
)
def get_word(self, bashkir: str) -> Optional[MemoryItem]:
"""Get a word from the palace"""
return self.items.get(bashkir)
def get_words_at_location(self, location_id: int) -> List[MemoryItem]:
"""Get all words at a specific location"""
return [
item for item in self.items.values()
if item.location_id == location_id
]
def get_journey(self, start_location: int = 1) -> List[MemoryLocation]:
"""
Get ordered path through palace using BFS
Args:
start_location: Starting location ID
Returns:
Ordered list of locations
"""
visited = set()
journey = []
queue = [start_location]
while queue:
loc_id = queue.pop(0)
if loc_id in visited:
continue
visited.add(loc_id)
journey.append(self.locations[loc_id])
# Add connected locations
for connected_id in self.locations[loc_id].connected_to:
if connected_id not in visited:
queue.append(connected_id)
return journey
def get_statistics(self) -> Dict:
"""Get palace statistics"""
total_words = len(self.items)
reviewed_words = sum(1 for item in self.items.values() if item.review_count > 0)
total_reviews = sum(item.review_count for item in self.items.values())
words_by_location = {}
for item in self.items.values():
loc_name = self.locations[item.location_id].name
words_by_location[loc_name] = words_by_location.get(loc_name, 0) + 1
return {
'total_words': total_words,
'reviewed_words': reviewed_words,
'total_reviews': total_reviews,
'words_by_location': words_by_location,
'total_locations': len(self.locations),
'locations_with_words': len(set(item.location_id for item in self.items.values()))
}
def save(self, filepath: Optional[Path] = None):
"""Save palace to JSON file"""
if filepath is None:
filepath = PALACES_DIR / f"{self.name.lower().replace(' ', '_')}.json"
data = {
'name': self.name,
'theme': self.theme,
'locations': {k: asdict(v) for k, v in self.locations.items()},
'items': {k: asdict(v) for k, v in self.items.items()},
'saved_date': datetime.now().isoformat()
}
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
logger.info(f"Palace saved to: {filepath}")
@classmethod
def load(cls, filepath: Path) -> 'MemoryPalace':
"""Load palace from JSON file"""
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
palace = cls(data['name'], data['theme'])
# Load locations
palace.locations = {
int(k): MemoryLocation(**v)
for k, v in data['locations'].items()
}
# Load items
palace.items = {
k: MemoryItem(**v)
for k, v in data['items'].items()
}
logger.info(f"Palace loaded from: {filepath}")
return palace