LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
CompoundsBuilderFromSyntacticData.cpp
Go to the documentation of this file.
1// Copyright 2002-2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
15
17
18//using namespace boost;
19using namespace Lima::Common::AnnotationGraphs;
22
23namespace Lima
24{
25namespace LinguisticProcessing
26{
27namespace Compounds
28{
29
31
32
34{
36
38
40
41 void initConceptTypes(Common::XMLConfigurationFiles::GroupConfigurationStructure& unitConfiguration) ;
42 void initMacros2ConceptsMapping(Common::XMLConfigurationFiles::GroupConfigurationStructure& unitConfiguration);
43 void initCompoundRels(Common::XMLConfigurationFiles::GroupConfigurationStructure& unitConfiguration);
44
45 uint64_t m_depGraphMaxBranchingFactor;
46 MediaId m_language;
47 std::map< std::string, Lima::Common::MediaticData::ConceptType > m_conceptTypes;
48 std::map< Lima::Common::MediaticData::ConceptType, std::string > m_conceptNames;
49
50 std::map< LinguisticCode, Lima::Common::MediaticData::ConceptType > m_macro2ConceptMapping;
51 std::set< std::string > m_compoundsRels;
52};
53
54CompoundsBuilderFromSyntacticDataPrivate::CompoundsBuilderFromSyntacticDataPrivate()
55{}
56
57
58CompoundsBuilderFromSyntacticDataPrivate::~CompoundsBuilderFromSyntacticDataPrivate()
59{}
60
65
66
71
74 Manager* manager)
75
76{
77// LIMA_UNUSED(unitConfiguration);
78// MORPHOLOGINIT;
79 m_d->m_language=manager->getInitializationParameters().media;
80 m_d->initConceptTypes(unitConfiguration);
81 m_d->initCompoundRels(unitConfiguration);
82 m_d->initMacros2ConceptsMapping(unitConfiguration);
83}
84
85void CompoundsBuilderFromSyntacticDataPrivate::initCompoundRels(
87{
89 LINFO << "init compounds relations";
90 try
91 {
92 // loads syntactic analysis resources
93 std::deque< std::string > compoundsRelsList = unitConfiguration.getListsValueAtKey("CompoundsRels");
94 m_compoundsRels.insert(compoundsRelsList.begin(), compoundsRelsList.end());
95 }
96 catch (NoSuchList& )
97 {
98 LERROR << "No list 'CompoundsRels' in 'SyntacticAnalysis' group for language " << (int)m_language;
100 }
101 catch (NoSuchGroup& )
102 {
103 LERROR << "No group 'SyntacticAnalysis' in common language configuration file for language " << (int)m_language;
104 throw InvalidConfiguration();
105 }
106}
107
109{
110 return (m_d->m_compoundsRels.find(rel) != m_d->m_compoundsRels.end());
111}
112
113void CompoundsBuilderFromSyntacticDataPrivate::initConceptTypes(
114 GroupConfigurationStructure& unitConfiguration)
115{
116#ifdef DEBUG_LP
118 LINFO << "CompoundsBuilderFromSyntacticDataPrivate::initConceptTypes";
119#endif
120
121 try
122 {
123 const auto& mapping= unitConfiguration.getMapAtKey("conceptTypes");
124#if __cplusplus >= 201703L // Only available with c++17 extensions : -std=c++1z
125 for (const auto& [key, val] : mapping)
126 {
127#else
128 for (std::map<std::string,std::string>::const_iterator it=mapping.begin();
129 it!=mapping.end();
130 it++)
131 {
132 const std::string key = it->first;
133 const std::string val = it->second;
134#endif
135 auto type = static_cast<Lima::Common::MediaticData::ConceptType>(atoi(val.c_str()));
136#ifdef DEBUG_LP
137 LDEBUG << "read concept type " << key.c_str() << " -> " << type;
138#endif
139 m_conceptTypes[key] = type;
140 m_conceptNames[type] = key;
141 }
142
143 }
144 catch (NoSuchMap& e)
145 {
147 LERROR << "No map 'conceptTypes' in configuration file:" << e.what();
148 throw InvalidConfiguration();
149 }
150}
151
152Lima::Common::MediaticData::ConceptType CompoundsBuilderFromSyntacticData::getConceptType(const std::string& typeName) const
153{
154 if (m_d->m_conceptTypes.find(typeName)==m_d->m_conceptTypes.end())
155 {
157 LERROR << "Concept type name " << typeName.c_str() << " not found. Returning value for LatticeDown (should be '0').";
158 return (*(m_d->m_conceptTypes.find("LatticeDown"))).second;
159 }
160 else
161 {
162 return (*(m_d->m_conceptTypes.find(typeName))).second;
163 }
164}
165
166
167const std::string& CompoundsBuilderFromSyntacticData::getConceptName(const Lima::Common::MediaticData::ConceptType& type) const
168{
169 if (m_d->m_conceptNames.find(type)==m_d->m_conceptNames.end())
170 {
172 LERROR << "Concept type " << type << " not found. Returning for 0 (should be 'LatticeDown').";
173 return (*(m_d->m_conceptNames.find(static_cast<Lima::Common::MediaticData::ConceptType>(0)))).second;
174 }
175 else
176 {
177 return (*(m_d->m_conceptNames.find(type))).second;
178 }
179}
180
181void CompoundsBuilderFromSyntacticDataPrivate::initMacros2ConceptsMapping(
183{
184#ifdef DEBUG_LP
186 LDEBUG << "Initializing macro to concepts mapping:";
187#endif
188 try
189 {
190 const std::map<std::string,std::string>& mapping = macro2ConceptsGroup.getMapAtKey("Macros2ConceptTypes");
191
193 const Lima::Common::PropertyCode::PropertyCodeManager& propCodeManager = languageData.getPropertyCodeManager();
194
196 propCodeManager.getPropertyManager("MACRO");
197
198 for(auto it = mapping.begin(), it_end = mapping.end(); it != it_end; it++)
199 {
200 LinguisticCode macro = macroManager.getPropertyValue((*it).first);
201 std::string typeName = (*it).second;
202 Lima::Common::MediaticData::ConceptType semanticConcept = (*(m_conceptTypes.find("LatticeDown"))).second;
203 if (m_conceptTypes.find(typeName) == m_conceptTypes.end())
204 {
206 LERROR << "Concept type name " << typeName.c_str() << " not found. Keeping LatticeDown (should be '0').";
207 }
208 else
209 {
210 semanticConcept = (*(m_conceptTypes.find(typeName))).second;
211 }
212
213#ifdef DEBUG_LP
214 LDEBUG << " " << (*it).first << " (" << macro << ") <-> "
215 << (*it).second << " (" << semanticConcept << ")";
216#endif
217 m_macro2ConceptMapping.insert(std::make_pair(macro, semanticConcept));
218 }
219
220 }
221 catch (NoSuchMap& )
222 {
224 LERROR << "No map 'Macros2ConceptTypes' for group 'SemanticData' in common "
225 "language configuration file for language " << m_language;
226 throw InvalidConfiguration();
227 }
228 catch (NoSuchGroup& )
229 {
231 LERROR << "No group 'SemanticData' in common language configuration file "
232 "for language " << m_language;
233 throw InvalidConfiguration();
234 }
235}
236
237
238Lima::Common::MediaticData::ConceptType CompoundsBuilderFromSyntacticData::getConceptForMacro(const LinguisticCode& macro) const
239{
240#ifdef DEBUG_LP
242 LDEBUG << "CompoundsBuilderFromSyntacticData::getConceptForMacro" << macro;
243#endif
246
247 if (m_d->m_macro2ConceptMapping.find(macro) == m_d->m_macro2ConceptMapping.end())
248 {
249#ifdef DEBUG_LP
251 LDEBUG << "CompoundsBuilderFromSyntacticData::getConceptForMacro" << macro << "not found. Returning for" << SYMBOLIC_NONE_1 << "(should be 0, 'LatticeDown').";
252#endif
253 const Lima::Common::PropertyCode::PropertyManager& macroManager = propertycodeManager.getPropertyManager("MACRO");
255 if (m_d->m_macro2ConceptMapping.find(none) == m_d->m_macro2ConceptMapping.end())
256 {
257#ifdef DEBUG_LP
259 LDEBUG << "SYMBOLIC_NONE_1 Not found !!! Returning 0";
260#endif
261 return static_cast<Lima::Common::MediaticData::ConceptType>(0);
262 }
263 else
264 {
265#ifdef DEBUG_LP
266 LDEBUG << "SYMBOLIC_NONE_1 Found !!! Returning " << (*(m_d->m_macro2ConceptMapping.find(none))).second;
267#endif
268 return (*(m_d->m_macro2ConceptMapping.find(none))).second;
269 }
270 }
271 else
272 {
273#ifdef DEBUG_LP
274 LDEBUG << "CompoundsBuilderFromSyntacticData::getConceptForMacro Found !!! Returning " << (*(m_d->m_macro2ConceptMapping.find(macro))).second;
275#endif
276 return (*(m_d->m_macro2ConceptMapping.find(macro))).second;
277 }
278
279}
280
282 AnalysisContent& analysis) const
283{
284 Lima::TimeUtilsController timer("CompoundsBuilderFromSyntacticData");
286#ifdef DEBUG_LP
287 LDEBUG << "CompoundsBuilderFromSyntacticData::process : start";
288#endif
289
290 auto tmp=analysis.getData("SyntacticData");
291 if (tmp==0)
292 {
293 LERROR << "Can't Process CompoundsBuilderFromSyntacticData : missing data 'SyntacticData'";
294 return MISSING_DATA;
295 }
296 auto data = std::dynamic_pointer_cast<SyntacticData>(tmp);
297
298 tmp = analysis.getData("PosGraph");
299 if (tmp==0)
300 {
301 LERROR << "Can't Process CompoundsBuilderFromSyntacticData : missing data 'AnalysisGraph'";
302 return MISSING_DATA;
303 }
304 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(tmp);
305
306 tmp=analysis.getData("SentenceBoundaries");
307 if (tmp==0)
308 {
309 LERROR << "can't process CompoundsBuilderFromSyntacticData : missing SentenceBounds !";
310 return MISSING_DATA;
311 }
312 auto sb = std::dynamic_pointer_cast<SegmentationData>(tmp);
313 if (sb->getGraphId() != "PosGraph") {
314 LERROR << "SentenceBounds have been computed on " << sb->getGraphId() << " !";
315 LERROR << "SyntacticAnalyzer-deps needs SentenceBounds on PosGraph";
317 }
318 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
319 if (annotationData==0)
320 {
321 annotationData = std::make_shared<AnnotationData>();
322 if (std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("AnalysisGraph")) != 0)
323 {
324 std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("AnalysisGraph"))->populateAnnotationGraph(
325 annotationData.get(), "AnalysisGraph");
326 }
327 if (std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph")) != 0)
328 {
329 std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph"))->populateAnnotationGraph(
330 annotationData.get(), "PosGraph");
331 }
332
333 analysis.setData("AnnotationData",annotationData);
334 }
335
336 const LinguisticGraph& graph=*(anagraph->getGraph());
337
338 //LinguisticGraphVertex beginSentence=sb->getStartVertex();
339 // ??OME2 for (SegmentationData::const_iterator boundItr=sb->begin();
340 // boundItr!=sb->end();
341 for (const auto& bound: sb->getSegments())
342 {
343 LinguisticGraphVertex beginSentence=bound.getFirstVertex();
344 LinguisticGraphVertex endSentence=bound.getLastVertex();
345
346#ifdef DEBUG_LP
347 LDEBUG << "process sentence from " << beginSentence << " to " << endSentence;
348 #endif
350 this,
351 m_d->m_language,
352 data.get(),
353 data->dependencyGraph() ,
354 anagraph.get(),
355 beginSentence,
356 endSentence,
357 annotationData.get());
358
359 // don't use STL algorithm to avoid throwing exceptions
360 LinguisticGraphVertex firstVx = anagraph->firstVertex();
361 LinguisticGraphVertex lastVx = anagraph->lastVertex();
362
363 std::set<LinguisticGraphVertex> visited;
364 std::queue<LinguisticGraphVertex> toVisit;
365 toVisit.push(beginSentence);
366
367 LinguisticGraphOutEdgeIt outItr,outItrEnd;
368 while (!toVisit.empty())
369 {
370 LinguisticGraphVertex v=toVisit.front();
371 toVisit.pop();
372
373 if (v != firstVx &&
374 v != lastVx)
375 {
376 vis.discover_vertex(v);
377 }
378
379 if (v == endSentence) {
380 continue;
381 }
382
383 for (boost::tie(outItr,outItrEnd)=out_edges(v,graph);
384 outItr!=outItrEnd;
385 outItr++)
386 {
387 LinguisticGraphVertex next=target(*outItr,graph);
388 if (visited.find(next)==visited.end())
389 {
390 visited.insert(next);
391 toVisit.push(next);
392 }
393 }
394 }
395
396// LDEBUG << compoundsItr->size() << " compounds built ! ";
397
398 //beginSentence=*boundItr;
399 }
400 return SUCCESS_ID;
401}
402
403
404}
405
406}
407
408}
This file is the main header file for the data related to annotation graphs.
#define COMPOUNDSBUILDERFROMSYNTACTICDATAFACTORY_CLASSID
#define MDATALOGINIT
Definition LimaCommon.h:209
#define LDATALOGINIT
Definition LimaCommon.h:208
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
LinguisticGraph::out_edge_iterator LinguisticGraphOutEdgeIt
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define MORPHOLOGINIT
Defines a Factory to create Object of type Base.
Data used for the syntactic analyzis of texts.
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
void setData(const QString &id, std::shared_ptr< AnalysisData > data)
set an analysisData with the given id.
Holds linguistic data for one language.
const PropertyCode::PropertyCodeManager & getPropertyCodeManager() const
const MediaData & mediaData(MediaId media) const
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.
Provide tools to manage a specific property.
LinguisticCode getPropertyValue(const std::string &symbolicValue) const
Get the coded property value from the symbolic value.
std::deque< std::string > & getListsValueAtKey(const std::string &key)
std::map< std::string, std::string > & getMapAtKey(const std::string &key)
return a message when a 'group' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
virtual const char * what() const override
Definition LimaCommon.h:280
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
const std::string & getConceptName(const Lima::Common::MediaticData::ConceptType &type) const
Lima::Common::MediaticData::ConceptType getConceptType(const std::string &typeName) const
Lima::Common::MediaticData::ConceptType getConceptForMacro(const LinguisticCode &macro) const
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
This file contains a class to control log of informations about time, such as logging cumulated time ...
#define SYMBOLIC_NONE_1
SimpleFactory< MediaProcessUnit, CompoundsBuilderFromSyntacticData > compoundsBuilderFromSyntacticDataFactory(COMPOUNDSBUILDERFROMSYNTACTICDATAFACTORY_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ INVALID_CONFIGURATION
Definition LimaCommon.h:242
@ MISSING_DATA
Definition LimaCommon.h:243