InterpretedChunksDecoder4Analyzer.cpp
6.33 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
/*
* File: InterpretedChunksDecoder4Analyzer.cpp
* Author: mlenart
*
* Created on 15 maj 2014, 15:28
*/
#include "InterpretedChunksDecoder4Analyzer.hpp"
#include <string>
using namespace std;
namespace morfeusz {
InterpretedChunksDecoder4Analyzer::InterpretedChunksDecoder4Analyzer(const Environment& env) : InterpretedChunksDecoder(env) {
}
void InterpretedChunksDecoder4Analyzer::decode(
unsigned int startNode,
unsigned int endNode,
const InterpretedChunk& interpretedChunk,
std::vector<MorphInterpretation>& out) const {
string orth;
string lemma4Prefixes;
if (tryToGetLemma4Prefixes(interpretedChunk, lemma4Prefixes)) {
orth.insert(orth.end(), interpretedChunk.textStartPtr, interpretedChunk.chunkEndPtr);
const unsigned char* currPtr = getInterpretationsPtr(interpretedChunk.interpsGroupPtr);
while (currPtr < interpretedChunk.interpsEndPtr) {
DecodeMorphInterpParams params = {startNode, endNode, orth, lemma4Prefixes, interpretedChunk};
this->decodeMorphInterpretation(params, currPtr, out);
}
}
}
void InterpretedChunksDecoder4Analyzer::decodeLemma(
const EncodedForm& encodedLemma,
int nonPrefixCodepointsNum,
bool forPrefix,
string& res) const {
// assert(nonPrefixCodepointsNum > orth.size());
unsigned int prefixSegmentsOrthLength = forPrefix
? 0
: (unsigned int) normalizedCodepoints.size() - nonPrefixCodepointsNum;
size_t endIdx = forPrefix
? normalizedCodepoints.size()
: normalizedCodepoints.size() - encodedLemma.suffixToCut;
for (unsigned int i = prefixSegmentsOrthLength + encodedLemma.prefixToCut; i < endIdx; i++) {
uint32_t cp =
(i < encodedLemma.casePattern.size() && encodedLemma.casePattern[i])
? env.getCaseConverter().toTitle(normalizedCodepoints[i])
: normalizedCodepoints[i];
env.getCharsetConverter().append(cp, res);
}
if (!forPrefix) {
const char* suffixPtr = encodedLemma.suffixToAdd.c_str();
const char* suffixEnd = suffixPtr + encodedLemma.suffixToAdd.length();
while (suffixPtr != suffixEnd) {
uint32_t cp = UTF8CharsetConverter::getInstance().next(suffixPtr, suffixEnd);
env.getCharsetConverter().append(cp, res);
}
}
}
void InterpretedChunksDecoder4Analyzer::decodeEncodedForm(const unsigned char*& ptr, unsigned char compressionByte, EncodedForm& encodedForm) const {
encodedForm.prefixToCut = hasCompressedPrefixCut(compressionByte)
? getPrefixCutLength(compressionByte)
: readInt8(ptr);
encodedForm.suffixToCut = readInt8(ptr);
encodedForm.suffixToAdd = readString(ptr);
assert(encodedForm.casePattern.size() == 0);
if (isLemmaOnlyLower(compressionByte)) {
}
else if (isLemmaOnlyTitle(compressionByte)) {
encodedForm.casePattern.push_back(true);
}
else {
encodedForm.casePattern = env.getCasePatternHelper().deserializeOneCasePattern(ptr);
}
}
EncodedInterpretation InterpretedChunksDecoder4Analyzer::decodeEncodedInterp(const unsigned char*& ptr, unsigned char compressionByte) const {
EncodedInterpretation interp;
if (isOrthOnlyLower(compressionByte)) {
}
else if (isOrthOnlyTitle(compressionByte)) {
interp.orthCasePattern.push_back(true);
}
else {
interp.orthCasePattern = this->env.getCasePatternHelper().deserializeOneCasePattern(ptr);
}
decodeEncodedForm(ptr, compressionByte, interp.value);
interp.tag = readInt16(ptr);
interp.nameClassifier = *ptr++;
interp.qualifiers = readInt16(ptr);
return interp;
}
void InterpretedChunksDecoder4Analyzer::decodeMorphInterpretation(
const DecodeMorphInterpParams& params,
const unsigned char*& ptr,
std::vector<MorphInterpretation>& out) const {
orthCodepoints.resize(0);
normalizedCodepoints.resize(0);
const char* currPtr = params.chunk.textStartPtr;
while (currPtr != params.chunk.textEndPtr) {
uint32_t cp = env.getCharsetConverter().next(currPtr, params.chunk.textEndPtr);
orthCodepoints.push_back(cp);
normalizedCodepoints.push_back(env.getCaseConverter().toLower(cp));
}
EncodedInterpretation ei = this->decodeEncodedInterp(ptr, *params.chunk.interpsGroupPtr);
if (params.chunk.forceIgnoreCase || env.getCasePatternHelper().checkCasePattern(normalizedCodepoints, orthCodepoints, ei.orthCasePattern)) {
string lemma(params.lemma4Prefixes);
lemma.reserve(lemma.size() + normalizedCodepoints.size());
this->decodeLemma(ei.value, params.chunk.codepointsNum, false, lemma);
MorphInterpretation mi(
params.startNode, params.endNode,
params.orth, lemma,
ei.tag,
ei.nameClassifier,
&env.getQualifiersHelper().getQualifiers(ei.qualifiers),
&env.getTagset());
out.push_back(mi);
}
}
bool InterpretedChunksDecoder4Analyzer::tryToGetLemma4Prefixes(
const InterpretedChunk& interpretedChunk,
string& lemma4Prefixes) const {
for (unsigned int i = 0; i < interpretedChunk.prefixChunks.size(); i++) {
const InterpretedChunk& prefixChunk = interpretedChunk.prefixChunks[i];
if (!tryToGetLemma4OnePrefix(prefixChunk, lemma4Prefixes)) {
return false;
}
}
return true;
}
bool InterpretedChunksDecoder4Analyzer::tryToGetLemma4OnePrefix(const InterpretedChunk& prefixChunk, std::string& lemma4Prefixes) const {
orthCodepoints.resize(0);
normalizedCodepoints.resize(0);
const char* currTextPtr = prefixChunk.textStartPtr;
while (currTextPtr != prefixChunk.textEndPtr) {
uint32_t cp = env.getCharsetConverter().next(currTextPtr, prefixChunk.textEndPtr);
orthCodepoints.push_back(cp);
normalizedCodepoints.push_back(env.getCaseConverter().toLower(cp));
}
const unsigned char* currPtr = getInterpretationsPtr(prefixChunk.interpsGroupPtr);
EncodedInterpretation ei = this->decodeEncodedInterp(currPtr, *prefixChunk.interpsGroupPtr);
if (env.getCasePatternHelper().checkCasePattern(normalizedCodepoints, orthCodepoints, ei.orthCasePattern)) {
this->decodeLemma(ei.value, prefixChunk.codepointsNum, true, lemma4Prefixes);
return true;
}
else {
return false;
}
}
}