-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatindict.py
86 lines (75 loc) · 2.76 KB
/
latindict.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from dataclasses import dataclass, field
from latindictbase import Definition, LatinEntry
from latindictinfl import *
from latindictsrc import dictEntries
import re
import random
def regularize(word: str) -> str:
word = word.lower()
mapping = str.maketrans("āēīōūȳjuäëïöüÿáéíóúýàèìòùỳ",
"aeiovyivaeiovyaeiovyaeiovy")
word = word.translate(mapping)
word = word.replace('æ', "ae")
word = word.replace('œ', "oe")
word = word.replace('-', "")
return word
@dataclass
class LatinDict(object):
entries: dict[tuple[str, int], LatinEntry]
entryCount: int = 0
def getInfo(self) -> str:
return "Hoc est lexicon Latino-Anglicum a Kotobà Trilio factum.\nNunc insunt {} vocabula.".format(self.entryCount)
def scan(self, filename):
for e in dictEntries:
try:
self.addEntry(e.head, e)
self.entryCount += 1
if e.sr:
for s in e.sr:
self.addEntry(s, e)
except BaseException as ex:
print(ex, "with", e)
continue
self.entries = dict(sorted(self.entries.items(), key=lambda x: x[0]))
def addEntry(self, entryName: str, entry: LatinEntry):
entryName = regularize(entryName)
i = 0
while (entryName, i) in self.entries:
i += 1
self.entries[(entryName, i)] = entry
def getEntry(self, entryName: str):
entryName = regularize(entryName)
i = 0
entries = []
while (entryName, i) in self.entries:
isDuplicate = False
for eIn in entries:
if self.entries[(entryName, i)] == eIn:
isDuplicate = True
if not isDuplicate:
entries.append(self.entries[(entryName, i)])
i += 1
#print(entries, len(entries))
return entries
def getEntryByDef(self, query: str, precise: bool=False):
entries = []
for e in self.entries.values():
isDuplicate = False
for d in e.defs:
if precise:
pattern = r"(\s|(^)){}[,;]".format(query)
else:
pattern = r"\b{}\b".format(query)
if re.search(pattern, d.head):
for eIn in entries:
if e == eIn:
isDuplicate = True
if not isDuplicate:
entries.append(e)
return entries
def getRandomEntry(self):
entry = random.choice(list(self.entries.values()))
return entry
def getVolume(self) -> int:
return self.entryCount
latinDict = LatinDict({})