-
Notifications
You must be signed in to change notification settings - Fork 10
/
hsk.py
159 lines (124 loc) · 3.81 KB
/
hsk.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
151
152
153
154
155
156
157
158
159
import re
import pickle as p
# load vocabulary data
with open('./dict/vocab_info.data', 'rb') as f:
vocab_data = p.load(f)
hsk_ambiguous = vocab_data['hsk_ambiguous']
hsk_5000 = vocab_data['hsk_5000']
hsk_all = vocab_data['hsk_all']
chars = vocab_data['chars']
jzcz = vocab_data['jzcz']
def diambiguate_level(word, pos, posseg, loc):
if word == '得':
if pos == 'u':
return 'hsk2'
else:
return 'hsk4' # v, d
if word == '等':
if pos == 'v':
return 'hsk2'
else:
return 'hsk4' # u
if word == '地':
if pos == 'u':
return 'hsk3'
else:
return 'hsk3_减字' # 天地
if word == '过':
if pos == 'u':
return 'hsk2'
else:
return 'hsk3' # v
if word == '还':
if pos == 'd':
return 'hsk2'
else:
return 'hsk3' # v
if word == '喂':
w1, p1 = posseg[loc + 1].split('/')
if w1 in [',', ',', '?', '?', '……', '。'] or p1 == 'w':
return 'hsk1'
else:
return 'hsk6'
if word == '长':
if pos == 'a':
return 'hsk2'
else:
return 'hsk3' # v
if word == '只':
if pos == 'q':
return 'hsk2'
else:
return 'hsk3' # d
if word == '种':
if pos == 'q':
return 'hsk3'
else:
return 'hsk6_减字' # v
def isWordChinese(word):
for char in word:
if char < '\u4e00' or char > '\u9fff':
return False
return True
def returnHSKlevel(word, pos, wplist, loc):
if word in hsk_5000:
return hsk_5000[word]
elif word in hsk_ambiguous:
return diambiguate_level(word, pos, wplist, loc)
def isJianzici(word):
for l, words in hsk_all.items():
for w in words:
if word in w:
return 'hsk' + str(l) + '_减字*' # 默认'None'
def isChongzuci(word):
char_levels = [0] * len(word)
for i, c in enumerate(word):
for l, charlist in chars.items():
if c in charlist:
char_levels[i] = l
if min(char_levels) > 0:
chongzu_level = max(char_levels)
all_levels = [str(i) for i in char_levels]
return 'hsk' + str(max(char_levels)) + '_重组*'
def level_analyze(sent, wordlist, wplist, current_level=0):
level_dict = {k: 0 for k in range(1, 8)}
for i, word in enumerate(wordlist):
level = '默认_超纲'
wp = wplist[i]
if '//w' in wp:
continue
pos = wp.split('/')[1]
if len(wordlist) == 1 and pos == 'nr':
pos = 'n'
if pos in ['w', 'nr', 'ns', 'Ng'] or not isWordChinese(word):
continue
if re.search('[a-zA-Z1-9]', word):
word = re.sub('[a-zA-Z1-9]', '', word)
if word in hsk_5000:
level = hsk_5000[word]
elif word in hsk_ambiguous:
level = diambiguate_level(word, pos, wplist, i)
elif word in jzcz:
level = jzcz[word]
if level == '默认_超纲':
if len(word) < 3:
t1 = isJianzici(word)
if t1:
level = t1
else:
t2 = isChongzuci(word)
if t2:
level = t2
else:
t2 = isChongzuci(word)
if t2:
level = t2
# word = word + '(' + level + ')' # for log
if 'hsk' in level and '_' in level:
word = word + '(' + level.split('_')[1] + ')'
if level.startswith('hsk'):
init_level = int(level[3])
level_dict[init_level] += 1
if '超纲' in level:
level_dict[7] += 1
return level_dict