LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
WordFeatureExtractor.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file Features.cpp
9 * @author Romaric Besancon (romaric.besancon@cea.fr)
10 * @date Mon Feb 7 2011
11 * copyright Copyright (C) 2011 by CEA LIST
12 *
13 ***********************************************************************/
14
18#include <sstream>
19
23
24using namespace std;
25using namespace boost;
27
28using namespace Lima::Common::MediaticData;
30
31namespace Lima {
32namespace LinguisticProcessing {
33
34//***********************************************************************
35// factories for feature extractors
36AbstractFeatureExtractor::AbstractFeatureExtractor(MediaId language, const std::string& /*complement*/):
37m_language(language)
38{
39}
40
46
56
57//***********************************************************************
58// Feature list
59//***********************************************************************
61m_language(0)
62{
63}
64
66m_language(language)
67{
68}
69
71{
72// for (WordFeatures::iterator it=begin(),it_end=end(); it!=it_end; it++) {
73// if (*it) {
74// delete (*it);
75// *it=0;
76// }
77// }
78}
79
80void WordFeatures::initialize(const deque<std::string>& featureNames)
81{
83 for (deque<std::string>::const_iterator it=featureNames.begin(),
84 it_end=featureNames.end();it!=it_end;it++)
85 {
86 string featureName=(*it);
87 string complement;
88 string::size_type i=featureName.find(":");
89 if (i!=string::npos) {
90 complement=string(featureName,i+1);
91 featureName=string(featureName,0,i);
92 }
93 try {
94 LDEBUG << "WordFeatures: initialize feature" << featureName;
95 push_back(FeatureLemmaFactory.getFactory(featureName)->create(m_language,complement));
96 back()->setName(featureName);
97 }
98 catch (LimaException& e) {
99 LERROR << "WordFeatures: feature" << featureName << "is not defined";
100 }
101
102 }
103}
104
105//***********************************************************************
106// Feature accessors
107//***********************************************************************
108FeaturePosition::FeaturePosition(MediaId language, const std::string& complement):
109AbstractFeatureExtractor(language,complement)
110{}
111
115 AnalysisContent & /*unused*/) const
116{
117 Token* token=get(vertex_token,*(graph->getGraph()),v);
118 if (token==0) {
119 return "";
120 }
121 ostringstream oss;
122 oss << token->position();
123 return oss.str();
124}
125
126//***********************************************************************
127FeatureToken::FeatureToken(MediaId language, const std::string& complement):
128AbstractFeatureExtractor(language,complement)
129{}
130
131std::string FeatureToken::
134 AnalysisContent & /*unused*/
135 ) const
136{
137 Token* token=get(vertex_token,*(graph->getGraph()),v);
138 if (token==0) {
139 return "";
140 }
142}
143
144//***********************************************************************
145FeatureInflectedForm::FeatureInflectedForm(MediaId language, const std::string& complement):
146AbstractFeatureExtractor(language,complement),
147m_sp()
148{
150}
151
155 AnalysisContent & /*unused*/
156 ) const
157{
158 MorphoSyntacticData* data=get(vertex_data,*(graph->getGraph()),v);
159 if (data==0) {
160 return "";
161 }
162 // take first
163 for (MorphoSyntacticData::const_iterator it=data->begin(),it_end=data->end();it!=it_end;it++) {
164 return Common::Misc::limastring2utf8stdstring((*m_sp)[(*it).inflectedForm]);
165 }
166 return "";
167}
168
169//***********************************************************************
170FeatureLemma::FeatureLemma(MediaId language, const std::string& complement):
171AbstractFeatureExtractor(language,complement),
172m_sp()
173{
175}
176
177std::string FeatureLemma::
180 AnalysisContent & /*unused*/
181 ) const
182{
183 MorphoSyntacticData* data=get(vertex_data,*(graph->getGraph()),v);
184 if (data==0) {
185 return "";
186 }
187 // take first
188 for (MorphoSyntacticData::const_iterator it=data->begin(),it_end=data->end();it!=it_end;it++) {
189 return Common::Misc::limastring2utf8stdstring((*m_sp)[(*it).normalizedForm]);
190 }
191 return "";
192}
193
194//***********************************************************************
195FeatureProperty::FeatureProperty(MediaId language,const std::string& complement):
197m_propertyName(complement),
198m_propertyAccessor(0),
199m_propertyManager(0)
200{
203 m_propertyAccessor=&codeManager.getPropertyAccessor(m_propertyName);
204 m_propertyManager=&codeManager.getPropertyManager(m_propertyName);
205}
206
210 AnalysisContent & /*unused*/) const
211{
212 MorphoSyntacticData* data=get(vertex_data,*(graph->getGraph()),v);
213 if (data==0) {
214 return "";
215 }
216 ostringstream oss;
217 // take first / take all ?
218 for (MorphoSyntacticData::const_iterator it=data->begin(),it_end=data->end();it!=it_end;it++) {
219 LinguisticCode code=m_propertyAccessor->readValue((*it).properties);
220 oss << m_propertyManager->getPropertySymbolicValue(code);
221 break;
222 }
223 return oss.str();
224}
225
226//***********************************************************************
227FeatureTstatus::FeatureTstatus(MediaId language, const std::string& complement):
228AbstractFeatureExtractor(language,complement)
229{}
230
234 AnalysisContent & /*unused*/) const
235{
236 Token* token=get(vertex_token,*(graph->getGraph()),v);
237 if (token==0) {
238 return "";
239 }
241}
242
243
244//***********************************************************************
245FeatureSpecificEntity::FeatureSpecificEntity(MediaId language, const std::string& complement):
246AbstractFeatureExtractor(language,complement)
247{
248}
249
253 AnalysisContent &analysis
254 ) const
255{
256 std::string typeName("NAN");
257 auto annot = std::dynamic_pointer_cast< Common::AnnotationGraphs::AnnotationData >(analysis.getData("AnnotationData"));
258
259 std::set< AnnotationGraphVertex > matches = annot->matches(graph->getGraphId(),v,"annot");
260 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin(); it != matches.end(); it++)
261 {
262 if (annot->hasAnnotation(*it, Common::Misc::utf8stdstring2limastring("SpecificEntity")))
263 {
265 const SpecificEntityAnnotation* se = annot->annotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")).
266 pointerValue<SpecificEntityAnnotation>();
267
270 }
271 }
272 return typeName;
273}
274
275//***********************************************************************
276FeatureLemmaSpecificEntity::FeatureLemmaSpecificEntity(MediaId language, const std::string& complement):
277AbstractFeatureExtractor(language,complement)
278{
279}
280
284 AnalysisContent &analysis
285) const
286{
287 std::string mxvalue("NAN");
288 auto annot = std::dynamic_pointer_cast< Common::AnnotationGraphs::AnnotationData >(analysis.getData("AnnotationData"));
289
290 std::set< AnnotationGraphVertex > matches = annot->matches(graph->getGraphId(),v,"annot");
291 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin(); it != matches.end(); it++)
292 {
293 if (annot->hasAnnotation(*it, Common::Misc::utf8stdstring2limastring("SpecificEntity")))
294 {
296 const SpecificEntityAnnotation* se = annot->annotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")).
297 pointerValue<SpecificEntityAnnotation>();
298
301 }
302 }
303 // replace NAN values by lemmas
304 if (mxvalue == "NAN") {
305 MorphoSyntacticData* data=get(vertex_data,*(graph->getGraph()),v);
306 // take first
307 auto it=data->begin(), it_end=data->end();
308 if (it != it_end) {
309 mxvalue = Common::Misc::limastring2utf8stdstring((*&(Common::MediaticData::MediaticData::single().stringsPool(m_language)))[(*it).normalizedForm]);
310 }
311 }
312 // replace empty lemma values by tokens
313 if (mxvalue == "" ) {
314 Token* token=get(vertex_token,*(graph->getGraph()),v);
316 }
317
318 return mxvalue;
319}
320
321//***********************************************************************
322FeatureStoredData::FeatureStoredData(MediaId language, const std::string& complement):
323AbstractFeatureExtractor(language,complement)
324{
325}
326
330 AnalysisContent &analysis) const
331{
332 LIMA_UNUSED(analysis)
333 Token* token=get(vertex_token,*(graph->getGraph()),v);
334 if (token==0) {
335 return "";
336 }
337 ostringstream oss;
338 oss << token->position() ;
339 return oss.str();
340}
341
342} // end namespace
343} // end namespace
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
@ vertex_data
#define DUMPERLOGINIT
#define FeatureLemmaSpecificEntity_ID
#define FeatureProperty_ID
#define FeatureTstatus_ID
#define FeatureLemma_ID
#define FeatureInflectedForm_ID
#define FeatureSpecificEntity_ID
#define FeaturePosition_ID
#define FeatureToken_ID
#define FeatureStoredData_ID
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
std::shared_ptr< AnalysisData > getData(const QString &id)
return AnalysisData by id
Holds linguistic data for one language.
const FsaStringsPool & stringsPool(MediaId med) const
const MediaData & mediaData(MediaId media) const
LimaString getEntityName(const EntityType &type) const
LinguisticCode readValue(const LinguisticCode &code) const
read a property in a coded int.
Provide tools to parse a property file, and deal with the property coding system.
const PropertyManager & getPropertyManager(const std::string &propertyName) const
Get the PropertyManager associated to a property.
const PropertyAccessor & getPropertyAccessor(const std::string &propertyName) const
Get the PropertyAccessor associated to a property.
const std::string & getPropertySymbolicValue(const LinguisticCode &value) const
The coded property value can hold several property data.
The main LIMA exception class.
Definition LimaCommon.h:262
AbstractFeatureExtractor(MediaId language, const std::string &complement="")
FeatureInflectedForm(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureLemmaSpecificEntity(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureLemma(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeaturePosition(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureProperty(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureSpecificEntity(MediaId language, const std::string &complement="")
FeatureStoredData(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureToken(MediaId language, const std::string &complement="")
std::string getValue(const LinguisticAnalysisStructure::AnalysisGraph *graph, LinguisticGraphVertex v, AnalysisContent &) const override
FeatureTstatus(MediaId language, const std::string &complement="")
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
A representation of a specific entity to store in the annotation graph.
void initialize(const std::deque< std::string > &featureNames)
Defines a Factory that automatically register to a MainFactory singleton.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
LimaString utf8stdstring2limastring(const std::string &src)
FeatureExtractorFactory< FeatureStoredData > FeatureStoredDataFactory(FeatureStoredData_ID)
FeatureExtractorFactory< FeatureTstatus > FeatureTstatusFactory(FeatureTstatus_ID)
FeatureExtractorFactory< FeatureProperty > FeaturePropertyFactory(FeatureProperty_ID)
FeatureExtractorFactory< FeaturePosition > FeaturePositionFactory(FeaturePosition_ID)
FeatureExtractorFactory< FeatureSpecificEntity > FeatureSpecificEntityFactory(FeatureSpecificEntity_ID)
FeatureExtractorFactory< FeatureToken > FeatureTokenFactory(FeatureToken_ID)
FeatureExtractorFactory< FeatureLemma > FeatureLemmaFactory(FeatureLemma_ID)
FeatureExtractorFactory< FeatureInflectedForm > FeatureInflectedFormFactory(FeatureInflectedForm_ID)
FeatureExtractorFactory< FeatureLemmaSpecificEntity > FeatureLemmaSpecificEntityFactory(FeatureLemmaSpecificEntity_ID)
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.