LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SimpleEventBuilder.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
16#include "SimpleEventBuilder.h"
17#include "EventData.h"
18
25#include <iostream>
26#include <queue>
27
28using namespace std;
29using namespace boost;
30using namespace boost::tuples;
31
32using namespace Lima::Common::MediaticData;
33using namespace Lima::Common::AnnotationGraphs;
34using namespace Lima::LinguisticProcessing;
38
39
40namespace Lima {
41namespace LinguisticProcessing {
42namespace EventAnalysis {
43
45
47m_segmData(),
48m_segmentType(),
49m_entities()
50{}
51
53 Manager* manager)
54
55{
56
58 MediaId language=manager->getInitializationParameters().media;
59 try
60 {
61 m_segmData=unitConfiguration.getParamsValueAtKey("segmentationData");
62 }
64 {
65 LERROR << "No segmentationData specified in "<<unitConfiguration.getName()
66 <<" configuration group for language " << language;
67 }
68
69 try
70 {
71 m_segmentType=unitConfiguration.getParamsValueAtKey("segmentType");
72 }
74 {
75 LERROR << "No segmentType specified in "<<unitConfiguration.getName()
76 <<" configuration group for language " << language;
77 }
78
79 try
80 {
81 deque<string> entities = unitConfiguration.getListsValueAtKey("entities");
82 for(deque<string>::iterator it=entities.begin(),it_end=entities.end();it!=it_end;it++)
83 {
84 try {
86 }
87 catch (LimaException& ) {
88 LERROR << "Error: unknown entity type '"<< *it << "' : ignored";
89 }
90 }
91 }
93 {
94 LERROR << "No entities specified in "<<unitConfiguration.getName()
95 <<" configuration group for language " << language;
96 }
97
98}
99
101{
104 LDEBUG << "start SimpleEventBuilder";
105
106 // get annotation data (for entities)
107 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
108 if (annotationData==0)
109 {
110 LERROR << "no annotation graph available !";
111 return MISSING_DATA;
112 }
113
114 // get segmentation data
115 auto data = analysis.getData(m_segmData);
116 if (data==0) {
117 LERROR << "Missing data '" << m_segmData << "'";
118 return MISSING_DATA;
119 }
120 auto segmData = std::dynamic_pointer_cast<SegmentationData>(data);
121 if (segmData==0)
122 {
123 LERROR << "Failed to interpret data '" << m_segmData << "' as SegmentationData";
124 return MISSING_DATA;
125 }
126
127 // get graph on which the segmentation data relies
128 string graphId=segmData->getGraphId();
129 auto graph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData(graphId));
130 if (graph==0) {
131 LERROR << "Cannot get graph '" << graphId << "' (from segmentation data)";
132 return MISSING_DATA;
133 }
134
135 EventData* eventData=new EventData;
136 LDEBUG << "set new data EventData of type EventData";
137 analysis.setData("EventData", eventData);
138
139 // get entities
140 map<Common::MediaticData::EntityType,vector<Entity> >& entities=eventData->getEntities();
141 // ??OME2 for (SegmentationData::const_iterator it=segmData->begin(),it_end=segmData->end();it!=it_end;it++) {
142 for (std::vector<Segment>::const_iterator it=(segmData->getSegments()).begin(),it_end=(segmData->getSegments()).end();it!=it_end;it++) {
143 if ((*it).getType()==m_segmentType) {
144 LDEBUG << "in segment " << m_segmentType << " [" << (*it).getPosBegin() << "," << (*it).getLength() << "]";
145 // get entities in this segment
146 getEntitiesFromSegment(entities, graph.get(), (*it).getFirstVertex(),(*it).getLastVertex(), annotationData.get());
147#ifdef DEBUG_LP
148 LDEBUG << "found " << entities.size() << " entities";
149#endif
150 }
151 else {
152#ifdef DEBUG_LP
153 LDEBUG << "ignored segment " << (*it).getType();
154#endif
155 }
156 }
157
158 // choose main entities : take first
159 for (map<Common::MediaticData::EntityType,vector<Entity> >::iterator it=entities.begin(),
160 it_end=entities.end();it!=it_end;it++) {
161 if ((*it).second.size()!=0) {
162#ifdef DEBUG_LP
163 LDEBUG << "set main for entity of type " << (*it).first << " at pos " << (*it).second[0].getPosition();
164#endif
165 (*it).second[0].setMain(true);
166 }
167 }
168
169
170 TimeUtils::logElapsedTime("SimpleEventBuilder");
171 return SUCCESS_ID;
172}
173
174void SimpleEventBuilder::getEntitiesFromSegment(map<Common::MediaticData::EntityType,vector<Entity> >& entities,
175 AnalysisGraph* anagraph,
176 LinguisticGraphVertex firstVertex,
177 LinguisticGraphVertex lastVertex,
178 const Common::AnnotationGraphs::AnnotationData* annotationData) const
179{
180 LinguisticGraph* graph=anagraph->getGraph();
181 std::queue<LinguisticGraphVertex> toVisit;
182 std::set<LinguisticGraphVertex> visited;
183 LinguisticGraphOutEdgeIt outItr,outItrEnd;
184
185 // get entities by going through the graph
186 toVisit.push(firstVertex);
187
188 bool first=true;
189 bool last=false;
190 while (!toVisit.empty()) {
191 LinguisticGraphVertex v=toVisit.front();
192 toVisit.pop();
193 if (last || v == anagraph->lastVertex()) {
194 continue;
195 }
196 if (v == lastVertex) {
197 last=true;
198 }
199
200 for (boost::tie(outItr,outItrEnd)=out_edges(v,*graph); outItr!=outItrEnd; outItr++)
201 {
202 LinguisticGraphVertex next=target(*outItr,*graph);
203 if (visited.find(next)==visited.end())
204 {
205 visited.insert(next);
206 toVisit.push(next);
207 }
208 }
209
210 if (first) {
211 first=false;
212 }
213 else {
214 // check if is entity
215 const SpecificEntityAnnotation* se=getSpecificEntity(anagraph,annotationData,v);
216 if (se!=0) {
217 entities[se->getType()].push_back(Entity(se->getPosition(),se->getLength(),se->getFeatures()));
219 LDEBUG << "inserted entity of type " << se->getType() << " at pos " << se->getPosition() << " with main=" << entities[se->getType()].back().getMain();
220 }
221 }
222 }
223}
224
225const SpecificEntityAnnotation* SimpleEventBuilder::
226getSpecificEntity(AnalysisGraph* graph,
227 const Common::AnnotationGraphs::AnnotationData* annotationData,
228 LinguisticGraphVertex v) const
229{
231 // then check if vertex corresponds to a specific entity found after POS tagging
232 std::set< AnnotationGraphVertex > matches = annotationData->matches(graph->getGraphId(),v,"annot");
233 LDEBUG << "vertex " << v << " has " << matches.size() << " annotations";
234 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
235 it != matches.end(); it++)
236 {
238 if (annotationData->hasAnnotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")))
239 {
240 //BoWToken* se = createSpecificEntity(v,*it, annotationData, anagraph, posgraph, offsetBegin);
241 const SpecificEntityAnnotation* se =
242 annotationData->annotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")).
243 pointerValue<SpecificEntityAnnotation>();
244 return se;
245 }
246 }
247 LDEBUG << "vertex " << v << ": no entity in graph " << graph->getGraphId();
248 return 0;
249}
250
251} // end namespace
252} // end namespace
253} // end namespace
#define LDEBUG
Definition LimaCommon.h:157
#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 EVENTANALYZERLOGINIT
#define SIMPLEEVENTBUILDER_CLASSID
Defines a Factory to create Object of type Base.
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 an annotation graph and gives an API to manipulate it.
std::set< AnnotationGraphVertex > matches(const std::string &first, AnnotationGraphVertex firstVx, const std::string &second) const
Gets the set of vertices matched in the second graph by the given vertex of the first graph.
const GenericAnnotation & annotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
EntityType getEntityType(const LimaString &entityName) const
entity types manager
std::deque< std::string > & getListsValueAtKey(const std::string &key)
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
The main LIMA exception class.
Definition LimaCommon.h:262
std::map< Common::MediaticData::EntityType, std::vector< Entity > > & getEntities()
Definition EventData.h:49
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
A representation of a specific entity to store in the annotation graph.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static void logElapsedTime(const std::string &mess, const std::string &taskCategory=std::string(""))
log the number of microseconds since last UpdateCurrentTime
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
bool hasAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< MediaProcessUnit, SimpleEventBuilder > SimpleEventBuilderFactory(SIMPLEEVENTBUILDER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.
static const struct TextHtmlEntity entities[]