InterpretedChunksDecoder.hpp
2.76 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
/*
* File: InterpsGroupDecoder.hpp
* Author: mlenart
*
* Created on November 22, 2013, 10:35 PM
*/
#ifndef INTERPSGROUPDECODER_HPP
#define INTERPSGROUPDECODER_HPP
#include "charset/CharsetConverter.hpp"
#include "EncodedInterpretation.hpp"
#include "InterpretedChunk.hpp"
#include "EncodedInterpretation.hpp"
#include "charset/CaseConverter.hpp"
#include "Environment.hpp"
class InterpretedChunksDecoder {
public:
InterpretedChunksDecoder(const Environment& env)
: env(env) {
}
template <class OutputIterator>
OutputIterator decode(
unsigned int startNode,
unsigned int endNode,
const InterpretedChunk& interpretedChunk,
OutputIterator out) {
string orth;
string lemmaPrefix;
for (unsigned int i = 0; i < interpretedChunk.prefixChunks.size(); i++) {
const InterpretedChunk& prefixChunk = interpretedChunk.prefixChunks[i];
orth += env.getCharsetConverter().toString(prefixChunk.originalCodepoints);
lemmaPrefix += convertLemma(
prefixChunk.lowercaseCodepoints,
prefixChunk.interpsGroup.interps[0].lemma);
}
orth += env.getCharsetConverter().toString(interpretedChunk.originalCodepoints);
for (unsigned int i = 0; i < interpretedChunk.interpsGroup.interps.size(); i++) {
const EncodedInterpretation& ei = interpretedChunk.interpsGroup.interps[i];
string lemma = lemmaPrefix + convertLemma(
interpretedChunk.lowercaseCodepoints,
ei.lemma);
*out = MorphInterpretation(
startNode, endNode,
orth, lemma,
ei.tag,
ei.nameClassifier,
env.getAnalyzerTagset(),
env.getCharsetConverter());
++out;
}
return out;
}
private:
string convertLemma(
const vector<uint32_t>& orth,
const EncodedLemma& lemma) {
string res;
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);
}
return res;
}
const Environment& env;
};
#endif /* INTERPSGROUPDECODER_HPP */