LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
AnnotationGraphXmlDumper.cpp
Go to the documentation of this file.
1// Copyright 2002-2019 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
22
30// #include "linguisticProcessing/core/LinguisticProcessors/HandlerStreamBuf.h"
34
35#include <fstream>
36
37using namespace boost;
38using namespace boost::tuples;
39
40typedef color_traits<default_color_type> Color;
41
42namespace Lima {
43 using namespace Common::XMLConfigurationFiles;
44 using namespace Common::MediaticData;
45 using namespace Common::AnnotationGraphs;
46namespace LinguisticProcessing {
47namespace AnalysisDumpers {
48
49
50//***********************************************************************
51// constructors
52//***********************************************************************
54
59
60AnnotationGraphXmlDumper::AnnotationGraphXmlDumper( const MediaId& language, const std::string& handler )
61 : m_language(language), m_handler( handler)
62 {}
63
67
70 Manager* manager)
71{
72// DUMPERLOGINIT;
74/* try
75 {
76 m_dumpFullTokens = (unitConfiguration.getParamsValueAtKey("dumpFullTokens") == "true");
77 }
78 catch (NoSuchParam& )
79 {
80 LWARN << "dumpFullTokens parameter not found, using default: "
81 << (m_dumpFullTokens?"true":"false");
82 }*/
83 try
84 {
85 m_handler=unitConfiguration.getParamsValueAtKey("handler");
86 }
87 catch (NoSuchParam& )
88 {
90 LERROR << "AnnotationGraphXmlDumper::init: Missing parameter handler in AnnotationGraphXmlDumper configuration";
92 }
93}
94
96 AnalysisContent& analysis) const
97{
98 std::string prologue("<?xml version='1.0' encoding='UTF-8'?>");
99 return dump( analysis, prologue );
100}
101
103 AnalysisContent& analysis, const std::string& prologue ) const
104{
105 TimeUtilsController timer("AnnotationGraphXmlDumper");
107 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
108 if (metadata == 0) {
109 LERROR << "AnnotationGraphXmlDumper::process: no LinguisticMetaData ! abort";
110 return MISSING_DATA;
111 }
112 LDEBUG << "handler will be: " << m_handler;
113 auto h = std::dynamic_pointer_cast<AnalysisHandlerContainer>(analysis.getData("AnalysisHandlerContainer"));
114 AbstractTextualAnalysisHandler* handler = static_cast<AbstractTextualAnalysisHandler*>(h->getHandler(m_handler));
115 if (handler==0)
116 {
117 LERROR << "AnnotationGraphXmlDumper::process: handler " << m_handler << " has not been given to the core client";
118 return MISSING_DATA;
119 }
120
121 handler->startAnalysis();
122 HandlerStreamBuf hsb(handler);
123 std::ostream outputStream(&hsb);
124
125 if( !prologue.empty() )
126 outputStream << prologue << std::endl;
127// outputStream << "<!DOCTYPE lima_analysis_dump SYSTEM \"lima-xml-output.dtd\">" << std::endl;
128 outputStream << "<lima_annotation_graph_dump>" << std::endl;
129
130 dumpLimaData(outputStream, *(std::dynamic_pointer_cast<AnnotationData>(analysis.getData("AnnotationData"))));
131
132 outputStream << "</lima_annotation_graph_dump>" << std::endl;
133 handler->endAnalysis();
134
135 return SUCCESS_ID;
136}
137
138
139//***********************************************************************
140// main function for outputing the graph
141//***********************************************************************
143 const AnnotationData& annotData) const
144{
145 AnnotationGraph& graph = const_cast<AnnotationGraph&>(annotData.getGraph());
146
147 // go through the graph, add BoWTokens that are not in complex terms
148 DumpGraphVisitor vis(*this, annotData, os, m_language);
149
150 os << "<annot-graph>" << std::endl;
151 try
152 {
154 boost::tie(i, i_end) = vertices(graph);
155 boost::depth_first_search(graph, boost::visitor(vis));
156 }
158 { //do nothing: normal ending
159 }
160 os << "</annot-graph>" << std::endl;
161}
162
163//***********************************************************************
164// output functions
165//***********************************************************************
166
167
169 const AnnotationGraph& graph,
170 const AnnotationData& annotData,
171 std::ostream& xmlStream) const
172{
173 xmlStream << " <vertex id=\"" << v << "\" >" << std::endl;
174 outputVertexIAnnotations(v, graph, annotData, xmlStream);
175 outputVertexSAnnotations(v, graph, annotData, xmlStream);
176 outputVertexGAnnotations(v, graph, annotData, xmlStream);
177 xmlStream << " </vertex>" << std::endl;
178}
179
181 const AnnotationGraph& graph,
182 const AnnotationData& annotData,
183 std::ostream& xmlStream) const
184{
185 const auto& map = get(vertex_iannot, graph, v);
186 auto it = map.cbegin(); auto it_end = map.cend();
187 if (it != it_end)
188 {
189 xmlStream << " <iannots>" << std::endl;
190 for (; it != it_end; it++)
191 {
192 xmlStream << " <iannot name=\""
193 << annotData.annotationName((*it).first).toStdString()
194 << "\" value=\"" << (*it).second << "\" />" << std::endl;
195 }
196 xmlStream << " </iannots>" << std::endl;
197 }
198}
199
201 const AnnotationGraph& graph,
202 const AnnotationData& annotData,
203 std::ostream& xmlStream) const
204{
205 const auto& map = get(vertex_sannot, graph, v);
206 auto it = map.cbegin(); auto it_end = map.cend();
207
208 if (it != it_end)
209 {
210 xmlStream << " <sannots>" << std::endl;
211 for (; it != it_end; it++)
212 {
213 xmlStream << " <sannot name=\""
214 << annotData.annotationName((*it).first).toStdString()
215 << "\" value=\"" << (*it).second.toStdString() << "\" />" << std::endl;
216 }
217 xmlStream << " </sannots>" << std::endl;
218 }
219}
220
222 const AnnotationGraph& graph,
223 const AnnotationData& annotData,
224 std::ostream& xmlStream) const
225{
226 const AGGannotProp& map = get(vertex_gannot, graph, v);
227 AGGannotProp::iterator it, it_end;
228 it = const_cast<AGGannotProp&>(map).begin(); it_end = const_cast<AGGannotProp&>(map).end();
229 if (it != it_end)
230 {
231 xmlStream << " <annots>" << std::endl;
232 for (; it != it_end; it++)
233 {
234 xmlStream << " <annot name=\""
235 << annotData.annotationName((*it).first).toStdString()
236 << "\" value=\"";
237 const_cast<AnnotationData&>(annotData).dumpFunction((*it).first)->dump(
238 xmlStream, ((*it).second));
239 xmlStream << "\" />" << std::endl;
240 }
241 xmlStream << " </annots>" << std::endl;
242 }
243}
244
246 const AnnotationGraph& graph,
247 const AnnotationData& annotData,
248 std::ostream& xmlStream) const
249{
250 xmlStream << " <edge src=\"" << source(e, graph)
251 << "\" targ=\"" << target(e, graph) << "\">" << std::endl;
252 outputEdgeIAnnotations(e, graph, annotData, xmlStream);
253 outputEdgeSAnnotations(e, graph, annotData, xmlStream);
254 outputEdgeGAnnotations(e, graph, annotData, xmlStream);
255 xmlStream << " </edge>" << std::endl;
256}
257
259 const AnnotationGraph& graph,
260 const AnnotationData& annotData,
261 std::ostream& xmlStream) const
262{
263 const AGIannotProp& map = get(edge_iannot, graph, e);
264 auto it = map.cbegin(); auto it_end = map.cend();
265 if (it != it_end)
266 {
267 xmlStream << " <iannots>" << std::endl;
268 for (; it != it_end; it++)
269 {
270 xmlStream << " <iannot name=\""
271 << annotData.annotationName((*it).first).toStdString()
272 << "\" value=\"" << (*it).second << "\" />" << std::endl;
273 }
274 xmlStream << " </iannots>" << std::endl;
275 }
276}
277
279 const AnnotationGraph& graph,
280 const AnnotationData& annotData,
281 std::ostream& xmlStream) const
282{
283 const AGSannotProp& map = get(edge_sannot, graph, e);
284 AGSannotProp::const_iterator it, it_end;
285 it = map.begin(); it_end = map.end();
286 if (it != it_end)
287 {
288 xmlStream << " <sannots>" << std::endl;
289 for (; it != it_end; it++)
290 {
291 xmlStream << " <sannot name=\""
292 << annotData.annotationName((*it).first).toStdString()
293 << "\" value=\"" << (*it).second.toStdString()
294 << "\" />" << std::endl;
295 }
296 xmlStream << " </sannots>" << std::endl;
297 }
298}
299
301 const AnnotationGraph& graph,
302 const AnnotationData& annotData,
303 std::ostream& xmlStream) const
304{
305 const AGGannotProp& map = get(edge_gannot, graph, e);
306 auto it = const_cast<AGGannotProp&>(map).begin();
307 auto it_end = const_cast<AGGannotProp&>(map).end();
308 if (it != it_end)
309 {
310 xmlStream << " <annots>" << std::endl;
311 for (; it != it_end; it++)
312 {
313 xmlStream << " <annot name=\""
314 << annotData.annotationName((*it).first).toStdString()
315 << "\" value=\"";
316 const_cast<AnnotationData&>(annotData).dumpFunction((*it).first)->dump(
317 xmlStream, ((*it).second));
318 xmlStream << "\" />" << std::endl;
319 }
320 xmlStream << " </annots>" << std::endl;
321 }
322}
323
324//***********************************************************************
325// visitor definition
326//***********************************************************************
327// examine_vertex to test if we are at end of search
329 const AnnotationGraph& g)
330{
331 LIMA_UNUSED(g);
333 LDEBUG << "DumpGraphVisitor: examine_vertex " << v;
334}
335
337 const AnnotationGraph& g)
338{
340 LDEBUG << "DumpGraphVisitor: discover_edge " << e;
341
342 m_dumper.outputEdge(e, g, m_data, m_os);
343}
344
346 const AnnotationGraph& graph)
347{
348 m_dumper.outputVertex(v, graph, m_data, m_os);
349}
350
351} // end namespace AnalysisDumpers
352} // end namespace LinguisticProcessings
353} // end namespace Lima
This file is the main header file for the data related to annotation graphs.
color_traits< default_color_type > Color
#define ANNOTATIONGRAPHXMLDUMPER_CLASSID
@ edge_sannot
@ vertex_gannot
std::map< uint64_t, Lima::Common::AnnotationGraphs::GenericAnnotation > AGGannotProp
Property to annotate vertices with generic values.
@ vertex_sannot
std::map< uint64_t, Lima::LimaString > AGSannotProp
Property to annotate vertices with string values.
@ edge_gannot
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, AGVertexProperties, AGEdgeProperties > AnnotationGraph
The graph class.
@ vertex_iannot
@ edge_iannot
std::map< uint64_t, uint64_t > AGIannotProp
Property to annotate vertices with integer values.
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define DUMPERLOGINIT
Defines a Factory to create Object of type Base.
virtual void startAnalysis()=0
function called by the LIMA analyzer on start of a new document
virtual void endAnalysis()=0
function called by the LIMA analyzer on the end of a text part that is to be analyzed
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.
const LimaString & annotationName(uint64_t annotationId) const
Gives the name of an annotation id.
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
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
a visitor for going through the graph between two points and storing a BoWToken for every vertex that...
void outputEdgeSAnnotations(const AnnotationGraphEdge e, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
void outputVertexIAnnotations(const AnnotationGraphVertex v, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
void outputVertexGAnnotations(const AnnotationGraphVertex v, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
void outputVertex(const AnnotationGraphVertex v, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
void outputEdgeGAnnotations(const AnnotationGraphEdge e, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
LimaStatusCode dump(AnalysisContent &analysis, const std::string &prologue="") const
void dumpLimaData(std::ostream &os, const Common::AnnotationGraphs::AnnotationData &annotData) const
void outputEdgeIAnnotations(const AnnotationGraphEdge e, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void outputVertexSAnnotations(const AnnotationGraphVertex v, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
void outputEdge(const AnnotationGraphEdge e, const AnnotationGraph &graph, const Common::AnnotationGraphs::AnnotationData &annotData, std::ostream &xmlStream) const
This file contains a class to control log of informations about time, such as logging cumulated time ...
AnnotationGraph::vertex_iterator AnnotationGraphVertexIt
AnnotationGraph::edge_descriptor AnnotationGraphEdge
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
SimpleFactory< MediaProcessUnit, AnnotationGraphXmlDumper > annotationGraphXmlDumperFactory(ANNOTATIONGRAPHXMLDUMPER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
launch exception related to the configuration file parsing