-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDictTools.py
54 lines (44 loc) · 1.73 KB
/
EDictTools.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
import re
entryRegex = r'(.*)\s\[(.*)\]\s\/(.*)'
entryKatakanaRegex = r'(.*)\s\/(.*)'
edict = []
class EdictEntry:
def __init__(self, word, reading, restOfEntry):
self.word = word
self.reading = reading
self.restOfEntry = restOfEntry
def __str__(self):
return "{} = {} {}".format(self.word, self.reading, self.restOfEntry)
def findEntries(inputText):
possibleEntries = []
for entry in edict:
if entry.word == inputText:
possibleEntries.append(entry)
if not possibleEntries:
print('Failed to find "{}" in edict'.format(inputText))
return possibleEntries
def loadEdict():
global edict
if not edict:
print('Loading Edict...')
edictFile = open('edict/edict', 'r', encoding='euc-jp')
for line in edictFile:
isKatakana = False
entryMatch = re.search(entryRegex, line)
if not entryMatch:
# Loan words only have katakana readings
entryMatch = re.search(entryKatakanaRegex, line)
if not entryMatch:
print("Error: could not parse dictionary line: \n\t{}".format(line))
continue
else:
isKatakana = True
if isKatakana:
edict.append(EdictEntry(entryMatch.group(1), entryMatch.group(1), entryMatch.group(2)))
else:
edict.append(EdictEntry(entryMatch.group(1), entryMatch.group(2), entryMatch.group(3)))
print('Loading Edict complete. {} entries found'.format(len(edict)))
loadEdict()
if __name__ == '__main__':
# print(findEntries('一回目')[0].reading)
print(findEntries('日中')[0].reading)