-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
disambiguate.py
203 lines (163 loc) · 6.97 KB
/
disambiguate.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import os
class Model:
disambiguate_verb_adj = True
@staticmethod
def from_name(name, kiwi_model_path=None, bareun_api_key=None):
if name == 'kiwi': return KiwiModel(kiwi_model_path, 'knlm')
if name == 'kiwi_sbg': return KiwiModel(kiwi_model_path, 'sbg')
if name == 'komoran': return KomoranModel()
if name == 'kkma': return KkmaModel()
if name == 'hannanum': return HannanumModel()
if name == 'mecab': return MecabModel()
if name == 'okt': return OktModel()
if name == 'khaiii': return KhaiiiModel()
if name == 'bareun': return BareunModel(bareun_api_key)
raise ValueError(f'Unknown model name: {name}')
def _convert(self, morph):
raise NotImplementedError()
def _tokenize(self, text):
raise NotImplementedError()
def tokenize(self, text):
return list(map(self._convert, self._tokenize(text)))
class KiwiModel(Model):
def __init__(self, model_path=None, model_type='knlm'):
import kiwipiepy
from kiwipiepy import Kiwi
print("Initialize kiwipiepy ({})".format(kiwipiepy.__version__), file=sys.stderr)
self._mdl = Kiwi(model_path=model_path, model_type=model_type)
def _convert(self, morph):
return morph.form, (morph.tag[:2] if morph.tag.startswith('V') else morph.tag[:1])
def _tokenize(self, text):
return self._mdl.tokenize(text)
class KomoranModel(Model):
def __init__(self):
import konlpy
from konlpy import tag
print("Initialize Komoran from konlpy ({})".format(konlpy.__version__), file=sys.stderr)
self._mdl = tag.Komoran()
def _convert(self, morph):
return morph[0], (morph[1][:2] if morph[1].startswith('V') else morph[1][:1])
def _tokenize(self, text):
return self._mdl.pos(text)
class KkmaModel(Model):
def __init__(self):
import konlpy
from konlpy import tag
print("Initialize Kkma from konlpy ({})".format(konlpy.__version__), file=sys.stderr)
self._mdl = tag.Kkma()
def _convert(self, morph):
return morph[0], (morph[1][:2] if morph[1].startswith('V') else morph[1][:1])
def _tokenize(self, text):
return self._mdl.pos(text)
class MecabModel(Model):
def __init__(self):
import konlpy
from konlpy import tag
print("Initialize Mecab from konlpy ({})".format(konlpy.__version__), file=sys.stderr)
self._mdl = tag.Mecab()
def _convert(self, morph):
return morph[0], (morph[1][:2] if morph[1].startswith('V') else morph[1][:1])
def _tokenize(self, text):
return self._mdl.pos(text, split_inflect=True)
class HannanumModel(Model):
disambiguate_verb_adj = False
def __init__(self):
import konlpy
from konlpy import tag
print("Initialize Hannanum from konlpy ({})".format(konlpy.__version__), file=sys.stderr)
self._mdl = tag.Hannanum()
def _convert(self, morph):
if morph[1] == 'P':
return morph[0], 'VV'
return morph[0], morph[1][:1]
def _tokenize(self, text):
return self._mdl.pos(text)
class OktModel(Model):
disambiguate_verb_adj = False
def __init__(self):
import konlpy
from konlpy import tag
print("Initialize Okt from konlpy ({})".format(konlpy.__version__), file=sys.stderr)
self._mdl = tag.Okt()
def _convert(self, morph):
if morph[1] == 'Verb':
return morph[0][:-1], 'VV'
return morph[0], morph[1][:1]
def _tokenize(self, text):
return self._mdl.pos(text, stem=True)
class KhaiiiModel(Model):
def __init__(self):
from khaiii import KhaiiiApi
self._mdl = KhaiiiApi()
print("Initialize khaiii ({})".format(self._mdl.version()), file=sys.stderr)
def _convert(self, morph):
form, tag = morph
return form, (tag[:2] if tag.startswith('V') else tag[:1])
def _tokenize(self, text):
return [(morph.lex, morph.tag) for word in self._mdl.analyze(text) for morph in word.morphs]
class BareunModel(Model):
def __init__(self, api_key, host='localhost', port=5656) -> None:
import bareunpy as brn
self._mdl = brn.Tagger(api_key, host, port)
print(f"Initialize Bareun from bareunpy (version={brn.version}, bareun_version={brn.bareun_version})", file=sys.stderr)
def _convert(self, morph):
form, tag = morph
return form, (tag[:2] if tag.startswith('V') else tag[:1])
def _tokenize(self, text):
return self._mdl.tag(text).pos()
def load_dataset(path):
ret = []
for line in open(path, encoding='utf-8'):
line = line.rstrip()
if not line: continue
try:
answer, exam = line.split('\t')
except:
print(f'Error at {path}: {line}', file=sys.stderr)
continue
form, tag = answer.split('/')
if tag.startswith('V'): tag = tag[:2]
else: tag = tag[:1]
ret.append(((form, tag), exam))
return ret
def evaluate(dataset, model, error_output=None, print_all_results=False):
acc, tot = 0, 0
for answer, exam in dataset:
if answer[1] == 'VA' and not model.disambiguate_verb_adj:
return None
result = model.tokenize(exam)
correct = answer in set(result)
acc += int(correct)
tot += 1
if (print_all_results or not correct) and error_output is not None:
print('/'.join(answer), ':', *map('/'.join, result), file=error_output)
return acc / tot
def main(args):
model_names = args.target.split(',')
models = [Model.from_name(n, kiwi_model_path=args.kiwi_model_path, bareun_api_key=args.bareun_api_key) for n in model_names]
if args.error_output_dir:
os.makedirs(args.error_output_dir, exist_ok=True)
error_outputs = [open(args.error_output_dir + '/' + name + '.error.txt', 'w', encoding='utf-8') for name in model_names]
else:
error_outputs = None
print('', *model_names, sep='\t')
for dataset in args.datasets:
ds = load_dataset(dataset)
scores = []
for i, model in enumerate(models):
acc = evaluate(ds, model, error_output=(error_outputs[i] if error_outputs else None), print_all_results=args.print_all_results)
scores.append(acc)
print(os.path.basename(dataset), *((f'{s:.3f}' if s is not None else '-') for s in scores), sep='\t')
if error_outputs:
for f in error_outputs: f.close()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('datasets', nargs='+')
parser.add_argument('--target', default='kiwi', help='kiwi,kiwi_sbg,komoran,mecab,kkma,hannanum,okt,khaiii,bareun')
parser.add_argument('--error_output_dir')
parser.add_argument('--print_all_results', default=False, action='store_true')
parser.add_argument('--kiwi_model_path')
parser.add_argument('--bareun_api_key')
main(parser.parse_args())