LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SyntacticData.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
17#include "SyntacticData.h"
18#include "SyntagmaticMatrix.h"
21
23
24namespace Lima {
25namespace LinguisticProcessing {
26namespace SyntacticAnalysis {
27
28
30 AnalysisGraph* anagraph,
31 std::shared_ptr<SyntagmDefStruct> matrices) :
32 m_anagraph(anagraph),
33 m_graph(anagraph->getGraph()),
34 m_depGraph(0),
35 m_matrices(matrices),
36 m_mapping(),
37 m_relations(),
38 m_ownedTokens(),
39 m_ownedMorphData(),
40 m_relationStoredForSelectionalConstraint(0,0,0)
41{
42// SALOGINIT;
43// LDEBUG << "######## new SyntacticData !!!!";
44}
45
47{
48 delete m_depGraph;
49 m_depGraph = 0;
50
51 std::vector<Token*>::iterator itTokens, itTokens_end;
52 itTokens = m_ownedTokens.begin(); itTokens_end = m_ownedTokens.end();
53 for (; itTokens != itTokens_end; itTokens++)
54 {
55 delete *itTokens;
56 }
57
58 std::vector<MorphoSyntacticData*>::iterator itMorphData, itMorphData_end;
59 itMorphData = m_ownedMorphData.begin(); itMorphData_end = m_ownedMorphData.end();
60 for (; itMorphData != itMorphData_end; itMorphData++)
61 {
62 delete *itMorphData;
63 }
64}
65
75{
76// SALOGINIT;
77// LDEBUG << "Calling setupDependencyGraph";
78 if (m_depGraph != 0)
79 delete m_depGraph;
80 m_depGraph = new DependencyGraph(num_vertices(*(m_anagraph->getGraph())));
81 m_mapping.first.clear();
82 m_mapping.second.clear();
83
84 LinguisticGraphVertexIt itg, itg_end;
85 std::tie(itg, itg_end) = vertices(*m_graph);
86 DependencyGraphVertexIt itd, itd_end;
87 std::tie(itd, itd_end) = vertices(*m_depGraph);
88
89 while (itg != itg_end)
90 {
91 m_mapping.first.insert(std::make_pair(*itg, *itd));
92 m_mapping.second.insert(std::make_pair(*itd, *itg));
93 itg++; itd++;
94 }
95}
96
97
106std::pair<LinguisticGraphVertex, DependencyGraphVertex> SyntacticData::addVertex()
107{
108// SALOGINIT;
109// LDEBUG << "Calling SyntacticData::addVertex";
110
111 LinguisticGraphVertex morph = add_vertex(*m_graph);
112 DependencyGraphVertex synt = add_vertex(*m_depGraph);
113 m_mapping.first.insert(std::make_pair(morph, synt));
114 m_mapping.second.insert(std::make_pair(synt, morph));
115 return std::make_pair(morph, synt);
116}
117
118
120{
121 if (m_mapping.first.find(v) == m_mapping.first.end())
122 {
123 std::ostringstream oss;
124 oss << "SyntacticData::depVertexForTokenVertex: searched dependency vertex " << v << " not found";
125 throw LimaException(oss.str());
126 }
127 return (*(m_mapping.first.find(v))).second;
128}
129
131{
132 //SALOGINIT;
133 //LDEBUG << this << " SyntacticData::m_mapping " << &m_mapping << " sizes : (" << m_mapping.first.size() << ","
134 // << m_mapping.second.size() << ")";
135 if (m_mapping.second.find(v) == m_mapping.second.end())
136 throw LimaException("SyntacticData::tokenVertexForDepVertex: searched morphosyntactic vertex not found");
137 return (*(m_mapping.second.find(v))).second;
138}
139
145 const LinguisticGraphVertex& srcVertex,
146 const LinguisticGraphVertex& tgtVertex)
147{
148 uint64_t nb = 0;
149 std::list< LinguisticGraphVertex > verticesToLookAt;
150 verticesToLookAt.push_back(srcVertex);
151 while (!verticesToLookAt.empty())
152 {
153 const LinguisticGraphVertex& currentVertex = verticesToLookAt.front();
154 verticesToLookAt.pop_front();
155 DependencyGraphVertex depVertex = depVertexForTokenVertex(currentVertex);
156 nb += out_degree(depVertex, *m_depGraph);
157
158 DependencyGraphOutEdgeIt it, it_end;
159 for (std::tie(it, it_end) = out_edges(depVertex, *m_depGraph); it != it_end; it++)
160 {
161 DependencyGraphVertex outDepVertex = target(*it, *m_depGraph);
162 LinguisticGraphVertex outVertex = tokenVertexForDepVertex(outDepVertex);
163 if ( outVertex != m_anagraph->lastVertex() && outVertex != tgtVertex)
164 {
165 verticesToLookAt.push_back(outVertex);
166 }
167 }
168 }
169 return nb;
170}
171
172
173std::vector<LinguisticGraphVertex>
176{
177 std::vector<LinguisticGraphVertex> related;
178 EdgeDepRelTypePropertyMap depRelMap = get(edge_deprel_type, *m_depGraph);
179
180 //look at in_edges and out_edges
181 DependencyGraphOutEdgeIt outIt, outIt_end;
182 std::tie(outIt, outIt_end) = out_edges(v, *m_depGraph);
183 for (; outIt != outIt_end; outIt++) {
184 if (depRelMap[*outIt] == relation) {
185 related.push_back(target(*outIt,*m_depGraph));
186 }
187 }
188
189 DependencyGraphInEdgeIt inIt, inIt_end;
190 std::tie(inIt, inIt_end) = in_edges(v, *m_depGraph);
191 for (; inIt != inIt_end; inIt++) {
192 if (depRelMap[*inIt] == relation) {
193 related.push_back(source(*inIt,*m_depGraph));
194 }
195 }
196
197 return related;
198}
199
200// the same but with a set of possible relations
201std::vector<LinguisticGraphVertex>
203 const std::set<Common::MediaticData::SyntacticRelationId>& relations)
204{
205 std::vector<LinguisticGraphVertex> related;
206 EdgeDepRelTypePropertyMap depRelMap = get(edge_deprel_type, *m_depGraph);
207
208 //look at in_edges and out_edges
209 DependencyGraphOutEdgeIt outIt, outIt_end;
210 std::tie(outIt, outIt_end) = out_edges(v, *m_depGraph);
211 for (; outIt != outIt_end; outIt++) {
212 if (relations.find(depRelMap[*outIt]) != relations.end()) {
213 related.push_back(target(*outIt,*m_depGraph));
214 }
215 }
216
217 DependencyGraphInEdgeIt inIt, inIt_end;
218 std::tie(inIt, inIt_end) = in_edges(v, *m_depGraph);
219 for (; inIt != inIt_end; inIt++) {
220 if (relations.find(depRelMap[*inIt]) != relations.end()) {
221 related.push_back(source(*inIt,*m_depGraph));
222 }
223 }
224
225 return related;
226}
227
228//**********************************************************************
229// add a relation that is not attached to a chain (relation between two
230// chains)
232 const LinguisticGraphVertex& src,
233 const LinguisticGraphVertex& dest) {
234#ifdef DEBUG_LP
235 SALOGINIT;
236 LDEBUG << "Adding relation " << relType << " between "
237 << src << " and " << dest << " for no chain";
238#endif
239
240 ChainIdStruct noChain;
241
245 bool success;
246 std::tie(edge, success) = add_edge(v1, v2, *m_depGraph);
247 if (success)
248 {
249// put(edge_depchain_id, *m_depGraph, edge, noChain);
250#ifdef DEBUG_LP
251 SALOGINIT;
252 LDEBUG << "Storing relation "<<v1<<","<<v2<<"," << relType;
253#endif
254 put(edge_deprel_type, *m_depGraph, edge, relType);
255 }
256 else {
257 SALOGINIT;
258 LERROR << "Was not able to add relation (" << relType << ") between "
259 << src << " and " << dest << " for no chain";
260 throw std::runtime_error("Was not able to add relation");
261 }
262}
263
264
266 const LinguisticGraphVertex& src,
267 const LinguisticGraphVertex& dest,
269// SALOGINIT;
270// LDEBUG << "modify relation " << relType << " between "
271// << src << " and " << dest << " for no chain";
272
273 ChainIdStruct noChain;
276 DependencyGraphOutEdgeIt outItr,outItrEnd;
277 std::tie(outItr, outItrEnd) = out_edges(v1, *m_depGraph);
278 for (; outItr != outItrEnd; outItrEnd++)
279 {
280 if(target(*outItr, *m_depGraph) != v2)
281 continue;
282
283 if (get(edge_deprel_type,*m_depGraph,*outItr) == relType)
284 {
285 put(edge_deprel_type, *m_depGraph, *outItr, newType);
286 return;
287 }
288 }
289
290 SALOGINIT;
291 LERROR << "Was not able to modify relation (" << relType << ") between "
292 << src << " and " << dest << " to " << newType;
293 throw std::runtime_error("Was not able to modify relation");
294}
295
296
298 const LinguisticGraphVertex& v2,
300{
301#ifdef DEBUG_LP
302 SALOGINIT;
303 LDEBUG << "SyntacticData::removeDependency " << v1 << ", " << v2 << ", " << relationType;
304#endif
305 EdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *m_depGraph);
306
309#ifdef DEBUG_LP
310 LDEBUG << "SyntacticData::removeDependency dep vertices are " << dv1 << ", " << dv2;
311#endif
312 DependencyGraphOutEdgeIt outIt, outIt_end;
313 std::tie (outIt, outIt_end) = boost::out_edges(dv1, *m_depGraph);
314 for (; outIt != outIt_end; outIt++)
315 {
316 if (target(*outIt, *m_depGraph) == dv2 && relTypeMap[*outIt] == relationType)
317 {
318#ifdef DEBUG_LP
319 LDEBUG << "SyntacticData::removeDependency dependency found. Removing it.";
320#endif
321 boost::remove_edge(*outIt, *m_depGraph);
322 return true;
323 }
324 }
325#ifdef DEBUG_LP
326 LDEBUG << "SyntacticData::removeDependency dependency NOT found.";
327#endif
328 return false;
329}
330
331} // closing namespace SyntacticAnalysis
332} // closing namespace LinguisticProcessing
333} // closing namespace Lima
DependencyGraph::edge_descriptor DependencyGraphEdge
typedefs to simplify the acces to various graphs elements
DependencyGraph::in_edge_iterator DependencyGraphInEdgeIt
DependencyGraph::vertex_iterator DependencyGraphVertexIt
DependencyGraph::out_edge_iterator DependencyGraphOutEdgeIt
boost::property_map< DependencyGraph, edge_deprel_type_t >::type EdgeDepRelTypePropertyMap
DependencyGraph::vertex_descriptor DependencyGraphVertex
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, DepVertexProperties, DepEdgeProperties > DependencyGraph
The dependency graph class.
@ edge_deprel_type
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
LinguisticGraph::vertex_iterator LinguisticGraphVertexIt
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
#define SALOGINIT
Data used for the syntactic analyzis of texts.
The main LIMA exception class.
Definition LimaCommon.h:262
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
uint64_t depsBetween(const LinguisticGraphVertex &srcVertex, const LinguisticGraphVertex &tgtVertex)
Computes the number of dependency relations in the graph between the given source and taget morphosyn...
LinguisticGraphVertex tokenVertexForDepVertex(const DependencyGraphVertex &v) const
void setupDependencyGraph()
This method is used to setup the search of the dependency relations when the PoS tagging is done and ...
std::pair< LinguisticGraphVertex, DependencyGraphVertex > addVertex()
This method is used to setup the search of the dependency relations when the PoS tagging is done and ...
SyntacticData(LinguisticAnalysisStructure::AnalysisGraph *pit, std::shared_ptr< SyntagmDefStruct > matrices)
This constructor uses the given graph to construct a dependency graph with the same number of vertice...
std::vector< LinguisticGraphVertex > findRelated(const LinguisticGraphVertex, const Common::MediaticData::SyntacticRelationId &relation)
find vertices related to a given vertex by a given relation (related vertices can be either source or...
DependencyGraphVertex depVertexForTokenVertex(const LinguisticGraphVertex &v) const
void modifyRelationNoChain(const Common::MediaticData::SyntacticRelationId &relType, const LinguisticGraphVertex &src, const LinguisticGraphVertex &dest, const Common::MediaticData::SyntacticRelationId &newType)
bool removeDependency(const LinguisticGraphVertex &v1, const LinguisticGraphVertex &v2, const Common::MediaticData::SyntacticRelationId relationType=0)
void addRelationNoChain(const Common::MediaticData::SyntacticRelationId &relType, const LinguisticGraphVertex &src, const LinguisticGraphVertex &dest)
NAUTITIA.