-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_segment.py
More file actions
364 lines (251 loc) · 12.7 KB
/
Copy patheval_segment.py
File metadata and controls
364 lines (251 loc) · 12.7 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
eval_segment.py
Supplementary file for:
INFIXER: A METHOD FOR SEGMENTING NON-CONCATENATIVE MORPHOLOGY IN TAGALOG
by Steven Butler
Evaluation and segmentation classes to be used with trained Infixer models.
The InfixerEvaluation class utilizes some of Morfessor's built-in evaluation
tools in order to assess the quality of a particular trained model. It can take
a gold standard file as input and return it with the model's segmentations for
comparison.
"""
from __future__ import print_function
from __future__ import unicode_literals
import logging
import os.path
import tempfile
from morfessor.evaluation import EvaluationConfig, MorfessorEvaluation, FORMAT_STRINGS
from morfessor.io import MorfessorIO
from model import AffixFilter
_logger = logging.getLogger(__name__)
class InfixerEvaluation(object):
"""An object for evaluating modified Morfessor Baseline segmenters.
Public functions:
evaluate_model
return_evaluation
output_modified_gold_standard
"""
def __init__(self, morfessor_model, feature_dict, affix_list):
"""Initialize an evaluation object with a model, feature dict, and affix list.
:param morfessor_model: a trained Morfessor Baseline object
:param feature_dict: the output dictionary from ModelBuilder object
:param affix_list:
"""
# save input
self._model = morfessor_model
self._feature_dict = feature_dict
self._affix_filter = AffixFilter(affix_list)
# set up morfessor's IO class
self._io_manager = MorfessorIO()
def _update_compounds(self, word):
"""Return the appropriate form the word in an annotation file.
For words in the supplied feature dictionary (i.e., in vocabulary words),
the final transformed word form is returned if it exists, and otherwise
the same word is returned. If the word is OOV, it is filtered using the
supplied list of affixes and returned.
"""
# NOTE: leave "redundant parentheses" in return statements,
# they are to ensure the output is a tuple
# TODO: substitute 'word' in dict.get('final_word_base, word) with another thing to ensure it works correctly
if word in self._feature_dict:
_logger.debug("IN DICT: {} -> {}".format(word, self._feature_dict[word].get('final_word_base', word)))
return ("IV", self._feature_dict[word].get('final_word_base', word))
else:
_logger.debug("OOV: {} -> {}".format(word, self._affix_filter.filter_word(word)))
return ("OOV", self._affix_filter.filter_word(word))
def _read_annotations_file(self, file_name, construction_separator=' ', analysis_sep=','):
"""Convert annotation file to generator.
This is based off a method of the same name in the MorfessorIO class. It
was modified so that the compound (for our purposes, the word being
segmented) would undergo the same filtering as the training set, to ensure
continuity.
Each line in the segmentation file is expected to have the format:
<compound> <constr1> <constr2>... <constrN>, <constr1>...<constrN>, ...
"""
with open(file_name, 'r') as f:
file_data = f.read().split('\n')
annotation_list = [line for line in file_data if line != '']
annotations = {}
_logger.info(
"Reading gold standard annotations from '%s'..." % file_name)
for line in annotation_list:
compound, analyses_line = line.split(None, 1)
# apply filtering transformations if needed
compound_mod = self._update_compounds(compound)
if compound_mod not in annotations:
annotations[compound_mod] = []
if analysis_sep is not None:
for analysis in analyses_line.split(analysis_sep):
analysis = analysis.strip()
annotations[compound_mod].append(analysis.strip().split(construction_separator))
else:
annotations[compound_mod].append(analyses_line.split(construction_separator))
_logger.info("Done.")
return annotations
def evaluate_model(self, gold_standard_file, num_samples=10, sample_size=40):
"""Call the morfessor evaluator.
:param gold_standard_file: a file with words and gold standard segmentations
:param num_samples: the number of samples to be taken
:param sample_size: the size of the samples to be taken
"""
annotations = self._read_annotations_file(gold_standard_file)
_logger.info(annotations)
eval_obj = MorfessorEvaluation(annotations)
results = eval_obj.evaluate_model(self._model, configuration=EvaluationConfig(num_samples, sample_size))
print(results.format(FORMAT_STRINGS['default']))
def return_evaluation(self, gold_standard_file, num_samples=10, sample_size=20):
"""Call the morfessor evaluator.
:param gold_standard_file: a file with words and gold standard segmentations
:param num_samples: the number of samples to be taken
:param sample_size: the size of the samples to be taken
"""
annotations = self._read_annotations_file(gold_standard_file)
eval_obj = MorfessorEvaluation(annotations)
results = eval_obj.evaluate_model(self._model, configuration=EvaluationConfig(num_samples, sample_size))
return results
def _process_segment_file(self, infile):
"""Process a word file to be segmented to ensure compatibility with the model."""
with open(infile, 'r') as f:
data = f.read().split('\n')
data_filtered = []
for word in data:
data_filtered.append(self._update_compounds(word))
temp_dir = tempfile.TemporaryDirectory()
container_file = os.path.join(temp_dir.name, 'temp_file.txt')
with open(container_file, 'w') as f:
f.write('\n'.join(data_filtered))
_logger.debug('\n'.join(data_filtered))
return container_file
def _filter_input_wordlist(self, in_list):
"""Process a word file to be segmented to ensure compatibility with the model."""
data = in_list
data_filtered = []
for word in data:
# filter empty strings
if word == '':
continue
else:
vocab_status, word_filtered = self._update_compounds(word)
data_filtered.append((word, vocab_status, word_filtered))
return data_filtered
def _filter_input_list(self, infile):
"""Process a word file to be segmented to ensure compatibility with the model."""
with open(infile, 'r') as f:
data = f.read().split('\n')
data_filtered = []
for word in data:
# filter empty strings
if word == '':
continue
else:
vocab_status, word_filtered = self._update_compounds(word)
data_filtered.append((word, vocab_status, word_filtered))
return data_filtered
def _filter_gold_standard(self, infile):
"""Process a word file to be segmented to ensure compatibility with the model."""
with open(infile, 'r') as f:
data = f.readlines()
data_filtered = []
for line in data:
# split word and segmentation
word, gold_segmentation = line.split(None, 1)
# filter empty strings
if word == '':
continue
else:
vocab_status, word_filtered = self._update_compounds(word)
data_filtered.append((word, vocab_status, word_filtered, gold_segmentation.strip()))
return data_filtered
def output_modified_gold_standard(self, gs_infile, outfile):
"""Process a gold standard file and return it with model-supplied segmentations.
:param gs_infile: a gold standard file to be processed
:param outfile: the file where results should be written to
"""
words_filtered = self._filter_gold_standard(gs_infile)
with open(outfile, 'w') as f:
for word, vocab_status, word_filtered, gs_segments in words_filtered:
out_str = '{} {}\n'.format(word_filtered, gs_segments)
out_str.encode('utf-8')
f.write(out_str)
_logger.info("Modified gold standard written to {}".format(outfile))
def return_modified_gold_standard(self, gs_infile):
"""Process a gold standard file and return it with model-supplied segmentations.
:param gs_infile: a gold standard file to be processed
:param outfile: the file where results should be written to
"""
words_filtered = self._filter_gold_standard(gs_infile)
out_str = ""
for word, vocab_status, word_filtered, gs_segments in words_filtered:
out_str += '{} {}\n'.format(word_filtered, gs_segments)
out_str.encode('utf-8')
return out_str
class InfixerSegmenter(InfixerEvaluation):
"""An object that segments input using a trained Infixer model.
Public functions:
segment_word
segment_file
segment_gold_standard
"""
def __init__(self, morfessor_model, feature_dict, affix_list):
InfixerEvaluation.__init__(self, morfessor_model, feature_dict, affix_list)
def segment_word(self, word, separators=None):
"""Segment a given word using a trained morfessor model.
:param word: the input word string
:param separators: a list of punctuation used to separate words (for filtering)
"""
# set a default here to avoid issues with mutables in argument list
if separators is None:
separators = ['-']
const_separator = ' '
viterbi_smooth = 0
viterbi_max_len = 30
constructions, _ = self._model.viterbi_segment(word, viterbi_smooth, viterbi_max_len)
constructions_filtered = [item for item in constructions if item not in separators]
return const_separator.join(constructions_filtered)
def segment_list(self, in_list, separator=','):
"""Segment an input file and write to an outfile.
:param infile: an input file containing words to be segmented
:param outfile: a filename for writing output
:param separator: the separator for output lines, defaults to ',' mimicking csv
"""
words_filtered = self._filter_input_wordlist(in_list)
out_list = []
for word, vocab_status, word_filtered in words_filtered:
out_tuple = (word, self.segment_word(word_filtered))
out_list.append(out_tuple)
return out_list
def segment_file(self, infile, outfile, separator=','):
"""Segment an input file and write to an outfile.
:param infile: an input file containing words to be segmented
:param outfile: a filename for writing output
:param separator: the separator for output lines, defaults to ',' mimicking csv
"""
words_filtered = self._filter_input_list(infile)
with open(outfile, 'w') as f:
for word, vocab_status, word_filtered in words_filtered:
items = {'word': word,
'status': vocab_status,
'filtered': word_filtered,
'segments': self.segment_word(word_filtered),
'sep': separator}
out_str = '{word}{sep}{status}{sep}{filtered}{sep}{segments}\n'.format(**items)
f.write(out_str)
_logger.info('Segmentations written to {}'.format(outfile))
def segment_gold_standard(self, gs_infile, outfile, separator=','):
"""Segment an input gold standard file and write to an outfile.
:param gs_infile: a gold standard file to be processed
:param outfile: the file where results should be written to
:param separator: the separator for output lines, defaults to ',' mimicking csv
"""
words_filtered = self._filter_gold_standard(gs_infile)
with open(outfile, 'w') as f:
for word, vocab_status, word_filtered, gs_segments in words_filtered:
items = {'word': word,
'status': vocab_status,
'filtered': word_filtered,
'gs_segments': "GS: " + gs_segments,
'segments': self.segment_word(word_filtered),
'sep': separator}
out_str = '{word}{sep}{status}{sep}{filtered}{sep}{gs_segments}{sep}{segments}\n'.format(**items)
f.write(out_str)
_logger.info('Segmentations (gold standard) written to {}'.format(outfile))