LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
AnnotationGraphWriter.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
7
8#include <fstream>
10
11namespace Lima
12{
13namespace Common
14{
15namespace AnnotationGraphs
16{
17
18std::string transcodeToXmlEntities(const std::string& str)
19{
20 std::string xml;
21 std::string::const_iterator it, it_end;
22 it = str.begin(); it_end = str.end();
23 for (; it != it_end; it++)
24 {
25 switch (*it)
26 {
27 case '<' : /*xml += "<";*/ break;
28 case '&' : /*xml += "\&";*/ break;
29 case '>' : /*xml += ">";*/ break;
30 case '"' : /*xml += "\\"";*/ break;
31 case '\'' : /*xml += "\'";*/ break;
32 case '\n' : xml += "\n"; break;
33 default:
34 {
35 xml += *it;
36 }
37 }
38 }
39 return xml;
40}
41
43{
44public:
45 AnnotationGraphVertexWriter(MediaId language,
46 const AnnotationData& data);
48
49 void operator()(std::ostream& out, const AnnotationGraphVertex& v) const;
50
51private:
52 AnnotationGraphVertexWriter& operator=(const AnnotationGraphVertexWriter&) {return *this;}
53
54 const AnnotationData& m_data;
55 MediaId m_language;
56 const Common::PropertyCode::PropertyCodeManager* m_propertyCodeManager;
57};
58
60{
61public:
62 AnnotationGraphEdgeWriter(MediaId language, const AnnotationData& data);
64
65 void operator()(std::ostream& out, const AnnotationGraphEdge& e) const;
66
67private:
68 AnnotationGraphEdgeWriter& operator=(const AnnotationGraphEdgeWriter&) {return *this;}
69
70 const AnnotationData& m_data;
71 const Common::PropertyCode::PropertyCodeManager* m_propertyCodeManager;
72};
73
75 MediaId language,
76 const AnnotationData& data):
77m_data(data),
78m_language(language)
79{
80 m_propertyCodeManager=&( static_cast<const Common::MediaticData::LanguageData&>( Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager());
81}
82
84 m_data(a.m_data),
85 m_language(a.m_language),
86 m_propertyCodeManager(a.m_propertyCodeManager)
87{
88}
89
91{
92 // AGLOGINIT;
93 // const Common::PropertyCode::PropertyManager& numberManager = m_propertyCodeManager->getPropertyManager("NUMBER");
94 // const Common::PropertyCode::PropertyManager& genderManager = m_propertyCodeManager->getPropertyManager("GENDER");
95 // const Common::PropertyCode::PropertyManager& syntaxManager = m_propertyCodeManager->getPropertyManager("SYNTAX");
96 // const Common::PropertyCode::PropertyManager& macroManager = m_propertyCodeManager->getPropertyManager("MACRO");
97 // const Common::PropertyCode::PropertyManager& microManager = m_propertyCodeManager->getPropertyManager("MICRO");
98 // const FsaStringsPool& stringspool= Common::MediaticData::MediaticData::single().stringsPool(m_language);
99
100 out << "[label=\"" << v << ": ";
101 CVertexAGIannotPropertyMap vimap = boost::get(vertex_iannot, m_data.getGraph());
102 AGIannotProp::const_iterator iit, iit_end;
103 iit = vimap[v].begin(); iit_end = vimap[v].end();
104 if (iit != iit_end)
105 {
106 out << Misc::limastring2utf8stdstring(m_data.annotationName((*iit).first)) << "="<<(*iit).second;
107 iit++;
108 }
109 for (; iit != iit_end; iit++)
110 {
111 out << "," << Misc::limastring2utf8stdstring(m_data.annotationName((*iit).first)) << "="
112 << (*iit).second;
113 }
114 out << ";";
115 CVertexAGSannotPropertyMap vsmap = boost::get(vertex_sannot, m_data.getGraph());
116 AGSannotProp::const_iterator sit, sit_end;
117 sit = vsmap[v].begin(); sit_end = vsmap[v].end();
118 if (sit != sit_end)
119 {
120 out << Misc::limastring2utf8stdstring(m_data.annotationName((*sit).first))
121 << "=" << Misc::limastring2utf8stdstring((*sit).second);
122 sit++;
123 }
124 for (; sit != sit_end; sit++)
125 {
126 out << "," << Misc::limastring2utf8stdstring(m_data.annotationName((*sit).first)) << "="
127 << Misc::limastring2utf8stdstring((*sit).second);
128
129 }
130 out << ";";
131 CVertexAGGannotPropertyMap vgmap = boost::get(vertex_gannot, m_data.getGraph());
132 AGGannotProp::const_iterator git, git_end;
133 git = vgmap[v].begin(); git_end = vgmap[v].end();
134 if (git != git_end)
135 {
136 std::string annot = Misc::limastring2utf8stdstring(m_data.annotationName((*git).first));
137 out << annot << "=";
138 std::ostringstream oss;
139 const_cast<AnnotationData*>(&m_data)->dumpFunction((*git).first)->dump(
140 oss,
141 const_cast<GenericAnnotation&>(((*git).second))
142 );
143 out<< transcodeToXmlEntities(oss.str());
144 git++;
145 }
146 for (; git != git_end; git++)
147 {
148 std::string annot = Misc::limastring2utf8stdstring(m_data.annotationName((*git).first));
149 out << "," << annot << "=";
150 std::ostringstream oss;
151 const_cast<AnnotationData*>(&m_data)->dumpFunction((*git).first)->dump(
152 oss,
153 const_cast<GenericAnnotation&>(((*git).second))
154 );
155 out<< transcodeToXmlEntities(oss.str());
156 }
157
158 out << "\"]";
159}
160
162 MediaId language,
163 const AnnotationData& data) :
164 m_data(data)
165{
166 m_propertyCodeManager=&( static_cast<const Common::MediaticData::LanguageData&>(
167 Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager());
168}
169
171 m_data(a.m_data),
172 m_propertyCodeManager(a.m_propertyCodeManager)
173{
174}
175
176void AnnotationGraphEdgeWriter::operator()(std::ostream& out, const AnnotationGraphEdge& e) const
177{
178
179 // const Common::PropertyCode::PropertyManager& microManager = m_propertyCodeManager->getPropertyManager("MICRO");
180
181 out << "[label=\"";
182 CEdgeAGIannotPropertyMap eimap = boost::get(edge_iannot, m_data.getGraph());
183 AGIannotProp::const_iterator iit, iit_end;
184 iit = eimap[e].begin(); iit_end = eimap[e].end();
185 if (iit != iit_end)
186 {
187 out << Misc::limastring2utf8stdstring(m_data.annotationName((*iit).first)) << "="<<(*iit).second;
188 iit++;
189 }
190 for (; iit != iit_end; iit++)
191 {
192 out << "," << Misc::limastring2utf8stdstring(m_data.annotationName((*iit).first)) << "="<<(*iit).second;
193 }
194 out << ";";
195 CEdgeAGSannotPropertyMap esmap = boost::get(edge_sannot, m_data.getGraph());
196 AGSannotProp::const_iterator sit, sit_end;
197 sit = esmap[e].begin(); sit_end = esmap[e].end();
198 if (sit != sit_end)
199 {
200 out << Misc::limastring2utf8stdstring(m_data.annotationName((*sit).first)) << "="
201 << Misc::limastring2utf8stdstring((*sit).second);
202 sit++;
203 }
204 for (; sit != sit_end; sit++)
205 {
206 out << "," << Misc::limastring2utf8stdstring(m_data.annotationName((*sit).first)) << "="
207 << Misc::limastring2utf8stdstring((*sit).second);
208
209 }
210 out << ";";
211 CEdgeAGGannotPropertyMap egmap = boost::get(edge_gannot, m_data.getGraph());
212 AGGannotProp::const_iterator git, git_end;
213 git = egmap[e].begin(); git_end = egmap[e].end();
214 if (git != git_end)
215 {
216 std::string annot = Misc::limastring2utf8stdstring(m_data.annotationName((*git).first));
217 out << annot << "=";
218 std::ostringstream oss;
219 const_cast<AnnotationData*>(&m_data)->dumpFunction((*git).first)->dump(
220 oss,
221 const_cast<GenericAnnotation&>(((*git).second))
222 );
223 out<< transcodeToXmlEntities(oss.str());
224 git++;
225 }
226 for (; git != git_end; git++)
227 {
228 std::string annot = Misc::limastring2utf8stdstring(m_data.annotationName((*git).first));
229 out << "," << annot << "=";
230 std::ostringstream oss;
231 const_cast<AnnotationData*>(&m_data)->dumpFunction((*git).first)->dump(
232 oss,
233 const_cast<GenericAnnotation&>(((*git).second))
234 );
235 out<< transcodeToXmlEntities(oss.str());
236 }
237
238 out << "\"]";
239}
240
241
242void AnnotationGraphWriter::writeToDotFile(const std::string& dotFileName)
243{
244 if (dotFileName != "")
245 {
246 std::ofstream ofs(dotFileName.c_str(), std::ofstream::binary);
247 boost::write_graphviz(
248 ofs,
249 m_data.getGraph(),
250 AnnotationGraphVertexWriter(m_language,m_data),
251 AnnotationGraphEdgeWriter(m_language,m_data));
252 }
253
254}
255
256} // closing namespace AnnotationGraphs
257} // closing namespace Common
258} // closing namespace Lima
@ edge_sannot
@ vertex_gannot
@ vertex_sannot
@ edge_gannot
@ vertex_iannot
@ edge_iannot
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.
AnnotationGraphEdgeWriter(MediaId language, const AnnotationData &data)
void operator()(std::ostream &out, const AnnotationGraphEdge &e) const
AnnotationGraphVertexWriter(MediaId language, const AnnotationData &data)
void operator()(std::ostream &out, const AnnotationGraphVertex &v) const
This class allows to convert any object into an annotation by inheritance.
Holds linguistic data for one language.
const MediaData & mediaData(MediaId media) const
Provide tools to parse a property file, and deal with the property coding system.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
boost::property_map< AnnotationGraph, vertex_iannot_t >::const_type CVertexAGIannotPropertyMap
boost::property_map< AnnotationGraph, edge_iannot_t >::const_type CEdgeAGIannotPropertyMap
AnnotationGraph::edge_descriptor AnnotationGraphEdge
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
boost::property_map< AnnotationGraph, edge_gannot_t >::const_type CEdgeAGGannotPropertyMap
boost::property_map< AnnotationGraph, edge_sannot_t >::const_type CEdgeAGSannotPropertyMap
boost::property_map< AnnotationGraph, vertex_sannot_t >::const_type CVertexAGSannotPropertyMap
boost::property_map< AnnotationGraph, vertex_gannot_t >::const_type CVertexAGGannotPropertyMap
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.
NAUTITIA.