LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EventTemplateDataXmlLogger.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 EventTemplateDataXmlLogger.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
24
25#include <boost/algorithm/string.hpp>
26
27using namespace std;
28using namespace Lima::Common;
29using namespace Lima::Common::AnnotationGraphs;
31using namespace boost;
32
33namespace Lima {
34namespace LinguisticProcessing {
35namespace EventAnalysis {
36
39
40//***********************************************************************
41// constructors and destructors
47
51
52//***********************************************************************
54 Manager* manager)
55
56{
58 LDEBUG << "EventTemplateDataXmlLogger::init";
59
60 AbstractTextualAnalysisDumper::init(unitConfiguration,manager);
61
62 try {
63 m_eventData=unitConfiguration.getParamsValueAtKey("eventTemplateData");
64 }
66 LDEBUG << "EventTemplateDataXmlLogger: no parameter 'eventTemplateData', use default ('"<<m_eventData << "')";
67 // not an error, keep default
68 }
69}
70
72{
74 LDEBUG << "EventTemplateDataXmlLogger::process";
76
77 // initialize output
78 auto dstream=AbstractTextualAnalysisDumper::initialize(analysis);
79 ostream& out=dstream->out();
80
81 const auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
82 if (annotationData==0)
83 {
84 LERROR << "no annotation graph available !";
85 return MISSING_DATA;
86 }
87
88 if (! m_eventData.empty()) {
89 auto data = analysis.getData(m_eventData);
90 if (data!=0) {
91 // see if the data is of type Events
92 auto eventData = std::dynamic_pointer_cast<const EventTemplateData>(data);
93 if (eventData==0) {
95 LERROR << "data '" << m_eventData << "' is neither of type EventData nor Events";
96 return MISSING_DATA;
97 }
98 else {
99 outputEventData(out, eventData.get(), annotationData.get());
100 }
101 }
102 else {
104 LERROR << "no data of name " << m_eventData;
105 }
106 }
107 TimeUtils::logElapsedTime("EventTemplateDataXmlLogger");
108 return SUCCESS_ID;
109}
110
111void EventTemplateDataXmlLogger::outputEventData(std::ostream& out,
112 const EventTemplateData* eventData,
113 const AnnotationData* annotationData) const
114{
115 int i=0;
116 out << "<events>" << endl;
117 for (std::vector<EventTemplate>::const_iterator it= eventData->begin(); it!= eventData->end();it++)
118 {
119 const TemplateElements& templateElements=(*it).getTemplateElements();
120 if (! templateElements.empty()) {
121 i++;
122 out << " <event id=\"" << i << "\""
123 << " w=\"" << (*it).getWeight() << "\""
124 << " main=\"" << (*it).isMainEvent() << "\""
125 << " type=\"" << (*it).getType() << "\">"
126 << endl;
127 int j=0;
128 out << " <entities>" << endl;
129 for(map<string,EventTemplateElement>::const_iterator it1= templateElements.begin(); it1!= templateElements.end();it1++)
130 {
131 string typeName=Common::Misc::limastring2utf8stdstring(MediaticData::MediaticData::single().getEntityName((*it1).second.getType()));
132 out << " <entity role=\"" << (*it1).first << "\""
133 << " type=\"" << typeName << "\">"
134 << endl;
135 const LinguisticAnalysisStructure::AnalysisGraph* graph=(*it1).second.getGraph();
136 LinguisticGraphVertex v=(*it1).second.getVertex();
137 outputEntity(out, graph, v,annotationData);
138 out << " </entity>" << endl;
139 j++;
140 }
141 out << " </entities>" << endl;
142 out << " </event>" << endl;
143 }
144 }
145 out << "</events>" << endl;
146}
147
148
149void EventTemplateDataXmlLogger::outputEntity(std::ostream& out,
152 const AnnotationData* annotationData) const
153{
154 LinguisticAnalysisStructure::Token* token=get(vertex_token, *(graph->getGraph()), v);
155 if (token==0) {
156 EVENTANALYSISLOGINIT; LWARN << "EventTemplateDataXmlLogger: no token for vertex " << v;
157 return;
158 }
159 out << " <entityOccurrence"
160 << " pos=\"" << token->position() << "\""
161 << " len=\"" << token->length() << "\"";
162
163 string str(""),norm("");
164 str=xmlString(Common::Misc::limastring2utf8stdstring(token->stringForm()));
165
166 // check if vertex corresponds to a specific entity found
167 std::set< AnnotationGraphVertex > matches = annotationData->matches(graph->getGraphId(),v,"annot");
168 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
169 it != matches.end(); it++)
170 {
172 if (annotationData->hasAnnotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")))
173 {
174 const SpecificEntityAnnotation* se =
175 annotationData->annotation(vx, Common::Misc::utf8stdstring2limastring("SpecificEntity")).
176 pointerValue<SpecificEntityAnnotation>();
177
178 const Automaton::EntityFeatures& features=se->getFeatures();
179 for (Automaton::EntityFeatures::const_iterator
180 featureItr=features.begin(),features_end=features.end();
181 featureItr!=features_end; featureItr++)
182 {
183 if (featureItr->getName()=="value") {
184 norm=xmlString(featureItr->getValueString());
185 }
186 }
187 }
188 }
189
190 if (! str.empty()) {
191 out << " string=\"" << str << "\"";
192 out << " norm=\"" << norm << "\"";
193 out << ">" << str ;
194 }
195 out << "</entityOccurrence>" << endl;
196}
197
198std::string EventTemplateDataXmlLogger::xmlString(const std::string& inputStr) const
199{
200 // protect XML entities
201 std::string str(inputStr);
202 boost::replace_all(str,"&", "&amp;");
203 boost::replace_all(str,"<", "&lt;");
204 boost::replace_all(str,">", "&gt;");
205 boost::replace_all(str,"\"", "&quot;");
206 boost::replace_all(str,"\n", "\n");
207 return str;
208}
209
210
211} // end namespace
212} // end namespace
213} // end namespace
This file is the main header file for the data related to annotation graphs.
#define EVENTTEMPLATEDATAXMLLOGGER_CLASSID
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
#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
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
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.
a list of generic features: each feature is unique (only one feature for a name)
virtual LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
virtual 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 LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
A representation of a specific entity to store in the annotation graph.
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
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)
std::multimap< std::string, EventTemplateElement > TemplateElements
SimpleFactory< MediaProcessUnit, EventTemplateDataXmlLogger > EventTemplateDataXmlLoggerFactory(EVENTTEMPLATEDATAXMLLOGGER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.