-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (101 loc) · 3.46 KB
/
main.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
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 26 15:58:30 2020
@author: Usuari
"""
from tqdm import tqdm
from time import sleep
from os import path
from copy import deepcopy
from include.objects import Kanji, Hanzi, Container
from report.report import Report
from include.jap_support import *
path_kanji = path.join(path.curdir, 'kanji.txt')
path_hanzi = path.join(path.curdir, 'hanzi.txt')
path_output = path.join(path.curdir, 'output.txt')
kanji = Container(Kanji)
hanzi = Container(Hanzi)
# Import ##################################################
with open(path_kanji,'r',encoding='UTF-8') as f:
lines = f.readlines()
for line in lines:
if line[0] == '#': continue
kanji.append(Kanji(line))
with open(path_hanzi,'r',encoding='UTF-8') as f:
lines = f.readlines()
for line in lines:
if line[0] == '#': continue
hanzi.append(Hanzi(line))
del lines, line
###########################################################
print(f"Imported:\n{len(kanji)} Kanji\n{len(hanzi)} Hanzi")
print(f"Looking for matches...")
sleep(0.5)
# Compare #################################################
matches_kanji = Container(Kanji)
matches_hanzi = Container(Hanzi)
unmatch_kanji = Container(Kanji)
unmatch_hanzi = deepcopy(hanzi)
for k in iter(kanji):
kk = k.oldest()
for h in unmatch_hanzi:
hh = h.trad
if kk == hh:
matches_kanji.append(k)
matches_hanzi.append(h)
unmatch_hanzi.remove(h)
break
else:
unmatch_kanji.append(k)
###########################################################
print(f"{len(matches_kanji)} matches found!")
# Export ##################################################
with open(path_output,'w',encoding='UTF-8') as f:
f.write("#Common\tKanji\tSimpl.\tKun'yomi\tOn'yomi\tPinyin\n")
for (k, h) in zip(matches_kanji, matches_hanzi):
lineout = [k.oldest(), k.shin, h.simp, k.kunyomi, k.onyomi, h.pinyin]
f.write('\t'.join(lineout)+'\n')
###########################################################
print('Matches exported')
# Stats ###################################################
id_match_kanji = matches_kanji.sort('id')
id_match_hanzi = matches_hanzi.sort('id')
range_match = list(range(len(matches_kanji)))
normal_match = [el/len(matches_kanji)*100 for el in range_match]
import matplotlib.pyplot as plt
plt.figure()
plt.title('Cumulative percentage of matching')
plt.xlabel('Id')
plt.ylabel('%')
plt.plot(id_match_kanji, normal_match, label='Kanji')
plt.plot(id_match_hanzi, normal_match, label='Hanzi')
plt.legend()
plt.show()
# Reports #################################################
data = [ [
h.id,
k.oldest(),
k.shin,
h.simp,
k.kunyomi,
k.onyomi,
h.pinyin ] for (k, h) in zip(matches_kanji, matches_hanzi) ]
cols = ['Hanzi ID', 'Kyuu/Trad', 'Shinjitai', 'Simplified', 'Kun\'yomi', 'On\'yomi', 'Pinyin']
index = [k.id for k in matches_kanji]
Report(data, cols, index, 'reportKH')
data = [ [
k.oldest(),
k.shin,
k.kunyomi,
k.onyomi] for k in unmatch_kanji ]
cols = ['Kyuushitai', 'Shinjitai', 'Kun\'yomi', 'On\'yomi']
index = [k.id for k in unmatch_kanji]
Report(data, cols, index, 'reportK')
data = [ [
h.trad,
h.simp,
h.pinyin ] for h in unmatch_hanzi ]
cols = ['Traditional', 'Simplified', 'Pinyin']
index = [h.id for h in unmatch_hanzi]
Report(data, cols, index, 'reportH')
del data, cols, index, h, k