LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
CorefSolvingNormalizedXmlLogger.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 * Copyright (C) 2008 by CEA LIST *
8 * *
9 ***************************************************************************/
10
14
23
24#include <fstream>
25
26using namespace std;
27using namespace Lima::Common::Misc;
28using namespace Lima::Common::AnnotationGraphs;
29
30namespace Lima
31{
32 namespace LinguisticProcessing
33 {
34 namespace Coreferences
35 {
36
38
40 AbstractLinguisticLogger(".output.xml"),
41 m_language(0),
42 m_graph("PosGraph")
43 {}
44
47
50 Manager* manager)
51
52 {
53// COREFSOLVERLOGINIT;
54 AbstractLinguisticLogger::init(unitConfiguration,manager);
55
56 m_language=manager->getInitializationParameters().media;
57 }
59 AnalysisContent& analysis) const
60 {
61// COREFSOLVERLOGINIT;
63 auto annotationData = std::dynamic_pointer_cast<AnnotationData>(analysis.getData("AnnotationData"));
64 const auto& graph = *(std::dynamic_pointer_cast<LinguisticAnalysisStructure::AnalysisGraph>(analysis.getData(m_graph)));
65
66// LinguisticGraph* lingGraph = const_cast<LinguisticGraph*>(graph.getGraph());
67 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
68 if (metadata == 0)
69 {
71 LERROR << "no LinguisticMetaData ! abort";
72 return MISSING_DATA;
73 }
74
75 ofstream out;
76 if (!openLogFile(out,metadata->getMetaData("FileName")))
77 {
79 LERROR << "Can't open log file ";
80 return UNKNOWN_ERROR;
81 }
82
83 out << "<coreferences>" << endl;
84
85
86 // LDEBUG << "CorefSolvingNormalizedXmlLogger on graph " << m_graph;
87 AnnotationGraphVertexIt itv, itv_end;
88 boost::tie(itv, itv_end) = vertices(annotationData->getGraph());
89 for (; itv != itv_end; itv++)
90 {
91 // process
92 //LDEBUG << "CorefSolvingNormalizedXmlLogger on annotation vertex " << *itv;
93 if (annotationData->hasAnnotation(*itv,utf8stdstring2limastring("Coreferent")))
94 //if (annotationData->hasAnnotation(*itv,utf8stdstring2limastring("Coreferent")))
95 {
97 try
98 {
99 annot = annotationData->annotation(*itv,utf8stdstring2limastring("Coreferent"))
100 .pointerValue<CoreferentAnnotation>();
101 }
102 catch (const boost::bad_any_cast& )
103 {
105 LERROR << "One annotation on vertex " << *itv << " you are trying to cast is not a Coreference; Coreference not logged";
106 for (int i = 0; i < 19 ; i++)
107 {
108 LERROR << "annot "<< i << " : " << annotationData->annotationName(i);
109 }
110 continue;
111 }
113 if (token == 0)
114 {
116 LERROR << "Vertex " << *itv << " has no entry in the analysis graph token map. This should not happen !!";
117 }
118 else
119 {
120 CoreferentAnnotation* antecedent;
121// bool hasAntecedent = false;
122 AnnotationGraphOutEdgeIt it, it_end;
123 boost::tie(it, it_end) = boost::out_edges(static_cast<AnnotationGraphVertex>(*itv), annotationData->getGraph());
124
125 for (; it != it_end; it++)
126 {
127 if (annotationData->hasAnnotation(target(*it,annotationData->getGraph()),utf8stdstring2limastring("Coreferent")))
128 {
129 try
130 {
131 antecedent = annotationData->annotation(target(*it, annotationData->getGraph()), utf8stdstring2limastring("Coreferent")).pointerValue<CoreferentAnnotation>();
132// hasAntecedent = true;
133 }
134 catch (const boost::bad_any_cast& )
135 {
137 LERROR << "One annotation on vertex you are trying to cast resulting from an edge out of " << *itv << " is not a Coreference; Coreference not logged";
138 continue;
139 }
140 }
141 }
142 out << " <reference>\n"
143 << " <pos>" << get(vertex_token,*graph.getGraph(),annot->morphVertex())->position() << "</pos>\n"
144 << " <len>" << token->stringForm().length() << "</len>\n"
145 << " <string>"<< limastring2utf8stdstring(transcodeToXmlEntities(token->stringForm())) << "</string>\n"
146 << " <npId>" << annot->id() << "</npId>\n"
147 << " <posVertex>" << annot->morphVertex() << "</posVertex>\n";
148 //if (hasAntecedent)
149 if (false)
150 {
151 out << " <npRef>" << antecedent->id() << "</npRef>\n";
152 out << " <refPosVertex>" << antecedent->morphVertex() << "</refPosVertex>\n";
153 out << " <salience>" << annot->salience() << "</salience>\n";
154 }
155 out << " <categ>" << annot->categ() << "</categ>\n"
156 << " </reference>\n"
157 << endl;
158 }
159 }
160 }
161 out << "</coreferences>" << endl;
162 out.close();
163
164 TimeUtils::logElapsedTime("CorefSolvingNormalizedXmlLogger");
165 return SUCCESS_ID;
166
167 }
168
169
170 } // Coreferences
171 } // LinguisticProcessing
172} // Lima
This file is the main header file for the data related to annotation graphs.
#define COREFSOLVINGNORMALIZEDXMLLOGGER_CLASSID
#define LERROR
Definition LimaCommon.h:161
@ vertex_token
#define COREFSOLVERLOGINIT
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
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
A generic process unit to log information in files: contains some common informations such as : outpu...
bool openLogFile(std::ofstream &output, const std::string &sourceFile) const
virtual void init(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.
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
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::vertex_descriptor AnnotationGraphVertex
AnnotationGraph::out_edge_iterator AnnotationGraphOutEdgeIt
std::string transcodeToXmlEntities(const std::string &str)
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, CorefSolvingNormalizedXmlLogger > CorefSolvingNormalizedXmlLoggerFactory(COREFSOLVINGNORMALIZEDXMLLOGGER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ UNKNOWN_ERROR
Definition LimaCommon.h:238
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.