forked from aymara/lima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplyRecognizer.h
289 lines (257 loc) · 9.93 KB
/
applyRecognizer.h
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
/*
Copyright 2002-2013 CEA LIST
This file is part of LIMA.
LIMA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LIMA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with LIMA. If not, see <http://www.gnu.org/licenses/>
*/
/************************************************************************
*
* @file applyRecognizer.h
* @author Besancon Romaric ([email protected])
* @date Wed Dec 17 2003
* @version $Id$
* copyright Copyright (C) 2003 by CEA LIST
* Project
*
* @brief application of a recognizer, depending of the preceding
* analysis of the text (no analysis, tokenization, complete
* analysis)
*
***********************************************************************/
#ifndef APPLYRECOGNIZER_H
#define APPLYRECOGNIZER_H
#include "linguisticProcessing/core/Automaton/recognizer.h"
//#include "AutomatonText/recognizerText.h"
#include "linguisticProcessing/core/Tokenizer/Tokenizer.h"
#include "linguisticProcessing/core/Tokenizer/Automaton.h"
#include "linguisticProcessing/core/Tokenizer/CharChart.h"
#include <iostream>
#include <string>
namespace Lima {
namespace LinguisticProcessing {
// two outputs possible : one MUC-like style where the tags are placed in
// the text (inline), and one Detect-like style, where the identified
// entities are placed in a separate file (offline)
typedef enum { INLINE, OFFLINE } StyleOutput;
enum OutputNormalizationType {
NO_NORMALIZATION, /**< no normalization is indicated */
DEFAULT_NORMALIZATION, /**< when there is no normalization available,
uses a default normalization (which is simply
the string of the entity in the text */
KNOWN_NORMALIZATION_ONLY /**< output normalization only if there is
a true normaization available for the
entity */
};
//**********************************************************************
// generic virtual class
//**********************************************************************
class RecognizerToApply {
public:
RecognizerToApply(Automaton::Recognizer* reco,
MediaId language):
m_language(language),
m_recognizer(reco),
m_listEntities(false),
m_encoding("latin1"),
m_outputStyle(INLINE),
m_normalizationStyle(NO_NORMALIZATION),
m_doPosTagging(false),
m_testOnFullToken(true)
{}
virtual ~RecognizerToApply() {}
virtual uint64_t applyToText(LimaString contentText,
std::ostream& output) =0;
void setParameters(const bool listEntities,
const std::string& encoding,
const StyleOutput& outputStyle,
const OutputNormalizationType& normalization,
const bool doPosTagging=false,
const bool testOnFullToken=true);
template<typename ResultType>
static LimaString
outputString(const ResultType& result,
const LimaString& text,
const uint64_t offsetLastSuccess,
const Common::MediaticData::LanguageData::EntityNames& entityNames,
const OutputNormalizationType normType=NO_NORMALIZATION,
const uint64_t firstOffset=1);
template<typename ResultType>
void printResults(const ResultType& result,
const LimaString& text,
std::ostream& output);
static bool findType(const std::string& type,
uint64_t& i);
static const std::string& getOpeningTag(const uint64_t& i);
static const std::string& getClosingTag(const uint64_t& i);
static const std::vector<std::string>& knownTypes();
static const std::vector<std::string>& openingTags();
static const std::vector<std::string>& closingTags();
unsigned char getLanguage() { return m_language; }
protected:
MediaId m_language;
Automaton::Recognizer* m_recognizer;
// parameters
bool m_listEntities;
std::string m_encoding;
StyleOutput m_outputStyle;
OutputNormalizationType m_normalizationStyle;
bool m_doPosTagging;
bool m_testOnFullToken;
};
//**********************************************************************
// apply recognizer after complete analysis
//**********************************************************************
class RecognizerOnAnalyzedText : public RecognizerToApply {
public:
RecognizerOnAnalyzedText(Automaton::Recognizer* reco,
MediaId language,
const std::string& resourcesPath,
const bool dumpXML=false,
const bool reorganizeRules=false);
~RecognizerOnAnalyzedText() {
}
uint64_t applyToText(LimaString contentText, std::ostream& output);
protected:
};
//**********************************************************************
// apply recognizer with no analysis, based on tokenization
//**********************************************************************
/*
class RecognizerOnTokenizedText : public RecognizerToApply {
public:
RecognizerOnTokenizedText(Automaton::Recognizer* reco,
MediaId language,
const std::string& resourcesPath);
~RecognizerOnTokenizedText() {
delete m_tokenizer;
}
uint64_t applyToText(LimaString contentText, std::ostream& output);
protected:
Tokenizer::Tokenizer* m_tokenizer;
MediaId m_language;
};
*/
//**********************************************************************
// apply recognizer with no analysis, on simple text, based on
// basic tokenization on spaces
//**********************************************************************
/*
class RecognizerOnSimpleText : public RecognizerToApply {
public:
RecognizerOnSimpleText(Automaton::Recognizer* reco);
uint64_t applyToText(LimaString contentText, std::ostream& output);
protected:
Automaton::RecognizerText* m_recognizerText;
};
*/
//**********************************************************************
// template function for output
/**
* text string with tags inserted around matches
*
* @param result the result of the named entity search
* @param text the reference text in which the tag are inserted
* @param offsetLastSuccess the position where we start in the text
* @param openingTag the opening tag to insert
* @param closingTag the closing tag to insert
* @param firstOffset the offset of the first element in the text
* (to deal with both FullTokens that start at 1 and usual strings
* that start at 0 -- default value is 1)
*
* @return a LimaString
*/
// the template function can be defined in the .cpp because
// it is only instanciated in this file
template<typename ResultType>
LimaString RecognizerToApply::
outputString(const ResultType& result,
const LimaString& text,
const uint64_t offsetLastSuccess,
const Common::MediaticData::LanguageData::EntityNames& entityNames,
const OutputNormalizationType normType,
const uint64_t firstOffset)
{
// have to take into account the offset of the first character of
// the string because FullToken positions begin at 1, not 0
if (result.size() == 0) {
return text;
}
LimaString output;
uint64_t lastPosition(offsetLastSuccess);
LimaString openingTag, closingTag;
uint64_t i;
Common::MediaticData::LanguageData::EntityNames::const_iterator
type=entityNames.find(result.getType());
if (type==entityNames.end()) {
AULOGINIT;
LERROR << "undefined type " << result.getType();
openingTag="UNDEFINED_TYPE";
closingTag="/UNDEFINED_TYPE";
}
else {
const std::string& typeName=(*type).second;
if (findType(typeName,i)) {
openingTag = getOpeningTag(i);
closingTag = getClosingTag(i);
}
else {
openingTag=typeName;
closingTag="/"+typeName;
}
}
switch (normType) {
case DEFAULT_NORMALIZATION:
{
if (result.getNormalizedForm().size() != 0) {
openingTag += LimaChar(' ')+result.getNormalizedForm().str();
}
else {
std::ostringstream oss;
oss << DEFAULT_ATTRIBUTE << "=\""
<< result.concatString() << "\"" ;
LimaString defaultNormalizedForm(oss.str());
openingTag += LimaChar(' ')+defaultNormalizedForm;
}
break;
}
case KNOWN_NORMALIZATION_ONLY:
{
if (result.getNormalizedForm().size() != 0) {
openingTag += LimaChar(' ')+result.getNormalizedForm().str();
}
break;
}
case NO_NORMALIZATION:
{
break;
}
}
/* for (uint64_t i(0); i<result.numberOfMatches(); i++) { */
/* output += LimaString(text,lastPosition, */
/* result[i].position()-firstOffset-lastPosition) */
/* +LimaString("<")+openingTag+LimaString(">") */
/* +LimaString(text,result[i].position()-firstOffset,result[i].length()) */
/* +LimaString("<")+closingTag+LimaString(">"); */
/* lastPosition=result[i].positionEnd()-firstOffset; */
/* } */
if (! result.empty()) {
// keep notKept element inside tags
output += LimaString(text,lastPosition,
result.positionBegin()-firstOffset-lastPosition)
+Common::Misc::utf8stdstring2limastring("<")+openingTag+Common::Misc::utf8stdstring2limastring(">")
+Common::Misc::utf8stdstring2limastring(LimaString(text,result.positionBegin()-firstOffset,result.length()))
+Common::Misc::utf8stdstring2limastring("<")+closingTag+Common::Misc::utf8stdstring2limastring(">");
}
return output;
}
} // end namespace
} // end namespace
#endif