MorfeuszInternal.cpp
19 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* File: Morfeusz.cpp
* Author: mlenart
*
* Created on November 13, 2013, 5:21 PM
*/
#include <string>
#include <iostream>
#include <vector>
#include "fsa/fsa.hpp"
#include "utils.hpp"
#include "data/default_fsa.hpp"
#include "MorfeuszInternal.hpp"
#include "deserialization/morphInterps/InterpretedChunksDecoder.hpp"
#include "charset/CharsetConverter.hpp"
#include "charset/charset_utils.hpp"
#include "case/CaseConverter.hpp"
#include "segrules/segrules.hpp"
#include "const.hpp"
#include "charset/utf8.h"
// TODO - konstruktor kopiujący działający Tak-Jak-Trzeba
using namespace std;
namespace morfeusz {
static MorfeuszOptions createDefaultOptions() {
MorfeuszOptions res;
res.caseHandling = WEAK;
res.encoding = UTF8;
res.tokenNumbering = SEPARATE;
res.whitespaceHandling = SKIP;
res.debug = false;
return res;
}
static void doShiftOrth(InterpretedChunk& from, InterpretedChunk& to) {
to.prefixChunks.push_back(from);
to.textStartPtr = from.textStartPtr;
from.orthWasShifted = true;
}
static string debugInterpsGroup(unsigned char type, const char* startPtr, const char* endPtr) {
stringstream res;
res << "(" << (int) type << ", " << string(startPtr, endPtr) << "), ";
return res.str();
}
static string debugAccum(vector<InterpretedChunk>& accum) {
stringstream res;
for (unsigned int i = 0; i < accum.size(); i++) {
res << debugInterpsGroup(accum[i].segmentType, accum[i].textStartPtr, accum[i].textEndPtr);
// res << "(" << (int) accum[i].interpsGroup.type << ", " << string(accum[i].chunkStartPtr, accum[i].chunkStartPtr) << "), ";
}
return res.str();
}
static void feedStateDirectly(
const FSAType& fsa,
StateType& state,
const char* inputStart,
const char* inputEnd) {
const char* currInput = inputStart;
while (currInput != inputEnd && !state.isSink()) {
state.proceedToNext(fsa, *currInput++);
}
}
static void feedStateIndirectly(
const FSAType& fsa,
StateType& state,
uint32_t codepoint) {
std::string chars;
UTF8CharsetConverter::getInstance().append(codepoint, chars);
for (unsigned int i = 0; i < chars.length() && !state.isSink(); i++) {
state.proceedToNext(fsa, chars[i]);
}
}
static void feedState(
const Environment& env,
StateType& state,
TextReader& reader) {
if (reader.peek() == reader.normalizedPeek() && &env.getCharsetConverter() == &UTF8CharsetConverter::getInstance()) {
feedStateDirectly(env.getFSA(), state, reader.getCurrPtr(), reader.getNextPtr());
}
else {
feedStateIndirectly(env.getFSA(), state, reader.normalizedPeek());
}
}
static InterpretedChunk createChunk(
const InterpsGroup& ig,
const TextReader& reader,
bool shiftOrth,
const string& homonymId) {
const unsigned char* interpsEndPtr = ig.ptr + ig.size;
InterpretedChunk ic;
ic.segmentType = ig.type;
ic.textStartPtr = reader.getWordStartPtr();
ic.textEndPtr = homonymId.empty() ? reader.getCurrPtr() : reader.getCurrPtr() - homonymId.length() - 1;
ic.interpsGroupPtr = ig.ptr;
ic.interpsEndPtr = interpsEndPtr;
ic.shiftOrth = shiftOrth;
ic.orthWasShifted = false;
ic.requiredHomonymId = homonymId;
ic.codepointsNum = reader.getCodepointsRead();
ic.forceIgnoreCase = false;
return ic;
}
MorfeuszInternal::MorfeuszInternal()
: analyzerEnv(DEFAULT_MORFEUSZ_CHARSET, ANALYZER, DEFAULT_FSA),
generatorEnv(DEFAULT_MORFEUSZ_CHARSET, GENERATOR, DEFAULT_SYNTH_FSA),
options(createDefaultOptions()),
accum(),
notMatchingCaseSegs(0),
graph(),
nextNodeNum(0) {
analyzerEnv.setCaseSensitive(options.caseHandling != IGNORE);
generatorEnv.setCaseSensitive(false);
}
void MorfeuszInternal::setAnalyzerDictionary(const string& filename) {
this->analyzerEnv.setDictionaryFile(filename);
}
void MorfeuszInternal::setGeneratorDictionary(const string& filename) {
this->generatorEnv.setDictionaryFile(filename);
}
MorfeuszInternal::~MorfeuszInternal() {
}
const char* getWordEndPtr(const TextReader& reader, const Environment& env) {
TextReader tmpReader(reader.getCurrPtr(), reader.getEndPtr(), env);
while (!tmpReader.isAtEnd() && !tmpReader.isAtWhitespace()) {
tmpReader.next();
}
return tmpReader.getCurrPtr();
}
bool MorfeuszInternal::handleWhitespacesAtBeginning(
const Environment& env,
TextReader& reader,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
if (env.getProcessorType() == ANALYZER
&& options.whitespaceHandling == KEEP) {
if (reader.isAtWhitespace() && !reader.isAtEnd()) {
processWhitespacesChunk(reader, startNodeNum, results);
return true;
}
}
else {
reader.skipWhitespaces();
}
return false;
}
void MorfeuszInternal::handleWhitespacesAtEnd(
const Environment& env,
TextReader& reader) const {
if (env.getProcessorType() == ANALYZER
&& options.whitespaceHandling == APPEND) {
reader.skipWhitespaces();
}
}
void MorfeuszInternal::processOneWord(
const Environment& env,
TextReader& reader,
int startNodeNum,
vector<MorphInterpretation>& results,
bool insideIgnHandler) const {
if (handleWhitespacesAtBeginning(env, reader, startNodeNum, results)) {
startNodeNum = results.back().getEndNode();
}
if (reader.isAtEnd()) {
return;
}
accum.resize(0);
notMatchingCaseSegs = 0;
graph.clear();
const SegrulesFSA& segrulesFSA = env.getCurrentSegrulesFSA();
reader.markWordStartsHere();
doProcessOneWord(env, reader, segrulesFSA.initialState);
while (reader.isInsideAWord()) {
reader.next();
}
const char* endOfWordPtr = reader.getCurrPtr();
handleWhitespacesAtEnd(env, reader);
if (!graph.empty()) {
const InterpretedChunksDecoder& interpretedChunksDecoder = env.getInterpretedChunksDecoder();
int srcNode = startNodeNum;
const std::vector< std::vector<InflexionGraph::Edge> >& theGraph = graph.getTheGraph();
size_t initialResultsSize = results.size();
for (unsigned int i = 0; i < theGraph.size(); i++) {
const vector<InflexionGraph::Edge>& edges = theGraph[i];
for (unsigned int j = 0; j < edges.size(); j++) {
const InflexionGraph::Edge& e = edges[j];
unsigned int targetNode = startNodeNum + e.nextNode;
InterpretedChunk ic = e.chunk;
ic.chunkEndPtr = (ic.textEndPtr == endOfWordPtr)
? reader.getCurrPtr()
: ic.textEndPtr;
interpretedChunksDecoder.decode(srcNode, targetNode, ic, results);
}
srcNode++;
}
if (results.size() == initialResultsSize) {
this->appendIgnotiumToResults(env, string(reader.getWordStartPtr(), reader.getCurrPtr()), startNodeNum, results);
}
}
else if (env.getProcessorType() == ANALYZER
&& !insideIgnHandler) {
this->handleIgnChunk(env, reader.getWordStartPtr(), reader.getCurrPtr(), startNodeNum, results);
}
else {
this->appendIgnotiumToResults(env, string(reader.getWordStartPtr(), reader.getCurrPtr()), startNodeNum, results);
}
}
void MorfeuszInternal::doProcessOneWord(
const Environment& env,
TextReader& reader,
const SegrulesState& segrulesState) const {
if (this->options.debug) {
cerr << "----------" << endl;
cerr << "PROCESS: '" << reader.getCurrPtr() << "', already recognized: " << debugAccum(accum) << endl;
}
StateType state = env.getFSA().getInitialState();
string homonymId;
vector<SegrulesState> newSegrulesStates;
while (!reader.isAtWhitespace()) {
feedState(env, state, reader);
reader.next();
if (state.isSink()) {
return;
}
if (env.getProcessorType() == GENERATOR && reader.getCurrPtr() != reader.getEndPtr() && reader.peek() == (uint32_t) HOMONYM_SEPARATOR) {
homonymId = env.getCharsetConverter().fromUTF8(string(reader.getCurrPtr() + 1, reader.getEndPtr()));
reader.proceedToEnd();
}
if (state.isAccepting()) {
InterpsGroupsReader& igReader = const_cast<InterpsGroupsReader&> (state.getValue());
while (igReader.hasNext()) {
processInterpsGroup(env, reader, reader.isAtWhitespace(), segrulesState, homonymId, igReader.getNext(), newSegrulesStates);
}
}
}
}
void MorfeuszInternal::processInterpsGroup(
const Environment& env,
const TextReader& reader,
bool isAtWhitespace,
const SegrulesState& segrulesState,
const string& homonymId,
const InterpsGroup& ig,
vector<SegrulesState>& newSegrulesStates) const {
bool caseMatches = env.getCasePatternHelper().checkInterpsGroupOrthCasePatterns(env, reader.getWordStartPtr(), reader.getCurrPtr(), ig);
if (caseMatches || options.caseHandling == WEAK) {
env.getCurrentSegrulesFSA().proceedToNext(ig.type, segrulesState, isAtWhitespace, newSegrulesStates);
if (!newSegrulesStates.empty()) {
for (unsigned int i = 0; i < newSegrulesStates.size(); i++) {
const SegrulesState& newSegrulesState = newSegrulesStates[i];
InterpretedChunk ic(
createChunk(ig, reader, newSegrulesState.shiftOrthFromPrevious, homonymId));
processInterpretedChunk(
env,
reader,
isAtWhitespace,
caseMatches,
newSegrulesState,
ic);
}
newSegrulesStates.resize(0);
}
else if (this->options.debug) {
std::cerr << "NOT ACCEPTING (segmentation)" << debugAccum(accum) << debugInterpsGroup(ig.type, reader.getWordStartPtr(), reader.getCurrPtr()) << std::endl;
}
}
else if (this->options.debug) {
std::cerr << "NOT ACCEPTING (case)" << debugAccum(accum) << debugInterpsGroup(ig.type, reader.getWordStartPtr(), reader.getCurrPtr()) << std::endl;
}
}
void MorfeuszInternal::processInterpretedChunk(
const Environment& env,
const TextReader& reader,
bool isAtWhitespace,
bool caseMatches,
const SegrulesState& newSegrulesState,
InterpretedChunk& ic) const {
if (!accum.empty() && accum.back().shiftOrth) {
doShiftOrth(accum.back(), ic);
}
if (!caseMatches && options.caseHandling == WEAK) {
notMatchingCaseSegs++;
ic.forceIgnoreCase = true;
}
accum.push_back(ic);
if (isAtWhitespace) {
assert(newSegrulesState.accepting);
if (this->options.debug) {
cerr << "ACCEPTING " << debugAccum(accum) << endl;
}
graph.addPath(accum, newSegrulesState.weak || notMatchingCaseSegs > 0);
}
else {
assert(!newSegrulesState.sink);
TextReader newReader(reader.getCurrPtr(), reader.getEndPtr(), env);
doProcessOneWord(env, newReader, newSegrulesState);
}
accum.pop_back();
if (!caseMatches && options.caseHandling == WEAK) {
notMatchingCaseSegs--;
}
}
void MorfeuszInternal::processWhitespacesChunk(
TextReader& reader,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
string orth(reader.readWhitespacesChunk());
MorphInterpretation mi(MorphInterpretation::createWhitespace(startNodeNum, startNodeNum + 1, orth, this->getDefaultAnalyzerTagset()));
results.push_back(mi);
}
void MorfeuszInternal::handleIgnChunk(
const Environment& env,
const char* inputStart,
const char* inputEnd,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
const char* currInput = inputStart;
const char* prevInput;
uint32_t codepoint = 0x00;
bool separatorFound = false;
while (currInput != inputEnd) {
prevInput = currInput;
const char* nonSeparatorInputEnd = prevInput;
do {
codepoint = env.getCharsetConverter().next(currInput, inputEnd);
if (!env.isSeparator(codepoint)) {
nonSeparatorInputEnd = currInput;
}
}
while (currInput != inputEnd && !env.isSeparator(codepoint));
if (env.isSeparator(codepoint)) {
separatorFound = true;
if (nonSeparatorInputEnd != prevInput) {
int startNode = results.empty() ? startNodeNum : results.back().getEndNode();
TextReader newReader1(prevInput, nonSeparatorInputEnd, env);
notMatchingCaseSegs = 0;
this->processOneWord(env, newReader1, startNode, results, true);
startNode = results.empty() ? startNodeNum : results.back().getEndNode();
TextReader newReader2(nonSeparatorInputEnd, currInput, env);
this->processOneWord(env, newReader2, startNode, results, true);
}
else {
int startNode = results.empty() ? startNodeNum : results.back().getEndNode();
TextReader newReader3(prevInput, currInput, env);
notMatchingCaseSegs = 0;
this->processOneWord(env, newReader3, startNode, results, true);
}
}
}
// currInput == inputEnd
if (!env.isSeparator(codepoint)) {
if (separatorFound) {
int startNode = results.empty() ? startNodeNum : results.back().getEndNode();
TextReader newReader4(prevInput, inputEnd, env);
this->processOneWord(env, newReader4, startNode, results, true);
}
else {
this->appendIgnotiumToResults(env, string(inputStart, inputEnd), startNodeNum, results);
}
}
}
void MorfeuszInternal::appendIgnotiumToResults(
const Environment& env,
const string& word,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
MorphInterpretation interp(MorphInterpretation::createIgn(startNodeNum, startNodeNum + 1, word, env.getTagset()));
results.push_back(interp);
}
ResultsIterator MorfeuszInternal::analyze(const string& text) const {
vector<MorphInterpretation> res;
this->analyze(text, res);
return ResultsIterator(res);
}
void MorfeuszInternal::analyze(const string& text, vector<MorphInterpretation>& results) const {
const char* input = text.c_str();
const char* inputEnd = input + text.length();
TextReader reader(input, inputEnd, this->analyzerEnv);
while (!reader.isAtEnd()) {
this->processOneWord(this->analyzerEnv, reader, nextNodeNum, results);
if (!results.empty()) {
nextNodeNum = results.back().getEndNode();
}
}
if (options.tokenNumbering == SEPARATE) {
nextNodeNum = 0;
}
}
ResultsIterator MorfeuszInternal::generate(const string& text) const {
vector<MorphInterpretation> res;
this->generate(text, res);
return ResultsIterator(res);
}
ResultsIterator MorfeuszInternal::generate(const string& text, int tagnum) const {
vector<MorphInterpretation> res;
this->generate(text, tagnum, res);
return ResultsIterator(res);
}
void MorfeuszInternal::generate(const string& lemma, vector<MorphInterpretation>& results) const {
const char* input = lemma.c_str();
const char* inputEnd = input + lemma.length();
int startNode = 0;
TextReader reader(input, inputEnd, this->generatorEnv);
this->processOneWord(this->generatorEnv, reader, startNode, results);
if (reader.getCurrPtr() != reader.getEndPtr()) {
throw MorfeuszException("Input contains more than one word");
}
}
// XXX - someday it should be improved
void MorfeuszInternal::generate(const std::string& lemma, int tagnum, vector<MorphInterpretation>& result) const {
vector<MorphInterpretation> partRes;
this->generate(lemma, partRes);
for (unsigned int i = 0; i < partRes.size(); i++) {
if (partRes[i].getTagnum() == tagnum) {
result.push_back(partRes[i]);
}
}
}
void MorfeuszInternal::setCharset(Charset charset) {
this->options.encoding = charset;
this->analyzerEnv.setCharset(charset);
this->generatorEnv.setCharset(charset);
}
void MorfeuszInternal::setAggl(const std::string& aggl) {
this->analyzerEnv.setSegrulesOption("aggl", aggl);
this->generatorEnv.setSegrulesOption("aggl", aggl);
}
void MorfeuszInternal::setPraet(const std::string& praet) {
this->analyzerEnv.setSegrulesOption("praet", praet);
this->generatorEnv.setSegrulesOption("praet", praet);
}
void MorfeuszInternal::setCaseHandling(CaseHandling caseHandling) {
this->options.caseHandling = caseHandling;
this->analyzerEnv.setCaseSensitive(caseHandling != IGNORE);
}
void MorfeuszInternal::setTokenNumbering(TokenNumbering tokenNumbering) {
this->options.tokenNumbering = tokenNumbering;
}
void MorfeuszInternal::setWhitespaceHandling(WhitespaceHandling whitespaceHandling) {
this->options.whitespaceHandling = whitespaceHandling;
}
void MorfeuszInternal::setDebug(bool debug) {
this->options.debug = debug;
}
const Tagset<string>& MorfeuszInternal::getDefaultAnalyzerTagset() const {
return this->generatorEnv.getTagset();
}
const Tagset<string>& MorfeuszInternal::getDefaultGeneratorTagset() const {
return this->analyzerEnv.getTagset();
}
}