InterpretedChunksDecoder.hpp
9.11 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
/*
* File: InterpsGroupDecoder.hpp
* Author: mlenart
*
* Created on November 22, 2013, 10:35 PM
*/
#ifndef INTERPSGROUPDECODER_HPP
#define INTERPSGROUPDECODER_HPP
#include <string>
#include <vector>
#include <utility>
#include "charset/CharsetConverter.hpp"
#include "EncodedInterpretation.hpp"
#include "InterpretedChunk.hpp"
#include "EncodedInterpretation.hpp"
#include "charset/CaseConverter.hpp"
#include "Environment.hpp"
#include "MorphInterpretation.hpp"
#include "CasePatternHelper.hpp"
#include "deserializationUtils.hpp"
class InterpretedChunksDecoder {
public:
InterpretedChunksDecoder(const Environment& env)
: env(env) {
}
virtual ~InterpretedChunksDecoder() {
}
virtual void decode(
unsigned int startNode,
unsigned int endNode,
const InterpretedChunk& interpretedChunk,
std::vector<MorphInterpretation>& out) const = 0;
protected:
const Environment& env;
};
class InterpretedChunksDecoder4Analyzer : public InterpretedChunksDecoder {
public:
InterpretedChunksDecoder4Analyzer(const Environment& env) : InterpretedChunksDecoder(env) {
}
void decode(
unsigned int startNode,
unsigned int endNode,
const InterpretedChunk& interpretedChunk,
std::vector<MorphInterpretation>& out) const {
string orth;
string lemmaPrefix;
if (convertPrefixes(interpretedChunk, orth, lemmaPrefix)) {
orth += this->env.getCharsetConverter().toString(interpretedChunk.originalCodepoints);
const unsigned char* currPtr = interpretedChunk.interpsPtr;
while (currPtr < interpretedChunk.interpsEndPtr) {
this->decodeMorphInterpretation(startNode, endNode, orth, lemmaPrefix, interpretedChunk, currPtr, out);
}
}
}
protected:
void decodeForm(
const vector<uint32_t>& orth,
const EncodedForm& lemma,
string& res) const {
for (unsigned int i = 0; i < orth.size() - lemma.suffixToCut; i++) {
uint32_t cp =
(i < lemma.casePattern.size() && lemma.casePattern[i])
? env.getCaseConverter().toTitle(orth[i])
: orth[i];
env.getCharsetConverter().append(cp, res);
}
const char* suffixPtr = lemma.suffixToAdd.c_str();
const char* suffixEnd = suffixPtr + lemma.suffixToAdd.length();
while (suffixPtr != suffixEnd) {
uint32_t cp = UTF8CharsetConverter().next(suffixPtr, suffixEnd);
env.getCharsetConverter().append(cp, res);
}
}
void deserializeEncodedForm(const unsigned char*& ptr, EncodedForm& encodedForm) const {
encodedForm.suffixToCut = *ptr;
ptr++;
encodedForm.suffixToAdd = (const char*) ptr;
ptr += strlen((const char*) ptr) + 1;
assert(encodedForm.casePattern.size() == 0);
encodedForm.casePattern = env.getCasePatternHelper().deserializeOneCasePattern(ptr);
}
EncodedInterpretation deserializeInterp(const unsigned char*& ptr) const {
EncodedInterpretation interp;
interp.orthCasePattern = this->env.getCasePatternHelper().deserializeOneCasePattern(ptr);
deserializeEncodedForm(ptr, interp.value);
interp.tag = readInt16(ptr);
interp.nameClassifier = *ptr++;
interp.qualifiers = readInt16(ptr);
return interp;
}
private:
pair<string, string> getLemmaHomonymIdPair(const string& lemma) const {
vector<string> splitRes(split(lemma, ':'));
if (splitRes.size() == 2) {
return make_pair(splitRes[0], splitRes[1]);
} else {
return make_pair(lemma, "");
}
}
void decodeMorphInterpretation(
unsigned int startNode, unsigned int endNode,
const string& orth,
const string& lemmaPrefix,
const InterpretedChunk& chunk,
const unsigned char*& ptr,
std::vector<MorphInterpretation>& out) const {
string lemma = lemmaPrefix;
EncodedInterpretation ei = this->deserializeInterp(ptr);
this->decodeForm(chunk.lowercaseCodepoints, ei.value, lemma);
if (env.getCasePatternHelper().checkCasePattern(chunk.lowercaseCodepoints, chunk.originalCodepoints, ei.orthCasePattern)) {
pair<string, string> lemmaHomonymId = getLemmaHomonymIdPair(lemma);
out.push_back(MorphInterpretation(
startNode, endNode,
orth, lemmaHomonymId.first,
lemmaHomonymId.second,
ei.tag,
ei.nameClassifier,
ei.qualifiers,
env));
}
}
bool convertPrefixes(const InterpretedChunk& interpretedChunk, std::string& orth, std::string& lemmaPrefix) const {
for (unsigned int i = 0; i < interpretedChunk.prefixChunks.size(); i++) {
const InterpretedChunk& prefixChunk = interpretedChunk.prefixChunks[i];
orth += env.getCharsetConverter().toString(prefixChunk.originalCodepoints);
const unsigned char* ptr = prefixChunk.interpsPtr;
std::vector<MorphInterpretation> mi;
// env.getCasePatternHelper().skipCasePattern(ptr);
this->decodeMorphInterpretation(0, 0, orth, string(""), prefixChunk, ptr, mi);
if (!mi.empty()) {
lemmaPrefix += mi[0].getLemma();
} else {
return false;
}
}
return true;
}
};
class InterpretedChunksDecoder4Generator : public InterpretedChunksDecoder {
public:
InterpretedChunksDecoder4Generator(const Environment& env) : InterpretedChunksDecoder(env) {
}
void decode(
unsigned int startNode,
unsigned int endNode,
const InterpretedChunk& interpretedChunk,
std::vector<MorphInterpretation>& out) const {
string orthPrefix;
string lemma;
convertPrefixes(interpretedChunk, orthPrefix, lemma);
lemma += env.getCharsetConverter().toString(interpretedChunk.originalCodepoints);
const unsigned char* currPtr = interpretedChunk.interpsPtr;
while (currPtr < interpretedChunk.interpsEndPtr) {
MorphInterpretation mi = this->decodeMorphInterpretation(startNode, endNode, orthPrefix, lemma, interpretedChunk, currPtr);
// cerr << mi.toString(false) << endl;
// cerr << "required='" << interpretedChunk.requiredHomonymId << "' morphInterp='" << mi.getHomonymId() << "'" << endl;
if (interpretedChunk.requiredHomonymId.empty() || mi.getHomonymId() == interpretedChunk.requiredHomonymId) {
out.push_back(mi);
}
}
}
private:
void convertPrefixes(const InterpretedChunk& interpretedChunk, std::string& orthPrefix, std::string& lemma) const {
for (unsigned int i = 0; i < interpretedChunk.prefixChunks.size(); i++) {
const InterpretedChunk& prefixChunk = interpretedChunk.prefixChunks[i];
lemma += env.getCharsetConverter().toString(prefixChunk.originalCodepoints);
const unsigned char* ptr = prefixChunk.interpsPtr;
MorphInterpretation mi = this->decodeMorphInterpretation(0, 0, orthPrefix, string(""), prefixChunk, ptr);
orthPrefix += mi.getOrth();
}
}
MorphInterpretation decodeMorphInterpretation(
unsigned int startNode, unsigned int endNode,
const string& orthPrefix,
const string& lemma,
const InterpretedChunk& chunk,
const unsigned char*& ptr) const {
string orth = orthPrefix;
EncodedInterpretation ei = this->deserializeInterp(ptr);
this->decodeForm(chunk.originalCodepoints, ei.value, orth);
return MorphInterpretation(
startNode, endNode,
orth, lemma,
ei.homonymId,
ei.tag,
ei.nameClassifier,
ei.qualifiers,
env);
}
void decodeForm(
const vector<uint32_t>& lemma,
const EncodedForm& orth,
string& res) const {
res += orth.prefixToAdd;
for (unsigned int i = 0; i < lemma.size() - orth.suffixToCut; i++) {
env.getCharsetConverter().append(lemma[i], res);
}
const char* suffixPtr = orth.suffixToAdd.c_str();
const char* suffixEnd = suffixPtr + orth.suffixToAdd.length();
while (suffixPtr != suffixEnd) {
uint32_t cp = UTF8CharsetConverter().next(suffixPtr, suffixEnd);
env.getCharsetConverter().append(cp, res);
}
}
EncodedInterpretation deserializeInterp(const unsigned char*& ptr) const {
EncodedInterpretation interp;
interp.homonymId = readString(ptr);
interp.value.prefixToAdd = readString(ptr);
interp.value.suffixToCut = readInt8(ptr);
interp.value.suffixToAdd = readString(ptr);
interp.tag = readInt16(ptr);
interp.nameClassifier = readInt8(ptr);
interp.qualifiers = readInt16(ptr);
return interp;
}
};
#endif /* INTERPSGROUPDECODER_HPP */