Morfeusz.cpp
8.55 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
/*
* File: Morfeusz.cpp
* Author: mlenart
*
* Created on November 13, 2013, 5:21 PM
*/
#include <string>
#include <iostream>
#include "fsa/fsa.hpp"
#include "utils.hpp"
#include "data/default_fsa.hpp"
#include "Morfeusz.hpp"
#include "MorphDeserializer.hpp"
#include "InterpretedChunksDecoder.hpp"
#include "charset/CharsetConverter.hpp"
#include "charset/charset_utils.hpp"
#include "charset/CaseConverter.hpp"
#include "const.hpp"
// TODO - konstruktor kopiujący działający Tak-Jak-Trzeba
using namespace std;
static Deserializer<vector<InterpsGroup> >* initializeDeserializer() {
static Deserializer < vector < InterpsGroup > > *deserializer
= new MorphDeserializer();
return deserializer;
}
static FSA<vector<InterpsGroup > > *initializeFSA(const string& filename) {
cerr << "initialize FSA" << endl;
return FSA < vector < InterpsGroup > > ::getFSA(filename, *initializeDeserializer());
}
static CharsetConverter* getCharsetConverter(MorfeuszCharset charset) {
cerr << "initialize charset converter for " << charset << endl;
static CharsetConverter* utf8Converter = new UTF8CharsetConverter();
// static CharsetConverter* utf16LEConverter = new UTF16CharsetConverter(UTF16CharsetConverter::UTF16CharsetConverter::LE);
// static CharsetConverter* utf16BEConverter = new UTF16CharsetConverter(UTF16CharsetConverter::Endianness::BE);
static CharsetConverter* iso8859_2Converter = new ISO8859_2_CharsetConverter();
static CharsetConverter* windows1250Converter = new Windows_1250_CharsetConverter();
static CharsetConverter* cp852Converter = new CP852_CharsetConverter();
switch (charset) {
case UTF8:
return utf8Converter;
// case UTF16LE:
// return utf16LEConverter;
// case UTF16BE:
// return utf16BEConverter;
case ISO8859_2:
return iso8859_2Converter;
case CP1250:
return windows1250Converter;
case CP852:
return cp852Converter;
default:
throw MorfeuszException("invalid charset");
}
}
static Tagset* initializeTagset(const string& filename) {
cerr << "initialize tagset" << endl;
static Tagset* tagset = new Tagset(readFile<unsigned char>(filename.c_str()));
return tagset;
}
static Tagset* initializeTagset(const unsigned char* data) {
cerr << "initialize tagset" << endl;
static Tagset* tagset = new Tagset(data);
return tagset;
}
static CaseConverter* initializeCaseConverter() {
cerr << "initialize case converter" << endl;
static CaseConverter* cc = new CaseConverter();
return cc;
}
static MorfeuszOptions createDefaultOptions() {
MorfeuszOptions res;
res.caseSensitive = true;
res.encoding = UTF8;
return res;
}
Morfeusz::Morfeusz()
: fsa(FSAType::getFSA(DEFAULT_FSA, *initializeDeserializer())),
charsetConverter(getCharsetConverter(DEFAULT_MORFEUSZ_CHARSET)),
tagset(initializeTagset(DEFAULT_FSA)),
caseConverter(initializeCaseConverter()),
options(createDefaultOptions()) {
}
Morfeusz::Morfeusz(const string& filename)
: fsa(initializeFSA(filename)),
charsetConverter(getCharsetConverter(DEFAULT_MORFEUSZ_CHARSET)),
tagset(initializeTagset(filename)),
caseConverter(initializeCaseConverter()),
options(createDefaultOptions()) {
}
Morfeusz::~Morfeusz() {
// delete &this->fsa;
// delete &this->charsetConverter;
}
void Morfeusz::processOneWord(
const char*& inputData,
const char* inputEnd,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
while (inputData != inputEnd
&& isEndOfWord(this->charsetConverter->peek(inputData, inputEnd))) {
this->charsetConverter->next(inputData, inputEnd);
}
const char* wordStart = inputData;
DEBUG(wordStart);
vector<InterpretedChunk> accum;
FlexionGraph graph;
const char* currInput = inputData;
doProcessOneWord(currInput, inputEnd, accum, graph);
if (!graph.empty()) {
InterpretedChunksDecoder interpretedChunksDecoder(*tagset, *charsetConverter, *caseConverter);
int srcNode = startNodeNum;
for (unsigned int i = 0; i < graph.getTheGraph().size(); i++) {
vector<FlexionGraph::Edge>& edges = graph.getTheGraph()[i];
for (unsigned int j = 0; j < edges.size(); j++) {
FlexionGraph::Edge& e = edges[j];
int targetNode = startNodeNum + e.nextNode;
interpretedChunksDecoder.decode(srcNode, targetNode, e.chunk, back_inserter(results));
}
srcNode++;
}
// graph.getResults(*this->tagset, results);
} else if (wordStart != currInput) {
this->appendIgnotiumToResults(string(wordStart, currInput), startNodeNum, results);
}
inputData = currInput;
}
void Morfeusz::doProcessOneWord(
const char*& inputData,
const char* inputEnd,
vector<InterpretedChunk>& accum,
FlexionGraph& graph) const {
bool endOfWord = inputData == inputEnd;
const char* currInput = inputData;
uint32_t codepoint = endOfWord ? 0 : this->charsetConverter->next(currInput, inputEnd);
// UnicodeChunk uchunk(*(this->charsetConverter), *(this->caseConverter));
vector<uint32_t> originalCodepoints;
vector<uint32_t> lowercaseCodepoints;
StateType state = this->fsa->getInitialState();
while (!isEndOfWord(codepoint)) {
uint32_t lowerCP = this->caseConverter->toLower(codepoint);
originalCodepoints.push_back(codepoint);
lowercaseCodepoints.push_back(lowerCP);
this->feedState(state, lowerCP);
if (state.isAccepting()) {
vector< InterpsGroup > val(state.getValue());
for (unsigned int i = 0; i < val.size(); i++) {
InterpsGroup& ig = val[i];
InterpretedChunk ic = {inputData, originalCodepoints, lowercaseCodepoints, ig};
accum.push_back(ic);
const char* newCurrInput = currInput;
doProcessOneWord(newCurrInput, inputEnd, accum, graph);
accum.pop_back();
}
}
codepoint = currInput == inputEnd ? 0 : this->charsetConverter->peek(currInput, inputEnd);
if (!isEndOfWord(codepoint)) {
this->charsetConverter->next(currInput, inputEnd);
}
}
if (state.isAccepting()) {
vector<InterpsGroup > val(state.getValue());
for (unsigned int i = 0; i < val.size(); i++) {
InterpsGroup& ig = val[i];
InterpretedChunk ic = {inputData, originalCodepoints, lowercaseCodepoints, ig};
accum.push_back(ic);
graph.addPath(accum);
accum.pop_back();
}
}
inputData = currInput;
}
void Morfeusz::feedState(
StateType& state,
int codepoint) const {
string chars;
this->utf8CharsetConverter.append(codepoint, chars);
for (unsigned int i = 0; i < chars.length(); i++) {
state.proceedToNext(chars[i]);
}
}
void Morfeusz::appendIgnotiumToResults(
const string& word,
int startNodeNum,
std::vector<MorphInterpretation>& results) const {
MorphInterpretation interp = MorphInterpretation::createIgn(startNodeNum, word, *this->tagset, *this->charsetConverter);
results.push_back(interp);
}
ResultsIterator Morfeusz::analyze(const string& text) const {
// const char* textStart = text.c_str();
// const char* textEnd = text.c_str() + text.length();
return ResultsIterator(text, *this);
}
void Morfeusz::analyze(const string& text, vector<MorphInterpretation>& results) const {
const char* input = text.c_str();
const char* inputEnd = input + text.length();
while (input != inputEnd) {
int startNode = results.empty() ? 0 : results.back().getEndNode();
DEBUG("process " + string(input, inputEnd));
this->processOneWord(input, inputEnd, startNode, results);
}
}
void Morfeusz::setEncoding(MorfeuszCharset encoding) {
this->options.encoding = encoding;
this->charsetConverter = getCharsetConverter(encoding);
}
ResultsIterator::ResultsIterator(const string& text, const Morfeusz& morfeusz)
: rawInput(text.c_str()),
morfeusz(morfeusz) {
}
MorphInterpretation ResultsIterator::getNext() {
// if (resultsBuffer.empty()) {
// morfeusz.processOneWord(rawInput, startNode, back_inserter(resultsBuffer));
// }
// startNode = resultsBuffer.back().getEndNode();
// MorphInterpretation res = resultsBuffer.front();
// resultsBuffer.pop_front();
// return res;
}
bool ResultsIterator::hasNext() {
return rawInput[0] != '\0' && resultsBuffer.empty();
}