-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.py
68 lines (47 loc) · 1.58 KB
/
dictionary.py
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
"""
Load different kinds of notes into memory from the collection
to form dictionaries and corpuses.
"""
from anki.collection import Collection
from anki.notes import Note
from config import VocabFields, COLLECTION_PATH, VOCAB_NOTES, HANJA_NOTES, HanjaFields
from data_cleaning import extract_word, extract_hanjas
from typing import NamedTuple
class Term(NamedTuple):
value: str
meaning: str
extra: dict
class Dictionary:
def __init__(self):
self._terms = {}
def add(self, value, meaning, extra=None):
self._terms[value] = Term(value, meaning, dict(extra or {}))
def lookup(self, value):
return self._terms.get(value)
def __iter__(self):
return iter(self._terms.values())
def load_vocab():
dictionary = Dictionary()
col = Collection(COLLECTION_PATH)
note_ids = col.find_notes(VOCAB_NOTES)
notes = [Note(col=col, id=note_id) for note_id in note_ids]
for note in notes:
items = dict(note.items())
korean = extract_word(items[VocabFields.KOREAN])
meaning = extract_word(items[VocabFields.ENGLISH])
hanja = extract_hanjas(items[VocabFields.HANJA])
dictionary.add(korean, meaning, extra={'hanja': hanja})
col.close()
return dictionary
def load_hanja():
dictionary = Dictionary()
col = Collection(COLLECTION_PATH)
note_ids = col.find_notes(HANJA_NOTES)
notes = [Note(col=col, id=note_id) for note_id in note_ids]
for note in notes:
items = dict(note.items())
hanja = items[HanjaFields.HANJA].strip()
meaning = items[HanjaFields.MEANING].strip()
dictionary.add(hanja, meaning)
col.close()
return dictionary