forked from aehlke/jlpt-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.py
More file actions
184 lines (153 loc) · 5.75 KB
/
classify.py
File metadata and controls
184 lines (153 loc) · 5.75 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
#!/usr/bin/env python
import json
import os
import zipfile
# https://stackoverflow.com/a/48374671/89373
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plot
import requests
JMDICT_JSON_URL = 'https://github.com/scriptin/jmdict-simplified/releases/download/2.0.0/jmdict_eng.json.zip'
JLPT_COLORS = {
1: '#d84c43',
2: '#f6934b',
3: '#dd9f40',
4: '#48a17a',
5: '#5890c5',
}
def _make_build_dir():
if not os.path.exists('build'):
os.makedirs('build')
def _get_cb4960_word_frequencies():
'''
Maps word to frequency_ordinal in corpus.
'''
frequencies = {}
with open('cb4960_novel_word_freq.txt', 'r', encoding="utf-8") as f:
for line_number, line in enumerate(f):
frequency, word, *rest = line.split('\t')
frequencies[word] = line_number
return frequencies
def _get_wikipedia_word_frequencies():
'''
Maps word to frequency rank in corpus.
'''
frequencies = {}
with open('japanese_wikipedia_word_freq.csv', 'r', encoding="utf-8") as f:
next(f) # Skip header row.
for line in f:
rank, word, *rest = line.split(',')
frequencies[word] = int(rank)
return frequencies
def _get_vn_word_frequencies():
'''
Maps word to frequency rank in visual novel corpus.
'''
frequencies = {}
with open('visual_novel_word_freq.txt', 'r', encoding="utf-8") as f:
for (line_number, line) in enumerate(f):
(freq, pos1, pos2, pos3, pos4, reading_spelling, etym, word_class,
freq_raw, conj, pronunciation, spelling, *rest) = line.split('\t')
frequencies[spelling] = line_number
return frequencies
def _get_narou_word_frequencies():
'''
Maps word to frequency rank in corpus.
'''
frequencies = {}
with open('narou_word_freq.txt', 'r', encoding="utf-8") as f:
for (line_number, line) in enumerate(f):
(freq, pos1, pos2, pos3, pos4, reading_spelling, etym, word_class,
freq_raw, conj, pronunciation, spelling, *rest) = line.split('\t')
frequencies[spelling] = line_number
return frequencies
def _load_jmdict():
'''
Maps JMDict ID to JMDict entry.
'''
if not os.path.exists('build/jmdict_eng.json'):
r = requests.get(JMDICT_JSON_URL, stream=True)
r.raise_for_status()
with open('build/jmdict_eng.json.zip', 'wb') as f:
f.write(r.content)
zip_ref = zipfile.ZipFile('build/jmdict_eng.json.zip', 'r')
zip_ref.extractall('build/')
zip_ref.close()
with open('build/jmdict_eng.json', 'r', encoding="utf-8") as f:
jmdict = json.load(f)
return {entry['id']: entry for entry in jmdict['words']}
def _get_jlpt_lists(jmdict):
'''
Maps JLPT level integer to a list of JMDict entries.
'''
jlpts = {}
for level in range(1, 6):
with open(f'jlpt-n{level}.csv', 'r', encoding="utf-8") as f:
content = f.readlines()
items = []
for line in content:
if '#' in line:
continue
if int(line) not in jmdict:
print(f' {int(line)} missing from JMDict; skipping.')
continue
items.append(jmdict[int(line)])
jlpts[level] = items
return jlpts
def plot_jlpt_list_densities(jlpt_levels, word_frequencies):
fig, ax = plot.subplots(5, sharex=True)
ax[0].set_title(
f'Histogram of JLPT Levels Mapped to Word Frequency List')
ax[0].set_xlim([0, 10000])
for (level_number, level_entries) in jlpt_levels.items():
subplot = ax[level_number - 1]
subplot.set_ylabel(f'N{level_number}')
subplot.yaxis.set_visible(False)
data = set()
max_ordinal = 0
for level_entry in level_entries:
found_data = None
for entry_subtype in ['kanji', 'kana']:
for item in level_entry[entry_subtype]:
if item['text'] in word_frequencies:
frequency_ordinal = word_frequencies[item['text']]
found_data = frequency_ordinal
max_ordinal = max(frequency_ordinal, max_ordinal)
break
if found_data is not None:
break
if found_data is not None:
data.add(found_data)
sorted_data = sorted(list(data))
n, bins, patches = subplot.hist(
sorted_data,
bins=max_ordinal//66,
density=True,
histtype='stepfilled',
color=JLPT_COLORS[level_number])
plot.show()
def classify():
_make_build_dir()
print('Loading JMDict...')
jmdict = _load_jmdict()
print('Getting JLPT levels...')
jlpt_lists = _get_jlpt_lists(jmdict)
print('Getting Novel Word Frequencies...')
novel_word_frequencies = _get_cb4960_word_frequencies()
print('Plotting Novel JLPT histograms...')
plot_jlpt_list_densities(jlpt_lists, novel_word_frequencies)
#print('Getting Visual Novels Word Frequencies...')
#vn_word_frequencies = _get_vn_word_frequencies()
#print('Plotting Visual Novels JLPT histograms...')
#plot_jlpt_list_densities(jlpt_lists, vn_word_frequencies)
#print('Getting Narou Word Frequencies...')
#narou_word_frequencies = _get_narou_word_frequencies()
#print('Plotting Narou JLPT histograms...')
#plot_jlpt_list_densities(jlpt_lists, narou_word_frequencies)
# I did not find this Wikipedia word freq list useful.
#print('Getting Wikipedia Word Frequencies...')
#wikipedia_word_frequencies = _get_wikipedia_word_frequencies()
#print('Plotting Wikipedia JLPT histograms...')
#plot_jlpt_list_densities(jlpt_lists, wikipedia_word_frequencies)
if __name__ == '__main__':
classify()