morfeusz_builder
16.5 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
365
366
367
368
369
370
371
372
373
374
#! /usr/bin/python3
# -*- coding:utf-8 -*-
'''
Created on 21 paź 2013
@author: mlenart
'''
import os
import sys
import logging
import codecs
from morfeuszbuilder.fsa import encode
from morfeuszbuilder.fsa import convertinput
from morfeuszbuilder.fsa.fsa import FSA
from morfeuszbuilder.fsa.serializer import Serializer, SerializationMethod
from morfeuszbuilder.tagset.tagset import Tagset
from morfeuszbuilder.segrules import rulesParser
from morfeuszbuilder.utils import exceptions, limits
from optparse import OptionParser
def _checkOption(opt, parser, msg):
if opt is None:
print(msg, file=sys.stderr)
parser.print_help()
exit(1)
def _checkCondition(cond, parser, msg):
if not cond:
print(msg, file=sys.stderr)
parser.print_help()
exit(1)
def _parseListCallback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
def _checkOpen(filename, mode):
try:
with open(filename, mode) as _:
pass
if 'w' in mode:
os.remove(filename)
except IOError as ex:
print(str(ex), file=sys.stderr)
exit(1)
def _getDictFilename(opts, isGenerator):
typeCode = 's' if isGenerator else 'a'
fname = '%s-%s.dict' % (opts.dictName, typeCode)
return os.path.join(opts.dictDir, fname)
def _parseOptions():
"""
Parses commandline args
"""
parser = OptionParser()
parser.add_option('--input-files',
type='string',
dest='inputFiles',
action='callback',
callback=_parseListCallback,
metavar='FILES',
help='comma separated list of dictionary files')
parser.add_option('--tagset-file',
dest='tagsetFile',
metavar='FILE',
help='path to the file with tagset')
parser.add_option('--segments-file',
dest='segmentsFile',
metavar='FILE',
help='path to the file with segment rules')
#~ parser.add_option('--trim-supneg',
#~ dest='trimSupneg',
#~ default=False,
#~ action='store_true',
#~ help='this option is ignored and exists only for backwards compatibility')
parser.add_option('--dict',
dest='dictName',
help='the name of result dictionary')
parser.add_option('--dict-dir',
dest='dictDir',
metavar='FILE',
default=os.getcwd(),
help='path to output directory (the default is current dir)')
parser.add_option('--only-analyzer',
dest='onlyAnalyzer',
action='store_true',
default=False,
help='Generate dictionary for morphological analysis only (default is both analysis and synthesis)')
parser.add_option('--only-generator',
dest='onlyGenerator',
action='store_true',
default=False,
help='Generate dictionary for morphological synthesis only (default is both analysis and synthesis)')
parser.add_option('--analyzer-cpp',
dest='analyzerCpp',
metavar='FILE',
help='Encode analyzer dictionary data in given c++ file')
parser.add_option('--generator-cpp',
dest='generatorCpp',
metavar='FILE',
help='Encode generator dictionary data in given c++ file')
#~ parser.add_option('--use-arrays',
#~ dest='useArrays',
#~ action='store_true',
#~ default=False,
#~ help='store states reachable by 2 transitions in arrays (should speed up recognition, available only when --serialization-method=V1)')
parser.add_option('--serialization-method',
dest='serializationMethod',
default='V1',
help="FSA serialization method: \
SIMPLE - fixed-length transitions, fastest and weakest compression \
V1 - variable-length transitions, compressed labels - strongest compression \
V2 - format similar to the default in Jan Daciuk's fsa package - variable-length transitions, non-compressed labels - good compression, good speed")
#~ parser.add_option('--visualize',
#~ dest='visualize',
#~ action='store_true',
#~ default=False,
#~ help='visualize result')
parser.add_option('--analyzer-train-file',
dest='analyzerTrainFile',
help='A text file used for analyzer training. Should contain words from some large corpus - one word in each line. Resulting analysis automaton should be faster with proper train file.')
parser.add_option('--generator-train-file',
dest='generatorTrainFile',
help='A text file used for generator training. Should contain words from some large corpus - one word in each line. Resulting synthesis automaton should be faster with proper train file.')
parser.add_option('--debug',
dest='debug',
action='store_true',
default=False,
help='output some debugging info')
#~ parser.add_option('--profile',
#~ dest='profile',
#~ action='store_true',
#~ default=False,
#~ help='show profiling graph (required pycallgraph and graphviz')
opts, args = parser.parse_args()
_checkOption(opts.inputFiles, parser, "Input file is missing")
_checkOption(opts.dictDir, parser, "Output dictionary dir is missing")
_checkCondition((opts.onlyAnalyzer, opts.onlyGenerator) != (True, True),
parser, 'Cannot set both --only-analyzer and --only-generator')
writeCpp = {opts.analyzerCpp, opts.generatorCpp} != {None}
_checkCondition(opts.dictName or writeCpp, parser, "Dictionary name is missing")
_checkCondition(opts.onlyGenerator or opts.analyzerCpp or not writeCpp, parser, "Analyzer .cpp output file path is missing")
_checkCondition(opts.onlyAnalyzer or opts.generatorCpp or not writeCpp, parser, "Generator .cpp output file path is missing")
#~ _checkCondition((opts.dictName, opts.outputCpp) != (None, None),
#~ parser, 'Must set at least one of: --dict-name, --output-cpp')
#~ _checkOption(opts.outputFile, parser, "Output file is missing")
_checkOption(opts.tagsetFile, parser, "Tagset file is missing")
_checkOption(opts.segmentsFile, parser, "Segmentation file is missing")
#~ _checkOption(opts.serializationMethod, parser, "Serialization method file is missing")
#~ _checkExactlyOneOptionSet([opts.analyzer, opts.generator],
#~ parser, 'Must set exactly one FSA type: --analyzer or --generator')
_checkOpen(opts.tagsetFile, 'r')
_checkOpen(opts.segmentsFile, 'r')
for filename in opts.inputFiles:
_checkOpen(filename, 'r')
if not opts.onlyGenerator:
_checkOpen(_getDictFilename(opts, isGenerator=False), 'w')
if not opts.onlyAnalyzer:
_checkOpen(_getDictFilename(opts, isGenerator=True), 'w')
if not opts.serializationMethod.upper() in [SerializationMethod.SIMPLE, SerializationMethod.V1]:
print('--serialization-method must be one of ('+str([SerializationMethod.SIMPLE, SerializationMethod.V1])+')', file=sys.stderr)
parser.print_help()
exit(1)
return opts
def _concatFiles(inputFiles):
for inputFile in inputFiles:
if inputFile:
with open(inputFile, 'r') as f:
for line in f:
yield line
def _readDictIdAndCopyright(inputFiles):
dictId = None
copyright = None
for inputFile in inputFiles:
if inputFile:
with codecs.open(inputFile, 'r', 'utf8') as f:
inCopyright = False
for linenum, line in enumerate(f, start=1):
if dictId is None and line.startswith('#!DICT-ID'):
dictIdTag, _, dictId = line.strip().partition(' ')
exceptions.validate(
dictIdTag == '#!DICT-ID',
'Dictionary ID tag must be followed by a space character and dictionary ID string')
exceptions.validate(
len(line.split(' ')) > 1,
'%s:%d: Must provide DICT-ID' % (inputFile, linenum))
exceptions.validate(
len(line.split(' ')) == 2,
'%s:%d: DICT-ID must not contain spaces' % (inputFile, linenum))
elif copyright is None and line.startswith('#<COPYRIGHT>'):
exceptions.validate(
line.strip() == '#<COPYRIGHT>',
'%s:%d: Copyright start tag must be the only one in the line' % (inputFile, linenum))
inCopyright = True
copyright = ''
elif line.startswith('#</COPYRIGHT>'):
exceptions.validate(
inCopyright,
'%s:%d: Copyright end tag must be preceded by copyright start tag' % (inputFile, linenum))
exceptions.validate(
line.strip() == '#</COPYRIGHT>',
'%s:%d: Copyright end tag must be the only one in the line' % (inputFile, linenum))
inCopyright = False
elif inCopyright:
copyright += line
if dictId is None:
logging.warn('No dictionary ID tag found')
dictId = ''
if copyright is None:
logging.warn('No copyright info found')
copyright = ''
return (dictId, copyright)
def _readNamesAndQualifiers(inputFiles):
names = set([''])
qualifiers = set([frozenset()])
lineParser = convertinput.LineParser()
for line in _concatFiles(inputFiles):
line = line.strip()
if not lineParser.ignoreLine(line):
_, _, _, name, qualifier = lineParser.parseLine(line)
names.add(name)
qualifiers.add(convertinput.parseQualifiers(qualifier))
namesMap = dict([(name, idx) for idx, name in enumerate(sorted(list(names)))])
qualifiersMap = dict([(quals, idx) for idx, quals in enumerate(sorted(qualifiers, key=lambda q: tuple(sorted(q))))])
exceptions.validate(
len(qualifiersMap) <= limits.MAX_QUALIFIERS_COMBINATIONS,
'Too many qualifiers combinations. The limit is %d' % limits.MAX_QUALIFIERS_COMBINATIONS)
return namesMap, qualifiersMap
def _readPolimorfInput4Analyzer(inputFiles, tagset, namesMap, qualifiersMap, encoder, segmentRulesManager):
logging.info('reading analyzer data from %s', str(inputFiles))
for entry in convertinput.PolimorfConverter4Analyzer(tagset, namesMap, qualifiersMap, encoder, 'utf8', segmentRulesManager).convert(_concatFiles(inputFiles)):
yield entry
def _readPolimorfInput4Generator(inputFiles, tagset, namesMap, qualifiersMap, encoder, segmentRulesManager):
logging.info('reading generator data from %s', str(inputFiles))
for entry in convertinput.PolimorfConverter4Generator(tagset, namesMap, qualifiersMap, encoder, 'utf8', segmentRulesManager).convert(_concatFiles(inputFiles)):
yield entry
def _readTrainData(trainFile):
with codecs.open(trainFile, 'r', 'utf8') as f:
for line in f:
yield line.strip()
def _printStats(fsa):
acceptingNum = 0
sinkNum = 0
arrayNum = 0
for s in fsa.dfs():
if s.isAccepting():
acceptingNum += 1
if s.transitionsNum == 0:
sinkNum += 1
if s.serializeAsArray:
arrayNum += 1
logging.info('states num: '+str(fsa.getStatesNum()))
logging.info('transitions num: '+str(fsa.getTransitionsNum()))
logging.info('accepting states num: '+str(acceptingNum))
logging.info('sink states num: '+str(sinkNum))
logging.info('array states num: '+str(arrayNum))
def buildAnalyzerFromPoliMorf(inputFiles, tagset, namesMap, qualifiersMap, segmentRulesManager):
encoder = encode.MorphEncoder()
fsa = FSA(encoder, tagset)
for word, data in _readPolimorfInput4Analyzer(inputFiles, tagset, namesMap, qualifiersMap, encoder, segmentRulesManager):
# print word, data
fsa.addEntry(word, data)
del word
del data
fsa.close()
logging.info('------')
logging.info('Analyzer FSA stats:')
logging.info('------')
_printStats(fsa)
return fsa
def buildGeneratorFromPoliMorf(inputFiles, tagset, namesMap, qualifiersMap, segmentRulesManager):
encoder = encode.Encoder4Generator()
fsa = FSA(encoder, tagset)
inputData = _readPolimorfInput4Generator(inputFiles, tagset, namesMap, qualifiersMap, encoder, segmentRulesManager)
for word, data in inputData:
fsa.addEntry(word, data)
fsa.close()
logging.info('------')
logging.info('Generator FSA stats:')
logging.info('------')
_printStats(fsa)
return fsa
def _doBuildDictionaryPart(opts, dictId, copyrightTxt, tagset, namesMap, qualifiersMap, isGenerator):
logging.info('reading segmentation rules')
rulesParserVersion = rulesParser.RulesParser.PARSE4ANALYZER if not isGenerator else rulesParser.RulesParser.PARSE4GENERATOR
segmentRulesManager = rulesParser.RulesParser(tagset, namesMap, qualifiersMap, rulesParserVersion).parse(opts.segmentsFile)
segmentationRulesData = segmentRulesManager.serialize()
logging.info('done reading segmentation rules')
logging.info('building automaton')
buildFunction = buildAnalyzerFromPoliMorf if not isGenerator else buildGeneratorFromPoliMorf
fsa = buildFunction(opts.inputFiles, tagset, namesMap, qualifiersMap, segmentRulesManager)
logging.info('done building automaton')
if not isGenerator and opts.analyzerTrainFile:
logging.info('training analyzer automaton with '+opts.analyzerTrainFile+' ...')
fsa.train(_readTrainData(opts.analyzerTrainFile))
logging.info('done training')
if isGenerator and opts.generatorTrainFile:
logging.info('training generator automaton with '+opts.generatorTrainFile+' ...')
fsa.train(_readTrainData(opts.analyzerTrainFile))
logging.info('done training')
serializer = Serializer.getSerializer(opts.serializationMethod, fsa, dictId, copyrightTxt, tagset, namesMap, qualifiersMap, segmentationRulesData)
if opts.generatorCpp and isGenerator:
serializer.serialize2CppFile(opts.generatorCpp, isGenerator=isGenerator)
if opts.analyzerCpp and not isGenerator:
serializer.serialize2CppFile(opts.analyzerCpp, isGenerator=isGenerator)
if opts.dictDir:
serializer.serialize2BinaryFile(_getDictFilename(opts, isGenerator=isGenerator), isGenerator=isGenerator)
logging.info('total FSA size (in bytes): '+str(fsa.initialState.reverseOffset))
def main(opts):
if opts.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logging.info('reading tagset')
tagset = Tagset(opts.tagsetFile)
logging.info('done reading tagset')
logging.info('reading names and qualifiers')
dictId, copyrightTxt = _readDictIdAndCopyright(opts.inputFiles)
namesMap, qualifiersMap = _readNamesAndQualifiers(opts.inputFiles)
logging.info('done reading names and qualifiers')
if not opts.onlyGenerator:
_doBuildDictionaryPart(opts, dictId, copyrightTxt, tagset, namesMap, qualifiersMap, isGenerator=False)
if not opts.onlyAnalyzer:
_doBuildDictionaryPart(opts, dictId, copyrightTxt, tagset, namesMap, qualifiersMap, isGenerator=True)
if __name__ == '__main__':
import os
opts = _parseOptions()
#~ try:
main(opts)
#~ except Exception as ex:
#~ print >> sys.stderr, u'Building dictionary file failed:', unicode(ex).encode('utf8'), 'type of error:', type(ex)
#~ sys.exit(1)
#~ finally:
#~ pass