LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SemanticRelationsXmlLogger.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 SemanticRelationsXmlLogger.cpp
9 * @author (romaric.besancon@cea.fr)
10 * @date Mon Sep 17 2007
11 * copyright Copyright (C) 2007 by CEA LIST
12 *
13 ***********************************************************************/
14
15
18#include "SemanticAnnotation.h"
19
20// #include "common/linguisticData/linguisticData.h"
21//#include "common/misc/strwstrtools.h"
22//#include "common/misc/traceUtils.h"
34
35#include <fstream>
36
38
39//#define SEMANTICANALYSISLOGINIT LOGINIT("LP::SemanticAnalysis")
40
41using namespace std;
42// using namespace boost;
43using namespace Lima::Common::MediaticData;
44using namespace Lima::Common::AnnotationGraphs;
47
48namespace Lima {
49namespace LinguisticProcessing {
50namespace SemanticAnalysis {
51
52
54
58
59
62
65 Manager* manager)
66
67{
68 AbstractTextualAnalysisDumper::init(unitConfiguration,manager);
69
70 m_language=manager->getInitializationParameters().media;
71
72 try
73 {
74 m_graph=unitConfiguration.getParamsValueAtKey("graph");
75 }
77 {
79 LWARN << "No 'graph' parameter in unit configuration '"
80 << unitConfiguration.getName() << "' ; using PosGraph";
81 m_graph=string("PosGraph");
82 }
83}
84
86process(AnalysisContent& analysis) const
87{
89
91 LDEBUG << "SemanticRelationsXmlLogger";
92
93 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
94
95 const auto& graph = *(std::dynamic_pointer_cast<LinguisticAnalysisStructure::AnalysisGraph>(analysis.getData(m_graph)));
96
97 LinguisticGraph* lingGraph = const_cast<LinguisticGraph*>(graph.getGraph());
98 VertexTokenPropertyMap tokenMap = get(vertex_token, *lingGraph);
99 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
100 if (metadata == 0) {
102 LERROR << "no LinguisticMetaData ! abort";
103 return MISSING_DATA;
104 }
105
106 auto dstream = initialize(analysis);
107 std::ostream& outputStream=dstream->out();
108
109 ofstream out;
110 /*if (!openLogFile(out,metadata->getMetaData("FileName"))) {
111 SEMANTICANALYSISLOGINIT;
112 LERROR << "Can't open log file ";
113 return UNKNOWN_ERROR;
114 }*/
115
116 uint64_t offset(0);
117 try {
118 offset=atoi(metadata->getMetaData("StartOffset").c_str());
119 }
121 // do nothing: not set in analyzeText (only in analyzeXmlDocuments)
122 }
123
124 uint64_t offsetIndexingNode(0);
125 try {
126 offsetIndexingNode=atoi(metadata->getMetaData("StartOffsetIndexingNode").c_str());
127 }
129 // do nothing: not set in analyzeText (only in analyzeXmlDocuments)
130 }
131
132 std::string docId("");
133 try {
134 docId=metadata->getMetaData("DocId");
135 }
137 // do nothing: not set in analyzeText (only in analyzeXmlDocuments)
138 }
139
140 outputStream << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
141 outputStream << "<semantic_relation_dump>" << std::endl;
142
143 outputStream << "<relations docid=\"" << docId
144 << "\" offsetNode=\"" << offsetIndexingNode
145 << "\">" << endl;
146
147// LDEBUG << "SemanticRelationsXmlLogger on graph " << m_graph;
148
149 //look at all vertices for annotations
150 AnnotationGraphVertexIt itv, itv_end;
151 boost::tie(itv, itv_end) = vertices(annotationData->getGraph());
152 for (; itv != itv_end; itv++)
153 {
154 LDEBUG << "SemanticRelationsXmlLogger on annotation vertex " << *itv;
155 if (annotationData->hasAnnotation(*itv,Common::Misc::utf8stdstring2limastring("SemanticAnnotation")))
156 {
157// LDEBUG << " it has SemanticRelationAnnotation";
158 const SemanticAnnotation* annot = 0;
159 try
160 {
161 annot = annotationData->annotation(*itv,Common::Misc::utf8stdstring2limastring("SemanticAnnotation"))
162 .pointerValue<SemanticAnnotation>();
163 }
164 catch (const boost::bad_any_cast& e)
165 {
167 LERROR << "This annotation is not a SemanticRelation";
168 continue;
169 }
170
171 // output
172 outputStream << "<annotation type=\"" << annot->getType() << "\">" << endl
173 << vertexStringForSemanticAnnotation("vertex", *itv, tokenMap, annotationData.get(), offset)
174 << "</annotation>" << endl;
175 }
176 }
177
178 // look at all edges for relations
179 AnnotationGraphEdgeIt it,it_end;
180 const AnnotationGraph& annotGraph=annotationData->getGraph();
181 boost::tie(it, it_end) = edges(annotGraph);
182 for (; it != it_end; it++) {
183 LDEBUG << "SemanticRelationsXmlLogger on annotation edge "
184 << source(*it,annotGraph) << "->" << target(*it,annotationData->getGraph());
185 if (annotationData->hasAnnotation(*it,Common::Misc::utf8stdstring2limastring("SemanticRelation")))
186 {
188 LDEBUG << "found semantic relation";
189 const SemanticRelationAnnotation* annot = 0;
190 try
191 {
192 annot = annotationData->annotation(*it,Common::Misc::utf8stdstring2limastring("SemanticRelation"))
193 .pointerValue<SemanticRelationAnnotation>();
194 }
195 catch (const boost::bad_any_cast& e)
196 {
198 LERROR << "This annotation is not a SemanticAnnotation";
199 continue;
200 }
201
202 //output
203 outputStream << "<relation type=\"" << annot->type() << "\">" << endl
204 << vertexStringForSemanticAnnotation("source",source(*it,annotGraph),tokenMap,annotationData.get(),offset)
205 << vertexStringForSemanticAnnotation("target",target(*it,annotGraph),tokenMap,annotationData.get(),offset)
206 << "</relation>" << endl;
207
208 }
209 }
210
211// LDEBUG << " all vertices done";
212 outputStream << "</relations>" << endl;
213 //out.close();
214 outputStream << "</semantic_relation_dump>" << std::endl;
215
216 TimeUtils::logElapsedTime("SemanticRelationsXmlLogger");
217 return SUCCESS_ID;
218}
219
220std::string SemanticRelationsXmlLogger::
221vertexStringForSemanticAnnotation(const std::string& vertexRole,
222 const AnnotationGraphVertex& vertex,
223 const VertexTokenPropertyMap& tokenMap,
224 AnnotationData* annotationData,
225 uint64_t offset) const
226{
227 ostringstream oss;
228
229 // get id of the corresponding vertex in analysis graph
231 if (!annotationData->hasIntAnnotation(vertex,Common::Misc::utf8stdstring2limastring(m_graph)))
232 {
233 // SEMANTICANALYSISLOGINIT;
234 // LDEBUG << *itv << " has no " << m_graph << " annotation. Skeeping it.";
235 return "";
236 }
237 v = annotationData->intAnnotation(vertex,Common::Misc::utf8stdstring2limastring(m_graph));
238 LinguisticAnalysisStructure::Token* vToken = tokenMap[v];
239 // LDEBUG << "SemanticRelationsXmlLogger tokenMap[" << v << "] = " << vToken;
240 if (vToken == 0)
241 {
243 LERROR << "Vertex " << v << " has no entry in the analysis graph token map. This should not happen !!";
244 return "";
245 }
246
247 // get annotation : element in relation can be an entity => get entity type
248 // otherwise, its type is "token"
249 //EntityT type("token");
250
251 //std::set< uint32_t > matches = annotationData->matches(m_graph,v,"annot");
252 std::set< AnnotationGraphVertex > matches = annotationData->matches(m_graph,v,"annot");
253 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
254 it != matches.end(); it++)
255 {
256 if (annotationData->hasAnnotation(*it,Common::Misc::utf8stdstring2limastring("SpecificEntity"))) {
257 const SpecificEntityAnnotation* annot = 0;
258 try {
259 annot = annotationData->annotation(*it,Common::Misc::utf8stdstring2limastring("SpecificEntity"))
260 .pointerValue<SpecificEntityAnnotation>();
261 }
262 catch (const boost::bad_any_cast& e) {
264 LERROR << "This annotation is not a SemanticAnnotation";
265 continue;
266 }
268 oss << " <" << vertexRole
269 << " type=\"" << Common::Misc::limastring2utf8stdstring(typeStr) << "\""
270 << " pos=\"" << offset+vToken->position() << "\""
271 << " len=\"" << vToken->length() << "\""
272 << " string=\"" << Common::Misc::limastring2utf8stdstring(vToken->stringForm()) << "\""
273 << "/>" << endl;
274 //EntityType type=Common::MediaticData::MediaticData::single().getEntityName(annot->getType());
275 break;
276 }
277 }
278
279
280 return oss.str();
281}
282
283
284} // SemanticAnalysis
285} // LinguisticProcessing
286} // Lima
287
This file is the main header file for the data related to annotation graphs.
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, AGVertexProperties, AGEdgeProperties > AnnotationGraph
The graph class.
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
boost::property_map< LinguisticGraph, vertex_token_t >::type VertexTokenPropertyMap
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define SEMANTICANALYSISLOGINIT
#define SEMANTICRELATIONSXMLLOGGER_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
Holds an annotation graph and gives an API to manipulate it.
uint64_t intAnnotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
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
LimaString getEntityName(const EntityType &type) const
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
std::shared_ptr< DumperStream > initialize(AnalysisContent &analysis) const
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
An annotation between two annotation graph vertices denoting the semantic relation(s) holding between...
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.
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_iterator AnnotationGraphVertexIt
AnnotationGraph::edge_iterator AnnotationGraphEdgeIt
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
bool hasIntAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
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)
SimpleFactory< MediaProcessUnit, SemanticRelationsXmlLogger > semanticRelationsXmlLoggerFactory(SEMANTICRELATIONSXMLLOGGER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
QString LimaString
Definition LimaString.h:33
STL namespace.