-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJMdictUtils.py
150 lines (126 loc) · 4.41 KB
/
JMdictUtils.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
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
import re
import json
import os
from tqdm import tqdm
import readchar
import pdb
import requests
import gzip
from JMdictToJSON import JMdictToJSON
from RandomWordsToJSON import RandomWordsToJSON
from QuerySpeedtest import QuerySpeedtest
from GenerateJLPTLists import GenerateJLPTLists
from Tests import TestModules
import sys
UTILS = [{'name': 'Generate Random words', 'utility': RandomWordsToJSON},
{'name': 'Output JSON to file', 'utility': JMdictToJSON},
{'name': 'JMdict Query Speedtest', 'utility': QuerySpeedtest},
{'name': 'Generate List of Words by JLPT', 'utility': GenerateJLPTLists},
]
UTIL_CHOICE = 0
UP_ARROW = "\x1b\x5b\x41"
DOWN_ARROW = "\x1b\x5b\x42"
ENTER = "\x0d"
JMDICT_URL = "http://ftp.edrdg.org/pub/Nihongo/JMdict_e.gz"
RESOURCE_DIR = os.path.join(os.path.curdir, 'Resources/')
def getArgs():
args = []
if len(sys.argv) > 0:
for i in range(1, len(sys.argv)):
option = re.search(r'-[-]*([a-z-]*)(=([a-z0-9]*))*', sys.argv[i])
if option == None:
raise Exception(sys.argv[i])
name = option.group(1)
value = option.group(3)
args.append({'name': name, 'value': value})
return args
def clearScreen():
os.system('cls')
def printMenu():
print('\n--------------- JMdict Utilities ---------------\n')
count = 0
for util in UTILS:
if count == UTIL_CHOICE:
print('>>> {util}'.format(util = util['name']))
else:
print(util['name'])
count += 1
def getInput():
print('\nPlease select a utility using ^, v, and enter')
return readchar.readkey()
def startUtility():
UTILS[UTIL_CHOICE]['utility'].startUtility()
def processInput(input):
global UTIL_CHOICE
if input == UP_ARROW:
UTIL_CHOICE = (UTIL_CHOICE - 1) % len(UTILS)
elif input == DOWN_ARROW:
UTIL_CHOICE = (UTIL_CHOICE + 1) % len(UTILS)
elif input == ENTER:
return startUtility()
showMenu()
def showMenu():
clearScreen()
printMenu()
input = getInput()
processInput(input)
def checkForDownload():
if 'JMdict_e' not in os.listdir('Resources'):
downloadJMdict()
unpackJMdict()
deleteJMdictZip()
def downloadJMdict():
if 'JMdict_e.gz' not in os.listdir('Resources'):
f = open("Resources/JMdict_e.gz", 'wb')
response = requests.get(JMDICT_URL, stream=True)
total = response.headers.get('content-length')
total = int(total)
print("\nDownloading JMdict.gz...")
with tqdm(total=total) as pbar:
for data in response.iter_content(chunk_size=max(int(total/1000), 1024*1024)):
downloaded = len(data)
f.write(data)
pbar.update(downloaded)
#print('\r[{}{}]'.format('█' * done, '.' * (50-done)))
def _reader_generator(reader):
b = reader(1024 * 1024)
while b:
yield b
b = reader(1024 * 1024)
def getGzipNewlineCount():
f = gzip.open('Resources/JMdict_e.gz', 'rb')
f_gen = _reader_generator(f.read)
return sum(buf.count(b'\n') for buf in f_gen)
def deleteJMdictZip():
try:
os.remove("Resources/JMdict_e.gz")
except Exception as e:
print("Error deleting 'JMdict_e.gz'")
def unpackJMdict():
linecount = getGzipNewlineCount()
print("\nUnpacking JMdict_e.gz\n")
with open('Resources/JMdict_e', 'wb') as f_out:
with gzip.open('Resources/JMdict_e.gz', 'rb') as f_in:
with tqdm(f_in, total=linecount) as pbar:
for line in f_in:
try:
f_out.write(line)
pbar.update(1)
except UnicodeDecodeError as e:
pdb.set_trace()
def checkForJMdict():
if 'JMdict_e.json' not in os.listdir(RESOURCE_DIR):
print("Requires 'JMdict_e.json' to run, press any key to run 'JMdictToJSON'")
readchar.readkey()
JMdictToJSON.startUtility()
def getJMdict():
checkForJMdict()
with open(os.path.join(RESOURCE_DIR, 'JMdict_e.json'), encoding='utf8') as f:
return json.loads(f.read())
def checkForRandomWords():
if 'randomWords.json' not in os.listdir(RESOURCE_DIR):
print("Requires 'randomWords.json' to run, press any key to run 'RandomWordsToJSON'")
readchar.readkey()
RandomWordsToJSON.startUtility()
if __name__ == "__main__":
showMenu()