-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhownet_generate.py
47 lines (39 loc) · 1.49 KB
/
hownet_generate.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
import pickle
def read_dataset(eval_path):
difficult_words = []
pos_tags = []
with open(eval_path, 'r', encoding='utf-8') as reader:
while True:
line = reader.readline()
if not line:
break
row = line.strip().split('\t')
difficult_word, pos_tag = row[1], row[2]
difficult_words.append(difficult_word)
pos_tags.append(pos_tag)
return difficult_words, pos_tags
def save_res(res, output_path):
with open(output_path, 'a', encoding='utf-8') as f:
f.write(' '.join(res) + '\n')
def main():
EVAL_PATH = './dataset/annotation_data.csv'
OUTPUT_PATH = './data/hownet_output.csv'
eval_path = EVAL_PATH
output_path = OUTPUT_PATH
difficult_words, pos_tags = read_dataset(eval_path)
with open('./hownet/word_candidates_decoded.pkl','rb') as fp:
word_candidates = pickle.load(fp)
for difficult_word, pos_tag in zip(difficult_words, pos_tags):
try:
res = word_candidates[difficult_word].get('noun')
res.extend(word_candidates[difficult_word].get('verb'))
res.extend(word_candidates[difficult_word].get('adj'))
res.extend(word_candidates[difficult_word].get('adv'))
res = [word for word in res if len(word) <= len(difficult_word)]
except:
res = ['NULL']
if len(res)==0:
res.append('NULL')
save_res(res, output_path)
if __name__ == '__main__':
main()