LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EventXmlLogger.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 EventXmlLogger.cpp
9 * @author Romaric Besancon (romaric.besancon@cea.fr)
10 * @date Thu Mar 24 2011
11 * copyright Copyright (C) 2011 by CEA LIST
12 *
13 ***********************************************************************/
14
19#include "EventXmlLogger.h"
20#include "EventTemplateData.h"
21#include <boost/algorithm/string.hpp>
22
23using namespace std;
24using namespace Lima::Common;
25using namespace Lima::Common::AnnotationGraphs;
26
27namespace Lima {
28namespace LinguisticProcessing {
29namespace EventAnalysis {
30
33
34//***********************************************************************
35// constructors and destructors
38m_eventData("EventData")
39{
40}
41
45
46//***********************************************************************
48 Manager* manager)
49
50{
52 LDEBUG << "EventXmlLogger::init";
53
54 AbstractTextualAnalysisDumper::init(unitConfiguration,manager);
55
56 try {
57 m_eventData=unitConfiguration.getParamsValueAtKey("eventData");
58 }
59 catch (Common::XMLConfigurationFiles::NoSuchParam& ) {} // do nothing, keep default
60}
61
63{
65 LDEBUG << "EventXmlLogger::process";
67
68 // initialize output
69 auto dstream=AbstractTextualAnalysisDumper::initialize(analysis);
70 ostream& out=dstream->out();
71
72 if (! m_eventData.empty()) {
73 auto data = analysis.getData(m_eventData);
74 if (data!=0) {
75 // see if the data is of type Events
76 auto events = std::dynamic_pointer_cast<const Events>(data);
77 if (events!=0) {
78 LDEBUG << "data '" << m_eventData << "' is of type Events";
79 outputEvents(out, events.get());
80 }
81 else {
82 // see if the data is of type EventData
83 auto eventData = std::dynamic_pointer_cast<const EventData>(data);
84 if (eventData!=0) {
85 LDEBUG << "data '" << m_eventData << "' is of type EventData";
86 outputEventData(out, eventData.get());
87 }
88 else {
89 // see if data is of type EventTemplateData
90 auto eventTemplateData = std::dynamic_pointer_cast<const EventTemplateData>(data);
91 if (eventTemplateData!=0) {
92 LDEBUG << "data '" << m_eventData << "' is of type EventTemplateData";
93 // output it converted to Events (need annotation data)
94 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
95 if (annotationData==0)
96 {
97 LERROR << "no annotation graph available !";
98 return MISSING_DATA;
99 }
100
101 outputEvents(out, eventTemplateData->convertToEvents(annotationData.get()));
102 }
103 else {
105 LERROR << "data '" << m_eventData << "' is neither of type EventData nor Events";
106 }
107 }
108 }
109 }
110 else {
112 LERROR << "no data of name " << m_eventData;
113 }
114 }
115 TimeUtils::logElapsedTime("EventXmlLogger");
116 return SUCCESS_ID;
117}
118
119void EventXmlLogger::outputEvents(std::ostream& out,
120 const Events* eventData) const
121{
122 int i=0;
123 out << "<events>" << endl;
124 for (std::vector<Event*>::const_iterator iT= eventData->begin(); iT!= eventData->end();iT++)
125 {
126 i++;
127 out << "<event id=\"" << i << "\""
128 << " w=\"" << (*iT)->get_weight() << "\""
129 << " main=\"" << (*iT)->getMain() << "\">" << endl;
130 out << "<segments>" << endl;
131 int j=0;
132 for(std::vector<EventParagraph*>::const_iterator iT1= (*iT)->begin(); iT1!= (*iT)->end();iT1++)
133 {
134 out << " <paragraph id=\"" << j << "\" pos=\"" << (*iT1)->getPosition() << "\" len=\"" << (*iT1)->getLength() <<"\">" << endl;
135 std::map<Common::MediaticData::EntityType,std::vector<Entity *> > otherEntities=(*iT1)->getOtherEntities();
136 out << " <entities>" << endl;
137 for(std::map<Common::MediaticData::EntityType,std::vector<Entity *> >::const_iterator iT2= otherEntities.begin(); iT2!= otherEntities.end();iT2++)
138 {
139 //out << " <entity type=\"" << (*iT2).first << "\">" << endl;
140 std::string entityTypeName=Common::Misc::limastring2utf8stdstring(MediaticData::MediaticData::single().getEntityName((*iT2).first));
141 out << " <entity type=\"" << entityTypeName << "\">" << endl;
142 for(std::vector<Entity *>::const_iterator iT3=(*iT2).second.begin();iT3!=(*iT2).second.end();iT3++)
143 {
144 outputEntity(out,**iT3);
145 }
146 out << " </entity>" << endl;
147 }
148 std::pair<Common::MediaticData::EntityType,std::vector<Entity *> > eventEntities=(*iT1)->getEventEntities();
149 if (! eventEntities.second.empty()) {
150 out << " <entity type=\"" << eventEntities.first << "\">" << endl;
151 for(std::vector<Entity *>::const_iterator iT3=eventEntities.second.begin();iT3!=eventEntities.second.end();iT3++)
152 {
153 outputEntity(out,**iT3);
154 }
155 out << " </entity>" << endl;
156 }
157 out << " </entities>" << endl;
158 out << " </paragraph>" << endl;
159 j++;
160 }
161 out << "</segments>" << endl;
162 out << "</event>" << endl;
163 }
164 out << "</events>" << endl;
165}
166
167void EventXmlLogger::outputEventData(std::ostream& out,
168 const EventData* eventData) const
169{
170 out << "<events>" << endl;
171 out << "<event id=\"" << 1 << "\""
172 << " w=\"" << 1 << "\""
173 << " main=\"true\">" << endl;
174 out << " <entities>" << endl;
175 const map<Common::MediaticData::EntityType,vector<Entity> >& entities=eventData->getEntities();
176 for (map<Common::MediaticData::EntityType,vector<Entity> >::const_iterator
177 it=entities.begin(),it_end=entities.end();it!=it_end;it++) {
178 out << " <entity type=\"" << Misc::limastring2utf8stdstring(MediaticData::MediaticData::single().getEntityName((*it).first)) << "\">" << endl;
179 for(std::vector<Entity>::const_iterator e=(*it).second.begin(),e_end=(*it).second.end();e!=e_end;e++)
180 {
181 outputEntity(out,*e);
182 }
183 out << " </entity>" << endl;
184 }
185 out << " </entities>" << endl;
186
187 out << "</event>" << endl;
188 out << "</events>" << endl;
189}
190
191void EventXmlLogger::outputEntity(std::ostream& out, const Entity& e) const
192{
193 out << " <entityOccurrence"
194 << " pos=\"" << e.getPosition() << "\""
195 << " len=\"" <<e.getLength() << "\"";
196 string str(""),norm(""),role("");
197 const Automaton::EntityFeatures& features=e.getFeatures();
198 for (Automaton::EntityFeatures::const_iterator
199 featureItr=features.begin(),features_end=features.end();
200 featureItr!=features_end; featureItr++)
201 {
202 if (featureItr->getName()=="value") {
203 str=xmlString(featureItr->getValueString());
204 //protect xml entities
205 }
206 else if (featureItr->getName()=="role") {
207 role=xmlString(featureItr->getValueString());
208 }
209 else {
210 norm=xmlString(featureItr->getValueString());
211 }
212 }
213 if (! role.empty()) {
214 out << " role=\"" << role << "\"";
215 }
216 if (! str.empty()) {
217 out << " string=\"" << str << "\"";
218 out << " norm=\"" << str << "\"";
219 if (e.getMain()) {
220 out << " main=\"true\"";
221 }
222 if (! norm.empty()) {
223 out << ">" << norm ;
224 }
225 else {
226 out << ">" << str ;
227 }
228 }
229 else {
230 if (e.getMain()) {
231 out << " main=\"true\"";
232 }
233 out << ">";
234 }
235 out << "</entityOccurrence>" << endl;
236}
237
238std::string EventXmlLogger::xmlString(const std::string& inputStr) const
239{
240 // protect XML entities
241 std::string str(inputStr);
242 boost::replace_all(str,"&", "&amp;");
243 boost::replace_all(str,"<", "&lt;");
244 boost::replace_all(str,">", "&gt;");
245 boost::replace_all(str,"\"", "&quot;");
246 boost::replace_all(str,"\n", "\n");
247 return str;
248}
249
250
251} // end namespace
252} // end namespace
253} // end namespace
#define EVENTXMLLOGGER_CLASSID
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define EVENTANALYSISLOGINIT
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
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
std::shared_ptr< DumperStream > initialize(AnalysisContent &analysis) const
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual void init(Lima::Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
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
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
SimpleFactory< MediaProcessUnit, EventXmlLogger > EventXmlLoggerFactory(EVENTXMLLOGGER_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[]