LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
DepGraphCompoundsBuildVisitor.cpp
Go to the documentation of this file.
1// Copyright 2002-2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
13
20
21#include <stdexcept>
22#include <iostream>
23
24namespace Lima
25{
26using namespace Common;
27using namespace Common::AnnotationGraphs;
28
29namespace LinguisticProcessing
30{
31using namespace LinguisticAnalysisStructure;
32using namespace SyntacticAnalysis;
33
34namespace Compounds
35{
36
38{
40
43 MediaId language,
45 DependencyGraph* depGraph,
47 LinguisticGraphVertex startVertex,
48 LinguisticGraphVertex stopVertex,
49// uint64_t depGraphMaxBranchingFactor,
51
53
54 bool isDepEdgeACompoundNounRelation(const DependencyGraphEdge& e) const;
55
56 std::vector< DependencyGraphVertex > getNodes(const std::vector< DependencyGraphEdge >& relations);
57
58 // calculer les incompatibilites binaires
59 std::map< uint64_t, std::set< uint64_t > > computeExclusions(
60 const std::vector< DependencyGraphEdge >& relations);
61
62 // remplir le tableau des tailles
63 std::map< uint64_t, bool > computeSizes();
64
65 // calculer l'ordre
66 std::vector< uint64_t > computeOrder(const std::map< uint64_t, std::set< uint64_t > >& exclusions);
67
68 // construire les termes correspondants et les enregistrer
69 void buildTermsFromCompoundsSets(std::list< std::set< uint64_t > >& compoundsSets);
70
71 DependencyGraphVertex findFinalSourceOfCompoundInRelationsSet(std::set< uint64_t >& relations);
72
74 const std::set< uint64_t > relations,
75 bool theHead = false);
76
77 void buildCompatibilityTermsFromWithChains(DependencyGraphVertex from);
78 void buildCompatibilityTermsFromNoChains(DependencyGraphVertex from);
79
84 AnnotationGraphVertex createAnnotationVertex(const LinguisticGraphVertex v);
85
86 std::vector< DependencyGraphEdge > getCompoundsRelationsFromWithChain(
89
90 std::map< uint64_t, std::set< uint64_t > > computeExclusionsWithChain(
91 const std::vector< DependencyGraphEdge >& relations,
93
94 std::vector< DependencyGraphEdge > getCompoundsRelationsFromNoChain(
96
97 std::map< uint64_t, std::set< uint64_t > > computeExclusionsNoChain(
98 const std::vector< DependencyGraphEdge >& relations);
99
100 ConceptModifier createModifierFor(LinguisticGraphVertex inSrcMorph);
101
102
103 const CompoundsBuilderFromSyntacticData* m_context;
104 MediaId m_language;
105 SyntacticAnalysis::SyntacticData* m_syntacticData;
106 DependencyGraphVertex m_depFailedVertex;
107 std::set< DependencyGraphVertex > m_visited;
108 std::set< std::pair< DependencyGraphVertex, LinguisticAnalysisStructure::ChainIdStruct > > m_visitedWithChains;
109 std::set< std::string > m_alreadyInserted;
110 DependencyGraph* m_depGraph;
112 const Common::PropertyCode::PropertyAccessor* m_macroAccessor;
113 const Common::PropertyCode::PropertyAccessor* m_microAccessor;
115
120 std::vector<
121 std::pair<
122 boost::tuple<uint64_t,DependencyGraphVertex, std::set< uint64_t>, Common::MediaticData::SyntacticRelationId >,
123 boost::tuple<uint64_t,DependencyGraphVertex, std::set<uint64_t>, Common::MediaticData::SyntacticRelationId >
124 >
125 > m_relsmap;
126
127 std::vector< boost::tuple<uint64_t,DependencyGraphVertex, std::set<uint64_t> > > m_nodesmap;
128};
129
130DepGraphCompoundsBuildVisitorPrivate::DepGraphCompoundsBuildVisitorPrivate(const
131 CompoundsBuilderFromSyntacticData* cpbfsd,
132 MediaId language,
134 DependencyGraph* depGraph,
136 LinguisticGraphVertex startVertex,
137 LinguisticGraphVertex stopVertex,
139 m_context(cpbfsd),
140 m_language(language),
141 m_syntacticData(syntacticData),
142 m_depFailedVertex(syntacticData->depVertexForTokenVertex(iter->firstVertex())),
143 m_visited(),
144 m_depGraph(depGraph),
145 m_iter(iter),
146 m_annotationData(annotationData)
147{
148 m_macroAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyAccessor("MACRO"));
149 m_microAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyAccessor("MICRO"));
150 if (m_annotationData->dumpFunction("CompoundTokenAnnotation") == 0)
151 {
152 m_annotationData->dumpFunction("CompoundTokenAnnotation", new DumpCompoundTokenAnnotation(Common::MediaticData::MediaticData::changeable().stringsPool(m_language)));
153 }
154}
155
156
158 MediaId language,
160 DependencyGraph* depGraph,
162 LinguisticGraphVertex startVertex,
163 LinguisticGraphVertex stopVertex,
166 language,
167 syntacticData,
168 depGraph,
169 iter,
170 startVertex,
171 stopVertex,
172 annotationData))
173{
174}
175
180
182{
183#ifdef DEBUG_LP
185 LDEBUG << "DepGraphCompoundsBuildVisitor::discover_vertex" << v;
186#endif
187 if ( (v != m_d->m_syntacticData-> iterator()-> firstVertex()) &&
188 (v != m_d->m_syntacticData-> iterator()-> lastVertex()) )
189 {
190 DependencyGraphVertex vd = m_d->m_syntacticData->depVertexForTokenVertex(v);
191 if (m_d->m_visited.find(vd) == m_d->m_visited.end())
192 {
193 m_d->buildCompatibilityTermsFromNoChains(vd);
194 }
195#ifdef DEBUG_LP
196 else
197 {
198 LDEBUG << "DepGraphCompoundsBuildVisitor::discover_vertex" << v << "already visited";
199 }
200#endif
201 }
202#ifdef DEBUG_LP
203 else
204 {
205 LDEBUG << "DepGraphCompoundsBuildVisitor::discover_vertex" << v << "is first or last vertex";
206 }
207#endif
208}
209
210void DepGraphCompoundsBuildVisitorPrivate::buildCompatibilityTermsFromWithChains(DependencyGraphVertex from)
211{
212 //ChainIdStruct
213#ifdef DEBUG_LP
215 LDEBUG << "Building compatibility terms with chains from " << from;
216#endif
217 VertexChainIdPropertyMap chainsMap = get(vertex_chain_id, *(m_syntacticData->graph()));
218 LinguisticGraphVertex fromTokVertex = m_syntacticData->tokenVertexForDepVertex(from);
219 VertexChainIdProp::const_iterator fromChainsIt, fromChainsIt_end;
220 fromChainsIt = chainsMap[fromTokVertex].begin(); fromChainsIt_end = chainsMap[fromTokVertex].end();
221 for (; fromChainsIt != fromChainsIt_end; fromChainsIt++)
222 {
223#ifdef DEBUG_LP
224 LDEBUG << "Building compatibility terms with chains from " << from << " on chain " << *fromChainsIt;
225#endif
226 if (m_visitedWithChains.find(std::make_pair(from,*fromChainsIt)) == m_visitedWithChains.end())
227 {
228 // recuperer l'ensemble des relations accessibles depuis from
229 std::vector< DependencyGraphEdge > relations = getCompoundsRelationsFromWithChain(from, *fromChainsIt);
230#ifdef DEBUG_LP
231 LDEBUG << "Got " << relations.size() << " compound relations";
232#endif
233 std::vector< DependencyGraphVertex > nodes = getNodes(relations);
234#ifdef DEBUG_LP
235 LDEBUG << "Got " << nodes.size() << " compound nodes";
236#endif
237 // calculer les incompatibilites binaires
238 std::map< uint64_t, std::set< uint64_t > > exclusions = computeExclusionsWithChain(relations, *fromChainsIt);
239#ifdef DEBUG_LP
240 LDEBUG << "Got " << exclusions.size() << " exclusions";
241#endif
242 // calculer l'ordre
243 std::map< uint64_t, bool > sizes = computeSizes();
244#ifdef DEBUG_LP
245 LDEBUG << "Got " << sizes.size() << " sizes";
246#endif
247 // remplir le tableau des tailles
248 std::vector< uint64_t > order = computeOrder(exclusions);
249#ifdef DEBUG_LP
250 LDEBUG << "Got " << order.size() << " order elements";
251#endif
252 // appeler le calcul des compatibilites
254 std::list< std::set< uint64_t > > compoundsSets = builder.computeCompatibilitiesWithChain(exclusions, sizes, order);
255#ifdef DEBUG_LP
256 LDEBUG << "Got " << compoundsSets.size() << " compounds sets" ;
257#endif
258 std::list< std::set< uint64_t > >::const_iterator compsIt, compsIt_end;
259 compsIt = compoundsSets.begin(); compsIt_end = compoundsSets.end();
260 for (; compsIt != compsIt_end; compsIt++)
261 {
262 std::set< uint64_t >::const_iterator compIt, compIt_end;
263 compIt = (*compsIt).begin(); compIt_end = (*compsIt).end();
264 for (; compIt != compIt_end; compIt++)
265 {
266// #ifdef DEBUG_LP
267// LDEBUG << "("<<m_relsmap[*compIt].first.get<1>()<<"->"
268// < <m_relsmap[*compIt].second.get<1>()<<"), ";
269// #endif
270 }
271#ifdef DEBUG_LP
272 LDEBUG;
273#endif
274 }
275
276 // construire les termes correspondants et les enregistrer
277#ifdef DEBUG_LP
278 LDEBUG << "Building terms from compatibility results";
279#endif
280 buildTermsFromCompoundsSets(compoundsSets);
281 }
282 }
283#ifdef DEBUG_LP
284 LDEBUG << "Finished Building compatibility terms with chains from " << from;
285#endif
286}
287
288void DepGraphCompoundsBuildVisitorPrivate::buildCompatibilityTermsFromNoChains(
290{
291 //ChainIdStruct
292#ifdef DEBUG_LP
294 LDEBUG << "Building compatibility terms no chains from " << from;
295#endif
296 {
297#ifdef DEBUG_LP
298 LDEBUG << "Building compatibility terms no chains from " << from ;
299#endif
300 {
301 // recuperer l'ensemble des relations accessibles depuis from
302 std::vector< DependencyGraphEdge > relations = getCompoundsRelationsFromNoChain(from);
303#ifdef DEBUG_LP
304 LDEBUG << "Got " << relations.size() << " compound relations";
305#endif
306 std::vector< DependencyGraphVertex > nodes = getNodes(relations);
307#ifdef DEBUG_LP
308 LDEBUG << "Got " << nodes.size() << " compound nodes";
309#endif
310 // calculer les incompatibilites binaires
311 std::map< uint64_t, std::set< uint64_t > > exclusions = computeExclusionsNoChain(relations);
312#ifdef DEBUG_LP
313 LDEBUG << "Got " << exclusions.size() << " exclusions";
314#endif
315 // calculer l'ordre
316 std::map< uint64_t, bool > sizes = computeSizes();
317#ifdef DEBUG_LP
318 LDEBUG << "Got " << sizes.size() << " sizes";
319#endif
320 // remplir le tableau des tailles
321 std::vector< uint64_t > order = computeOrder(exclusions);
322#ifdef DEBUG_LP
323 LDEBUG << "Got " << order.size() << " order elements";
324#endif
325 // appeler le calcul des compatibilites
326 CompoundsCompatibilityBuilder builder;
327 std::list< std::set< uint64_t > > compoundsSets = builder.computeCompatibilitiesWithChain(exclusions, sizes, order);
328#ifdef DEBUG_LP
329 LDEBUG << "Got " << compoundsSets.size() << " compounds sets" ;
330#endif
331 std::list< std::set< uint64_t > >::const_iterator compsIt, compsIt_end;
332 compsIt = compoundsSets.begin(); compsIt_end = compoundsSets.end();
333 for (; compsIt != compsIt_end; compsIt++)
334 {
335 std::set< uint64_t >::const_iterator compIt, compIt_end;
336 compIt = (*compsIt).begin(); compIt_end = (*compsIt).end();
337 for (; compIt != compIt_end; compIt++)
338 {
339// #ifdef DEBUG_LP
340// LDEBUG << "("<<m_relsmap[*compIt].first.get<1>()<<"->"
341// < <m_relsmap[*compIt].second.get<1>()<<"), ";
342// #endif
343 }
344#ifdef DEBUG_LP
345 LDEBUG;
346#endif
347 }
348
349 // construire les termes correspondants et les enregistrer
350#ifdef DEBUG_LP
351 LDEBUG << "Building terms from compatibility results";
352#endif
353 buildTermsFromCompoundsSets(compoundsSets);
354 }
355 }
356#ifdef DEBUG_LP
357 LDEBUG << "Finished Building compatibility terms no chains from " << from;
358#endif
359}
360
361// construire les termes correspondants et les enregistrer
362void DepGraphCompoundsBuildVisitorPrivate::buildTermsFromCompoundsSets(
363 std::list< std::set< uint64_t > >& compoundsSets)
364{
365/*
366 Critical function : comment logging messages
367*/
368// MORPHOLOGINIT;
369 for (auto currentSet: compoundsSets)
370 {
371// LDEBUG << "Building a Compound";
372 DependencyGraphVertex head = findFinalSourceOfCompoundInRelationsSet(currentSet);
373 if (head == 0)
374 {
376 LWARN << "No final source of compound found => no term built here.";
377 continue;
378 }
379// LDEBUG << "Compound head is " << head;
380 buildTermFor(head,currentSet,true);
381 }
382}
383
384
385bool DepGraphCompoundsBuildVisitorPrivate::isDepEdgeACompoundNounRelation(const DependencyGraphEdge& e) const
386{
387 EdgeDepRelTypePropertyMap relTypeMap =
388 get(edge_deprel_type, *m_depGraph);
389
390 Common::MediaticData::SyntacticRelationId relType=relTypeMap[e];
391 std::string relName = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationName(relType);
392
393#ifdef DEBUG_LP
395 LDEBUG << "isDepEdgeACompoundNounRelation: Relation name" << relName;
396#endif
397
398 if (relName.empty())
399 {
401 LERROR << "Unknown type " << relType
402 << " while testing edge for compound relation.";
403 return false;
404 }
405 if (m_context->isACompoundRel(relName))
406 {
407 return true;
408 }
409 else
410 {
411 return false;
412 }
413}
414
415std::vector< DependencyGraphVertex > DepGraphCompoundsBuildVisitorPrivate::getNodes(
416 const std::vector< DependencyGraphEdge >& relations)
417{
418/*
419 Critical function : comment logging messages
420*/
421// MORPHOLOGINIT;
422// LDEBUG << "Getting nodes";
423 std::vector< DependencyGraphVertex > nodes;
424 std::set< DependencyGraphVertex > snodes;
425 std::vector< DependencyGraphEdge >::const_iterator it, it_end;
426 it = relations.begin(); it_end = relations.end();
427 for (; it != it_end; it++)
428 {
429 if (snodes.find(source(*it, *m_depGraph)) == snodes.end())
430 {
431 snodes.insert(source(*it, *m_depGraph));
432 nodes.push_back(source(*it, *m_depGraph));
433 }
434 if (snodes.find(target(*it, *m_depGraph)) == snodes.end())
435 {
436 snodes.insert(target(*it, *m_depGraph));
437 nodes.push_back(target(*it, *m_depGraph));
438 }
439 }
440 return nodes;
441}
442
443// calculer les incompatibilites binaires
444std::map< uint64_t, std::set< uint64_t > > DepGraphCompoundsBuildVisitorPrivate::computeExclusions(
445 const std::vector< DependencyGraphEdge >& relations)
446{
447#ifdef DEBUG_LP
449 LDEBUG << "Computing exclusions";
450#endif
451 std::map< uint64_t, std::set< uint64_t > > exclusions;
452 EdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *m_depGraph);
453 VertexChainIdPropertyMap chainsMap = get(vertex_chain_id, *(m_syntacticData->graph()));
454 m_relsmap.clear();
455 m_nodesmap.clear();
456 std::vector< DependencyGraphEdge >::const_iterator relsit, relsit_end;
457 relsit = relations.begin(); relsit_end = relations.end();
458 //preparation des donnees
459 for (; relsit != relsit_end; relsit++)
460 {
461 DependencyGraphVertex src = source(*relsit, *m_depGraph);
462 DependencyGraphVertex tgt = target(*relsit, *m_depGraph);
463 LinguisticGraphVertex srcTokVertex = m_syntacticData->tokenVertexForDepVertex(src);
464 LinguisticGraphVertex tgtTokVertex = m_syntacticData->tokenVertexForDepVertex(tgt);
465 const Token* srcToken = get(vertex_token, *(m_iter->getGraph()), src);
466 const Token* tgtToken = get(vertex_token, *(m_iter->getGraph()), tgt);
467 uint64_t srcPos = srcToken->position();
468 uint64_t tgtPos = tgtToken->position();
469 std::set<uint64_t> srcChains;
470 VertexChainIdProp::const_iterator srcChainsIt, srcChainsIt_end;
471 srcChainsIt = chainsMap[srcTokVertex].begin(); srcChainsIt_end = chainsMap[srcTokVertex].end();
472 for ( ; srcChainsIt != srcChainsIt_end; srcChainsIt++)
473 {
474 srcChains.insert((*srcChainsIt).chainId());
475 }
476 std::set<uint64_t> tgtChains;
477 VertexChainIdProp::const_iterator tgtChainsIt, tgtChainsIt_end;
478 tgtChainsIt = chainsMap[tgtTokVertex].begin(); tgtChainsIt_end = chainsMap[tgtTokVertex].end();
479 for ( ; tgtChainsIt != tgtChainsIt_end; tgtChainsIt++)
480 {
481 tgtChains.insert((*tgtChainsIt).chainId());
482 }
483 // VertexWordPropertyMap dwsMap = get(vertex_word, *(m_iter->getGraph()));
484 // const DicoWord& word = dwsMap[v];
485 // const LimaString& lemma = word.lemma();
486 // uint64_t macroCategory=word.category();
487 Common::MediaticData::SyntacticRelationId relType = relTypeMap[boost::edge(src,tgt,*m_depGraph).first];
488 m_relsmap.push_back(std::make_pair(boost::make_tuple(srcPos,srcTokVertex,srcChains,relType),boost::make_tuple(tgtPos,tgtTokVertex,tgtChains,relType)));
489 m_nodesmap.push_back(boost::make_tuple(srcPos,srcTokVertex,srcChains));
490 m_nodesmap.push_back(boost::make_tuple(tgtPos,tgtTokVertex,tgtChains));
491 }
492 for (uint64_t i = 0; i < m_relsmap.size(); i++)
493 {
494 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
495 }
496 for (uint64_t i = m_relsmap.size(); i < m_relsmap.size()+m_nodesmap.size(); i++)
497 {
498 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
499 }
500 return exclusions;
501 /*
502 // calcul des exclusions
503 for (uint64_t i = 0; i < m_relsmap.size(); i++)
504 {
505 for (uint64_t j = i+1; j < m_relsmap.size(); j++)
506 {
507 std::set< uint64_t > intersec;
508
509 std::insert_iterator< std::set< uint64_t > > ins(intersec, intersec.end());
510 std::set_intersection(m_relsmap[i].first.get<2>().begin(), m_relsmap[i].first.get<2>().end(),
511 m_relsmap[j].first.get<2>().begin(), m_relsmap[j].first.get<2>().end(), ins);
512 if (
513 ((m_relsmap[i].first.get<0>() == m_relsmap[j].first.get<0>() && m_relsmap[i].first.get<1>() != m_relsmap[j].first.get<1>()) || intersec.empty())
514 || ((m_relsmap[i].first.get<0>() == m_relsmap[j].second.get<0>() && m_relsmap[i].first.get<1>() != m_relsmap[j].second.get<1>()) || intersec.empty())
515 || ((m_relsmap[i].second.get<0>() == m_relsmap[j].first.get<0>() && m_relsmap[i].second.get<1>() != m_relsmap[j].first.get<1>()) || intersec.empty())
516 || ((m_relsmap[i].second.get<0>() == m_relsmap[j].second.get<0>() && m_relsmap[i].second.get<1>() != m_relsmap[j].second.get<1>()) || intersec.empty())
517 || (m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()) )
518 {
519 exclusions[i].insert(j);
520 exclusions[j].insert(i);
521 }
522 }
523 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
524 {
525 std::set< uint64_t > intersec;
526
527 std::insert_iterator< std::set< uint64_t > > ins(intersec, intersec.end());
528 std::set_intersection(m_relsmap[i].first.get<2>().begin(), m_relsmap[i].first.get<2>().end(),
529 m_nodesmap[j].get<2>().begin(), m_nodesmap[j].get<2>().end(), ins);
530 if (
531 ((m_relsmap[i].first.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].first.get<1>() != m_nodesmap[j].get<1>()) || intersec.empty() )
532 || ((m_relsmap[i].second.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].second.get<1>() != m_nodesmap[j].get<1>()) || intersec.empty() ) )
533 {
534 exclusions[i].insert(i+j);
535 exclusions[i+j].insert(i);
536 }
537 }
538 }
539 for (uint64_t i = 0; i < m_nodesmap.size(); i++)
540 {
541 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
542 {
543 std::set< uint64_t > intersec;
544
545 std::insert_iterator< std::set< uint64_t > > ins(intersec, intersec.end());
546 std::set_intersection(m_nodesmap[i].get<2>().begin(), m_nodesmap[i].get<2>().end(),
547 m_nodesmap[j].get<2>().begin(), m_nodesmap[j].get<2>().end(), ins);
548 if ( ((m_nodesmap[i].get<0>() == m_nodesmap[j].get<0>()) && (m_nodesmap[i].get<1>() != m_nodesmap[j].get<1>())) || (!intersec.empty()) )
549 {
550 exclusions[m_relsmap.size()+i].insert(m_relsmap.size()+i+j);
551 exclusions[m_relsmap.size()+i+j].insert(m_relsmap.size()+i);
552 }
553 }
554 }
555
556 return exclusions;
557 */
558}
559
560// remplir le tableau des tailles
561std::map< uint64_t, bool > DepGraphCompoundsBuildVisitorPrivate::computeSizes()
562{
563/*
564 Critical Function : comment logging messages
565*/
566// MORPHOLOGINIT;
567// LDEBUG << "Computing sizes";
568 std::map< uint64_t, bool > sizes;
569 for (uint64_t i = 0; i < m_relsmap.size(); i++)
570 {
571 sizes.insert(std::make_pair(i, true));
572 }
573 for (uint64_t i = m_relsmap.size(); i < m_relsmap.size()+m_nodesmap.size(); i++)
574 {
575 sizes.insert(std::make_pair(i, false));
576 }
577 return sizes;
578}
579
580// calculer l'ordre
581std::vector< uint64_t > DepGraphCompoundsBuildVisitorPrivate::computeOrder(
582 const std::map< uint64_t,
583 std::set< uint64_t > >& exclusions)
584{
585/*
586 Critical Function : comment logging messages
587*/
588// MORPHOLOGINIT;
589// LDEBUG << "Computing order";
590 std::vector< uint64_t > order;
591 std::multimap< uint64_t, uint64_t > tmp;
592 std::map< uint64_t, std::set< uint64_t > >::const_iterator it, it_end;
593 it = exclusions.begin(); it_end = exclusions.end();
594 for (; it != it_end; it++)
595 {
596 tmp.insert(std::make_pair((*it).second.size(), (*it).first));
597 }
598
599 std::multimap< uint64_t, uint64_t >::const_reverse_iterator tmpIt, tmpIt_end;
600 tmpIt = tmp.rbegin(); tmpIt_end = tmp.rend();
601 for (; tmpIt != tmpIt_end; tmpIt++)
602 {
603 order.push_back((*tmpIt).second);
604 }
605 return order;
606}
607
608DependencyGraphVertex DepGraphCompoundsBuildVisitorPrivate::findFinalSourceOfCompoundInRelationsSet(
609 std::set< uint64_t >& relations)
610{
611// #ifdef DEBUG_LP
612// MORPHOLOGINIT;
613// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::findFinalSourceOfCompoundInRelationsSet"
614// << relations.size();
615// LDEBUG << "Critical function : comment logging messages"
616// #endif
617 // le seul noeud cible d'une relation qui n'est pas aussi source
618
619 // ne devrait pas arriver ; test de precaution
620 if (relations.empty())
621 {
623 LERROR << "Trying to find a source of compound in an empty set !";
624 return 0;
625 }
626
627 std::set< DependencyGraphVertex > sources, targets;
628 std::set< uint64_t >::const_iterator it, it_end;
629 it = relations.begin(); it_end = relations.end();
630 for (; it != it_end; it++)
631 {
632 sources.insert(m_relsmap[*it].first.get<1>());
633 targets.insert(m_relsmap[*it].second.get<1>());
634 }
635 std::set< DependencyGraphVertex > res;
636 std::insert_iterator< std::set< DependencyGraphVertex > > ins(res, res.end());
637 std::set_difference(targets.begin(), targets.end(),
638 sources.begin(), sources.end(), ins );
639
640 // si il y a un cycle de relations qui n'etait pas detectable
641 if (res.empty())
642 {
643// #ifdef DEBUG_LP
644// LDEBUG << "No final source of compound found in a proposed set !";
645// LDEBUG << " There was probably a dependencies cycle.";
646// LDEBUG << " Trying by removing a random relation.";
647// #endif
648 // retrait d'une relation au hasard et nouvel essai
649 relations.erase(*relations.rbegin());
650 return findFinalSourceOfCompoundInRelationsSet(relations);
651 }
652 // possible d'avoir plusieurs relations vers la meme tete !!!
653 DependencyGraphVertex result = *(res.begin());
654// #ifdef DEBUG_LP
655// if (res.size() > 1)
656// {
657// LDEBUG << "Multiple sources of compound found in a proposed set ! Choosing one randomly..." << result;
658// }
659// #endif
660 return result;
661
662}
663
665AnnotationGraphVertex DepGraphCompoundsBuildVisitorPrivate::buildTermFor(
667 const std::set< uint64_t > relations,
668 bool theHead)
669{
670 LinguisticGraphVertex vxTokVertex = m_syntacticData->tokenVertexForDepVertex(vx);
671#ifdef DEBUG_LP
673 LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::buildTermFor" << vx << vxTokVertex << theHead;
674#endif
675
676
677 AnnotationGraphVertex cpdAnnotVertex = createAnnotationVertex(vxTokVertex);
678
681 if (theHead)
682 {
683 m_annotationData->annotate(cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("cpdHead"), 1);
684 m_annotationData->addMatching("PosGraph", vxTokVertex, "cpdHead", cpdAnnotVertex);
685 }
686
687
688// EdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *m_depGraph);
689
690 // Gets the source vertices of the non-compound relations (determiners,
691 // adverbs, etc.)
692 DependencyGraphInEdgeIt init, init_end;
693 boost::tie(init, init_end) = in_edges(vx, *m_depGraph);
694 for (; init != init_end; init++)
695 {
696 if ( !isDepEdgeACompoundNounRelation(*init) )
697 {
698#ifdef DEBUG_LP
699 LDEBUG << "building annotation for non-compound relation " << source( *init, *m_depGraph ) << " -> " << target( *init, *m_depGraph );
700#endif
703 DependencyGraphVertex inSrc = source( *init, *m_depGraph );
704 LinguisticGraphVertex inSrcMorph = m_syntacticData->tokenVertexForDepVertex(inSrc);
705 ConceptModifier modifier= createModifierFor(inSrcMorph);
706// if (modifier.getConceptType() == MediaticData::MediaticData::single().getConceptType("LatticeDown"))
707 if (modifier.getConceptType() == m_context->getConceptType("LatticeDown"))
708 {
709#ifdef DEBUG_LP
710 LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::buildTermFor Not storable concept type" << modifier.getConceptType()
711 << ". aborting this annotation.";
712#endif
713 break;
714 }
715 if (!m_annotationData->hasAnnotation(cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("CompoundTokenAnnotation")))
716 {
717 CompoundTokenAnnotation annot;
718 GenericAnnotation ga(annot);
719 m_annotationData->annotate(cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("CompoundTokenAnnotation"), ga);
720 }
721 CompoundTokenAnnotation* annot = m_annotationData->annotation(cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("CompoundTokenAnnotation")).pointerValue<CompoundTokenAnnotation>();
722 if (annot != 0)
723 annot->push_back(modifier);
724 else
725 {
727 LERROR << "DepGraphCompoundsBuildVisitor::buildTermFor annotation CompoundTokenAnnotation has not been created for" << cpdAnnotVertex;
728 }
729 }
730 }
731
732 // Gets the source vertices of this compound's relations
733 std::set< DependencyGraphVertex > governors;
734
735
736 for (auto it = relations.cbegin(), it_end = relations.cend();
737 it != it_end;
738 it++)
739 {
740 if (m_relsmap[*it].second.get<1>() == vx)
741 {
742 DependencyGraphVertex srcDepVertex = m_relsmap[*it].first.get<1>();
743#ifdef DEBUG_LP
744 LDEBUG << "building annotation for compound relation " << srcDepVertex << " -> " << vx;
745#endif
746 Common::MediaticData::SyntacticRelationId relType = m_relsmap[*it].first.get<3>();
747 if (governors.find(srcDepVertex) == governors.end())
748 {
749 governors.insert(srcDepVertex);
750#ifdef DEBUG_LP
751 LDEBUG << "recursive call from" << vx << vxTokVertex;
752#endif
753 AnnotationGraphVertex srcAnnotVertex = buildTermFor(srcDepVertex, relations);
754#ifdef DEBUG_LP
755 LinguisticGraphVertex srcTokVertex = m_syntacticData->tokenVertexForDepVertex(srcDepVertex);
756 LDEBUG << "returned from recursive call on" << srcDepVertex << srcTokVertex;
757#endif
759
760 if (!m_annotationData->hasAnnotation(srcAnnotVertex, "cpdExt"))
761 {
762 m_annotationData->annotate(srcAnnotVertex, "cpdExt", 1);
763 m_annotationData->addMatching("PosGraph", m_syntacticData->tokenVertexForDepVertex(srcDepVertex), "cpdExt", srcAnnotVertex);
764 }
765 if (!m_annotationData->hasAnnotation(srcAnnotVertex, cpdAnnotVertex, "CompoundTokenAnnotation"))
766 {
767 CompoundTokenAnnotation edgeAnnot;
768 GenericAnnotation ga(edgeAnnot);
769 m_annotationData->annotate(srcAnnotVertex, cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("CompoundTokenAnnotation"), ga);
770 }
771 if (!m_annotationData->hasAnnotation(srcAnnotVertex, cpdAnnotVertex, "CompoundTokenAnnotation"))
772 {
774 LERROR << "DepGraphCompoundsBuildVisitor::buildTermFor annotation CompoundTokenAnnotation has not been created for" << srcAnnotVertex << "->" << cpdAnnotVertex;
775 continue;
776 }
777 CompoundTokenAnnotation* edgeAnnot = m_annotationData->annotation(srcAnnotVertex, cpdAnnotVertex, Common::Misc::utf8stdstring2limastring("CompoundTokenAnnotation")).pointerValue<CompoundTokenAnnotation>();
778 if (relType == static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationId("ADJPRENSUB")
779 || relType == static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationId("SUBADJPOST"))
780 {
782 //ConceptModifier mod(static_cast<StringsPoolIndex>(0),m_context->getConceptType("Complementer"));
783 Lima::Common::MediaticData::ConceptType type = m_context->getConceptType("Complementer");
784 ConceptModifier mod(static_cast<StringsPoolIndex>(0),type);
785 //ConceptModifier mod(static_cast<StringsPoolIndex>(0));
786
787 edgeAnnot->push_back(mod);
788 }
789 else if (relType == static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationId("COMPDUNOM"))
790 {
791// LDEBUG << "Building annot for COMPDUNOM";
795 EdgeDepRelTypePropertyMap relTypeMap =
796 get(edge_deprel_type, *m_depGraph);
797
798 StringsPoolIndex modString = static_cast<StringsPoolIndex>(0);
799 std::vector< uint64_t > modReferences;
800 DependencyGraphInEdgeIt modInit, modInit_end;
801 boost::tie(modInit, modInit_end) = in_edges(srcDepVertex, *m_depGraph);
802 for (; modInit != modInit_end; modInit++)
803 {
804 uint64_t modRelType = relTypeMap[*modInit];
805 if (modRelType == static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationId("PREPSUB"))
806 {
807 VertexDataPropertyMap dataMap = get(vertex_data, *(m_syntacticData->graph()));
808 const MorphoSyntacticData* data = dataMap[source(*modInit, *m_depGraph)];
809 if (data != 0 && !data->empty())
810 {
811 modString = *(data->allLemma().begin());
812#ifdef DEBUG_LP
813 LDEBUG << "Found modifier '" <<Common::MediaticData::MediaticData::changeable().stringsPool(m_language)[modString] << "'";
814#endif
815 modReferences.push_back(m_syntacticData->tokenVertexForDepVertex(source(*modInit, *m_depGraph)));
816 }
817 break;
818 }
819 }
820 ConceptModifier mod(
821 modString,
822 m_context->getConceptType("Complementer"),
823 modReferences);
824 edgeAnnot->push_back(mod);
825 }
826 }
827 }
828 }
829#ifdef DEBUG_LP
830 LDEBUG << "DepGraphCompoundsBuildVisitor: == DONE buildTermFor " << vx;
831#endif
832 return cpdAnnotVertex;
833}
834
835AnnotationGraphVertex DepGraphCompoundsBuildVisitorPrivate::createAnnotationVertex(
836 const LinguisticGraphVertex v)
837{
838#ifdef DEBUG_LP
840 LDEBUG << "Creating annotation for morph vertex " << v;
841#endif
842
843 AnnotationGraphVertex agv = m_annotationData->createAnnotationVertex();
844 m_annotationData->addMatching("PosGraph", v, "cpd", agv);
845 m_annotationData->annotate(agv, Common::Misc::utf8stdstring2limastring("PosGraph"), v);
846
847 /* Inutile, la presence de la CompoundTokenAnnotation sera suffisante pour cela
848
850 m_annotationData->annotate(agv, Common::Misc::utf8stdstring2limastring("cpd"), 1);
851 */
852
855#ifdef DEBUG_LP
856 LDEBUG << "Built annotation vertex " << agv;
857#endif
858 return agv;
859}
860
861
862std::vector< DependencyGraphEdge >
863 DepGraphCompoundsBuildVisitorPrivate::getCompoundsRelationsFromWithChain(
865 const ChainIdStruct& chId)
866{
867 #ifdef DEBUG_LP
869 LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::getCompoundsRelationsFromWithChain"
870 << from << " on chain " << chId;
871 LDEBUG << "Critical function : comment logging messages";
872 #endif
873 std::set< DependencyGraphVertex > stackedVxs;
874 std::list< DependencyGraphVertex > vxsToExplore;
875 vxsToExplore.push_back(from);
876 stackedVxs.insert(from);
877 std::vector< DependencyGraphEdge > collectedEdges;
878 std::set< std::pair<DependencyGraphVertex, DependencyGraphVertex> > collectedEdgesSet;
879
880 VertexChainIdPropertyMap chainsMap = get(vertex_chain_id, *(m_syntacticData->graph()));
881 while (!vxsToExplore.empty())
882 {
883 DependencyGraphVertex vx = vxsToExplore.front();
884 #ifdef DEBUG_LP
885 LDEBUG << "Exploring from" << vx;
886 #endif
887 m_visited.insert(vx);
888 vxsToExplore.pop_front();
889 DependencyGraphOutEdgeIt outit, outit_end;
890 boost::tie(outit, outit_end) = out_edges(vx, *m_depGraph);
891 for (; outit != outit_end; outit++)
892 {
893 #ifdef DEBUG_LP
894 LDEBUG << "Looking at " << vx << " out relation ";
895 #endif
896 LinguisticGraphVertex tgtTokVertex = m_syntacticData->tokenVertexForDepVertex(target(*outit, *m_depGraph));
897 if ( isDepEdgeACompoundNounRelation(*outit) &&
898 chainsMap[tgtTokVertex].find(chId) != chainsMap[tgtTokVertex].end())
899 {
900 #ifdef DEBUG_LP
901 LDEBUG << "Elected";
902 #endif
903 if (stackedVxs.find(target(*outit, *m_depGraph)) == stackedVxs.end())
904 {
905 vxsToExplore.push_back(target(*outit, *m_depGraph));
906 stackedVxs.insert(target(*outit, *m_depGraph));
907 }
908 if (collectedEdgesSet.find(std::make_pair(source(*outit,*m_depGraph),target(*outit,*m_depGraph))) == collectedEdgesSet.end())
909 {
910 #ifdef DEBUG_LP
911 LDEBUG << " and collected";
912 #endif
913 collectedEdges.push_back(*outit);
914 collectedEdgesSet.insert(std::make_pair(source(*outit,*m_depGraph),target(*outit,*m_depGraph)));
915 m_visitedWithChains.insert(std::make_pair(target(*outit, *m_depGraph),chId));
916 }
917 else
918 {
919 #ifdef DEBUG_LP
920 LDEBUG << " but not collected";
921 #endif
922 }
923 }
924 else
925 {
926 #ifdef DEBUG_LP
927 LDEBUG << "Not collected because not electable because ";
928 //LDEBUG << "\t- " << (*outit) << " is not a compounds nouns relation ";
929 LDEBUG << "\t- or " << chId << " is not a chain of " << tgtTokVertex;
930 #endif
931 }
932 }
933 DependencyGraphInEdgeIt init, init_end;
934 boost::tie(init, init_end) = in_edges(vx, *m_depGraph);
935 for (; init != init_end; init++)
936 {
937#ifdef DEBUG_LP
938 LDEBUG << "Looking at " << vx << " in relation ";
939// << MediaticData::single().getEntityName(relTypeMap[*init])
940// << " on " << *init;
941#endif
942 LinguisticGraphVertex srcTokVertex = m_syntacticData->tokenVertexForDepVertex(source(*init, *m_depGraph));
943 if ( isDepEdgeACompoundNounRelation(*init) &&
944 chainsMap[srcTokVertex].find(chId) != chainsMap[srcTokVertex].end())
945 {
946// LDEBUG << "Elected";
947 if (stackedVxs.find(source(*init, *m_depGraph)) == stackedVxs.end())
948 {
949 vxsToExplore.push_back(source(*init, *m_depGraph));
950 stackedVxs.insert(source(*init, *m_depGraph));
951 }
952 if (collectedEdgesSet.find(std::make_pair(source(*init,*m_depGraph),target(*init,*m_depGraph))) == collectedEdgesSet.end())
953 {
954// LDEBUG << " and collected";
955 collectedEdges.push_back(*init);
956 collectedEdgesSet.insert(std::make_pair(source(*init,*m_depGraph),target(*init,*m_depGraph)));
957 m_visitedWithChains.insert(std::make_pair(source(*init, *m_depGraph),chId));
958 }
959// else
960// {
961// LDEBUG << " but not collected because " << chId << " is not a chain of " << srcTokVertex;
962// }
963 }
964// else
965// {
966// LDEBUG << "Not collected because not electable because ";
967// LDEBUG << "\t- " << (*init) << " is not a compounds nouns relation ";
968// LDEBUG << "\t- or " << chId << " is not a chain of " << srcTokVertex;
969// }
970 }
971 }
972 return collectedEdges;
973}
974
975std::vector< DependencyGraphEdge >
976 DepGraphCompoundsBuildVisitorPrivate::getCompoundsRelationsFromNoChain(
978{
979 #ifdef DEBUG_LP
981 LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::getCompoundsRelationsFromNoChain"
982 << from;
983 LDEBUG << "Critical function : comment logging messages";
984 #endif
985 std::set< DependencyGraphVertex > stackedVxs;
986 std::list< DependencyGraphVertex > vxsToExplore;
987 vxsToExplore.push_back(from);
988 stackedVxs.insert(from);
989 std::vector< DependencyGraphEdge > collectedEdges;
990 std::set< std::pair<DependencyGraphVertex, DependencyGraphVertex> > collectedEdgesSet;
991
992 while (!vxsToExplore.empty())
993 {
994 DependencyGraphVertex vx = vxsToExplore.front();
995 #ifdef DEBUG_LP
996 LDEBUG << "Exploring from" << vx;
997 #endif
998 m_visited.insert(vx);
999 vxsToExplore.pop_front();
1000 DependencyGraphOutEdgeIt outit, outit_end;
1001 boost::tie(outit, outit_end) = out_edges(vx, *m_depGraph);
1002 for (; outit != outit_end; outit++)
1003 {
1004 #ifdef DEBUG_LP
1005 LDEBUG << "Looking at " << vx << " out relation ";
1006 #endif
1007 if ( isDepEdgeACompoundNounRelation(*outit) )
1008 {
1009 #ifdef DEBUG_LP
1010 LDEBUG << "Elected";
1011 #endif
1012 if (stackedVxs.find(target(*outit, *m_depGraph)) == stackedVxs.end())
1013 {
1014 vxsToExplore.push_back(target(*outit, *m_depGraph));
1015 stackedVxs.insert(target(*outit, *m_depGraph));
1016 }
1017 if (collectedEdgesSet.find(std::make_pair(source(*outit,*m_depGraph),target(*outit,*m_depGraph))) == collectedEdgesSet.end())
1018 {
1019 #ifdef DEBUG_LP
1020 LDEBUG << " and collected";
1021 #endif
1022 collectedEdges.push_back(*outit);
1023 collectedEdgesSet.insert(std::make_pair(source(*outit,*m_depGraph),target(*outit,*m_depGraph)));
1024 }
1025 else
1026 {
1027 #ifdef DEBUG_LP
1028 LDEBUG << " but not collected";
1029 #endif
1030 }
1031 }
1032 else
1033 {
1034 #ifdef DEBUG_LP
1035 LDEBUG << "Not collected because not electable because ";
1036// LDEBUG << "\t- " << (*outit) << " is not a compounds nouns relation ";
1037 #endif
1038 }
1039 }
1040 DependencyGraphInEdgeIt init, init_end;
1041 boost::tie(init, init_end) = in_edges(vx, *m_depGraph);
1042 for (; init != init_end; init++)
1043 {
1044#ifdef DEBUG_LP
1045 LDEBUG << "Looking at " << vx << " in relation ";
1046// << MediaticData::single().getEntityName(relTypeMap[*init])
1047// << " on " << *init;
1048#endif
1049 if ( isDepEdgeACompoundNounRelation(*init) )
1050 {
1051// LDEBUG << "Elected";
1052 if (stackedVxs.find(source(*init, *m_depGraph)) == stackedVxs.end())
1053 {
1054 vxsToExplore.push_back(source(*init, *m_depGraph));
1055 stackedVxs.insert(source(*init, *m_depGraph));
1056 }
1057 if (collectedEdgesSet.find(std::make_pair(source(*init,*m_depGraph),target(*init,*m_depGraph))) == collectedEdgesSet.end())
1058 {
1059// LDEBUG << " and collected";
1060 collectedEdges.push_back(*init);
1061 collectedEdgesSet.insert(std::make_pair(source(*init,*m_depGraph),target(*init,*m_depGraph)));
1062 }
1063 }
1064// else
1065// {
1066// LDEBUG << "Not collected because not electable because ";
1067// LDEBUG << "\t- " << (*init) << " is not a compounds nouns relation ";
1068// }
1069 }
1070 }
1071 return collectedEdges;
1072}
1073
1074// calculer les incompatibilites binaires
1075std::map< uint64_t, std::set< uint64_t > >
1076 DepGraphCompoundsBuildVisitorPrivate::computeExclusionsWithChain(
1077 const std::vector< DependencyGraphEdge >& relations,
1078 const ChainIdStruct& chId)
1079{
1080// MORPHOLOGINIT;
1081// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::computeExclusionsWithChain" << chId;
1082// LDEBUG << "Critical Function : comment logging messages"
1083 std::map< uint64_t, std::set< uint64_t > > exclusions;
1084 EdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *m_depGraph);
1085// VertexChainIdPropertyMap chainsMap = get(vertex_chain_id, *(m_syntacticData->graph()));
1086 m_relsmap.clear();
1087 m_nodesmap.clear();
1088 std::vector< DependencyGraphEdge >::const_iterator relsit, relsit_end;
1089 relsit = relations.begin(); relsit_end = relations.end();
1090 //preparation des donnees
1091 for (uint64_t relNum=0; relsit != relsit_end; relsit++,relNum++)
1092 {
1093 DependencyGraphVertex src = source(*relsit, *m_depGraph);
1094 DependencyGraphVertex tgt = target(*relsit, *m_depGraph);
1095 LinguisticGraphVertex srcTokVertex = m_syntacticData->tokenVertexForDepVertex(src);
1096 LinguisticGraphVertex tgtTokVertex = m_syntacticData->tokenVertexForDepVertex(tgt);
1097 const Token* srcToken = get(vertex_token, *(m_iter->getGraph()), src);
1098 const Token* tgtToken = get(vertex_token, *(m_iter->getGraph()), tgt);
1099 uint64_t srcPos = srcToken->position();
1100 uint64_t tgtPos = tgtToken->position();
1101 std::set<uint64_t> srcChains;
1102 srcChains.insert(chId.chainId());
1103 std::set<uint64_t> tgtChains;
1104 tgtChains.insert(chId.chainId());
1105 Common::MediaticData::SyntacticRelationId relType = relTypeMap[boost::edge(src,tgt,*m_depGraph).first];
1106// LDEBUG << srcTokVertex << " -> " << tgtTokVertex << " is number: " << relNum;
1107 m_relsmap.push_back(std::make_pair(boost::make_tuple(srcPos,srcTokVertex,srcChains,relType),boost::make_tuple(tgtPos,tgtTokVertex,tgtChains,relType)));
1108 m_nodesmap.push_back(boost::make_tuple(srcPos,srcTokVertex,srcChains));
1109 m_nodesmap.push_back(boost::make_tuple(tgtPos,tgtTokVertex,tgtChains));
1110 }
1111 for (uint64_t i = 0; i < m_relsmap.size(); i++)
1112 {
1113 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
1114 }
1115 for (uint64_t i = m_relsmap.size(); i < m_relsmap.size()+m_nodesmap.size(); i++)
1116 {
1117 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
1118 }
1119 // return exclusions;
1120 // calcul des exclusions
1121 for (uint64_t i = 0; i < m_relsmap.size(); i++)
1122 {
1123 // incompatibilites entre relations
1124 for (uint64_t j = i+1; j < m_relsmap.size(); j++)
1125 {
1126 if (
1127 // Les positions des sources sont egales
1128 ( (m_relsmap[i].first.get<0>() == m_relsmap[j].first.get<0>())
1129 /* && ( m_relsmap[i].first.get<1>() != m_relsmap[j].first.get<1>()
1130 || m_relsmap[i].first.get<3>() != m_relsmap[j].first.get<3>())*/
1131 )
1132 ||
1133 // position de la source du premier egal position de la cible du second
1134 // mais les 2 vertex sont differents
1135 ( (m_relsmap[i].first.get<0>() == m_relsmap[j].second.get<0>())
1136 && ( m_relsmap[i].first.get<1>() != m_relsmap[j].second.get<1>()
1137 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1138 ||
1139 // position de la source du second egal position de la cible du premier
1140 // mais les 2 vertex sont differents
1141 ( (m_relsmap[i].second.get<0>() == m_relsmap[j].first.get<0>())
1142 && ( m_relsmap[i].second.get<1>() != m_relsmap[j].first.get<1>()
1143 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1144 ||
1145 // position des deux cibles egales mais vertex differents
1146 ( (m_relsmap[i].second.get<0>() == m_relsmap[j].second.get<0>())
1147 && ( m_relsmap[i].second.get<1>() != m_relsmap[j].second.get<1>()
1148 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1149 )
1150 {
1151// LDEBUG << i << " and " << j << " are incompatible";
1152 exclusions[i].insert(j);
1153 exclusions[j].insert(i);
1154 }
1155// else
1156// {
1157 //std::cerr << m_relsmap[i].first.get<0>() << " / " << m_relsmap[j].first.get<0>() << std::endl;
1158
1159// LDEBUG << i << " and " << j << " are compatible: ";
1160// }
1161 }
1162 // incompatibilites entre une relation et les noeuds
1163 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
1164 {
1165 if (
1166 // source: meme position que le noeud mais vertex differents
1167 (m_relsmap[i].first.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].first.get<1>() != m_nodesmap[j].get<1>())
1168 // cible: meme position que le noeud mais vertex differents
1169 || (m_relsmap[i].second.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].second.get<1>() != m_nodesmap[j].get<1>()) )
1170 {
1171// LDEBUG << i << " and " << i+j << " are incompatible";
1172 exclusions[i].insert(i+j);
1173 exclusions[i+j].insert(i);
1174 }
1175// else
1176// {
1177// LDEBUG << i << " and " << i+j << " are compatible";
1178// }
1179 }
1180 }
1181 // incompatibilites entre les noeuds
1182 for (uint64_t i = 0; i < m_nodesmap.size(); i++)
1183 {
1184 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
1185 {
1186 // memes positions mais vertex differents
1187 if ( (m_nodesmap[i].get<0>() == m_nodesmap[j].get<0>()) && (m_nodesmap[i].get<1>() != m_nodesmap[j].get<1>()))
1188 {
1189// LDEBUG << m_relsmap.size()+i << " and " << m_relsmap.size()+i+j << " are incompatible";
1190 exclusions[m_relsmap.size()+i].insert(m_relsmap.size()+i+j);
1191 exclusions[m_relsmap.size()+i+j].insert(m_relsmap.size()+i);
1192 }
1193// else
1194// {
1195// LDEBUG << m_relsmap.size()+i << " and " << m_relsmap.size()+i+j << " are compatible";
1196// }
1197 }
1198 }
1199
1200 return exclusions;
1201}
1202
1203// calculer les incompatibilites binaires
1204std::map< uint64_t, std::set< uint64_t > >
1205 DepGraphCompoundsBuildVisitorPrivate::computeExclusionsNoChain(
1206 const std::vector< DependencyGraphEdge >& relations)
1207{
1208// MORPHOLOGINIT;
1209// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::computeExclusionsNoChain" << chId;
1210// LDEBUG << "Critical Function : comment logging messages"
1211 std::map< uint64_t, std::set< uint64_t > > exclusions;
1212 EdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *m_depGraph);
1213 m_relsmap.clear();
1214 m_nodesmap.clear();
1215 std::vector< DependencyGraphEdge >::const_iterator relsit, relsit_end;
1216 relsit = relations.begin(); relsit_end = relations.end();
1217 //preparation des donnees
1218 for (uint64_t relNum=0; relsit != relsit_end; relsit++,relNum++)
1219 {
1220 DependencyGraphVertex src = source(*relsit, *m_depGraph);
1221 DependencyGraphVertex tgt = target(*relsit, *m_depGraph);
1222 LinguisticGraphVertex srcTokVertex = m_syntacticData->tokenVertexForDepVertex(src);
1223 LinguisticGraphVertex tgtTokVertex = m_syntacticData->tokenVertexForDepVertex(tgt);
1224 const Token* srcToken = get(vertex_token, *(m_iter->getGraph()), src);
1225 const Token* tgtToken = get(vertex_token, *(m_iter->getGraph()), tgt);
1226 uint64_t srcPos = srcToken->position();
1227 uint64_t tgtPos = tgtToken->position();
1228 std::set<uint64_t> srcChains;
1229 std::set<uint64_t> tgtChains;
1230 Common::MediaticData::SyntacticRelationId relType = relTypeMap[boost::edge(src,tgt,*m_depGraph).first];
1231// LDEBUG << srcTokVertex << " -> " << tgtTokVertex << " is number: " << relNum;
1232 m_relsmap.push_back(std::make_pair(boost::make_tuple(srcPos,srcTokVertex,srcChains,relType),boost::make_tuple(tgtPos,tgtTokVertex,tgtChains,relType)));
1233 m_nodesmap.push_back(boost::make_tuple(srcPos,srcTokVertex,srcChains));
1234 m_nodesmap.push_back(boost::make_tuple(tgtPos,tgtTokVertex,tgtChains));
1235 }
1236 for (uint64_t i = 0; i < m_relsmap.size(); i++)
1237 {
1238 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
1239 }
1240 for (uint64_t i = m_relsmap.size(); i < m_relsmap.size()+m_nodesmap.size(); i++)
1241 {
1242 exclusions.insert(std::make_pair(i, std::set< uint64_t >()));
1243 }
1244 // return exclusions;
1245 // calcul des exclusions
1246 for (uint64_t i = 0; i < m_relsmap.size(); i++)
1247 {
1248 // incompatibilites entre relations
1249 for (uint64_t j = i+1; j < m_relsmap.size(); j++)
1250 {
1251 if (
1252 // Les positions des sources sont egales
1253 ( (m_relsmap[i].first.get<0>() == m_relsmap[j].first.get<0>())
1254 /* && ( m_relsmap[i].first.get<1>() != m_relsmap[j].first.get<1>()
1255 || m_relsmap[i].first.get<3>() != m_relsmap[j].first.get<3>())*/
1256 )
1257 ||
1258 // position de la source du premier egal position de la cible du second
1259 // mais les 2 vertex sont differents
1260 ( (m_relsmap[i].first.get<0>() == m_relsmap[j].second.get<0>())
1261 && ( m_relsmap[i].first.get<1>() != m_relsmap[j].second.get<1>()
1262 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1263 ||
1264 // position de la source du second egal position de la cible du premier
1265 // mais les 2 vertex sont differents
1266 ( (m_relsmap[i].second.get<0>() == m_relsmap[j].first.get<0>())
1267 && ( m_relsmap[i].second.get<1>() != m_relsmap[j].first.get<1>()
1268 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1269 ||
1270 // position des deux cibles egales mais vertex differents
1271 ( (m_relsmap[i].second.get<0>() == m_relsmap[j].second.get<0>())
1272 && ( m_relsmap[i].second.get<1>() != m_relsmap[j].second.get<1>()
1273 /*|| m_relsmap[i].first.get<3>() == m_relsmap[j].first.get<3>()*/))
1274 )
1275 {
1276// LDEBUG << i << " and " << j << " are incompatible";
1277 exclusions[i].insert(j);
1278 exclusions[j].insert(i);
1279 }
1280// else
1281// {
1282 //std::cerr << m_relsmap[i].first.get<0>() << " / " << m_relsmap[j].first.get<0>() << std::endl;
1283
1284// LDEBUG << i << " and " << j << " are compatible: ";
1285// }
1286 }
1287 // incompatibilites entre une relation et les noeuds
1288 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
1289 {
1290 if (
1291 // source: meme position que le noeud mais vertex differents
1292 (m_relsmap[i].first.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].first.get<1>() != m_nodesmap[j].get<1>())
1293 // cible: meme position que le noeud mais vertex differents
1294 || (m_relsmap[i].second.get<0>() == m_nodesmap[j].get<0>() && m_relsmap[i].second.get<1>() != m_nodesmap[j].get<1>()) )
1295 {
1296// LDEBUG << i << " and " << i+j << " are incompatible";
1297 exclusions[i].insert(i+j);
1298 exclusions[i+j].insert(i);
1299 }
1300// else
1301// {
1302// LDEBUG << i << " and " << i+j << " are compatible";
1303// }
1304 }
1305 }
1306 // incompatibilites entre les noeuds
1307 for (uint64_t i = 0; i < m_nodesmap.size(); i++)
1308 {
1309 for (uint64_t j = 0; j < m_nodesmap.size(); j++)
1310 {
1311 // memes positions mais vertex differents
1312 if ( (m_nodesmap[i].get<0>() == m_nodesmap[j].get<0>()) && (m_nodesmap[i].get<1>() != m_nodesmap[j].get<1>()))
1313 {
1314// LDEBUG << m_relsmap.size()+i << " and " << m_relsmap.size()+i+j << " are incompatible";
1315 exclusions[m_relsmap.size()+i].insert(m_relsmap.size()+i+j);
1316 exclusions[m_relsmap.size()+i+j].insert(m_relsmap.size()+i);
1317 }
1318// else
1319// {
1320// LDEBUG << m_relsmap.size()+i << " and " << m_relsmap.size()+i+j << " are compatible";
1321// }
1322 }
1323 }
1324
1325 return exclusions;
1326}
1327
1328ConceptModifier DepGraphCompoundsBuildVisitorPrivate::createModifierFor(
1329 LinguisticGraphVertex inSrcMorph)
1330{
1331// #ifdef DEBUG_LP
1332// MORPHOLOGINIT;
1333// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::createModifierFor " << inSrcMorph;
1334// #endif
1335 VertexDataPropertyMap dataMap = get(vertex_data, *(m_syntacticData->graph()));
1336 const MorphoSyntacticData* data = dataMap[inSrcMorph];
1337 LinguisticCode macro;
1338 StringsPoolIndex realization = static_cast<StringsPoolIndex>(0);
1339 if (data != 0 && !data->empty())
1340 {
1341 macro = m_macroAccessor->readValue(data->begin()->properties);
1342 if (!data->allLemma().empty())
1343 {
1344 realization = *(data->allLemma().begin());
1345 }
1346 }
1347 // MediaticData::ConceptType conceptType = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getConceptForMacro(macro);
1348 MediaticData::ConceptType conceptType = m_context->getConceptForMacro(/* m_language, */macro);
1349
1350 std::vector< uint64_t > references;
1351// #ifdef DEBUG_LP
1352// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::createModifierFor Got macro " << macro;
1353// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::createModifierFor Got conceptType " << conceptType;
1354// LDEBUG << "DepGraphCompoundsBuildVisitorPrivate::createModifierFor Got realization "
1355// << realization << Common::MediaticData::MediaticData::single().stringsPool(m_language)[realization];
1356// #endif
1357 if (conceptType == m_context->getConceptType("Determiner")
1358 || conceptType == m_context->getConceptType("SemanticModifier") )
1359 {
1360 references.push_back(inSrcMorph);
1361 }
1362 return ConceptModifier(realization, conceptType, references);
1363}
1364
1365} // closing namespace Compounds
1366} // closing namespace LinguisticProcessing
1367} // closing namespace Lima
1368
This file is the main header file for the data related to annotation graphs.
DependencyGraph::edge_descriptor DependencyGraphEdge
typedefs to simplify the acces to various graphs elements
DependencyGraph::in_edge_iterator DependencyGraphInEdgeIt
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 LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
boost::property_map< LinguisticGraph, vertex_chain_id_t >::type VertexChainIdPropertyMap
boost::property_map< LinguisticGraph, vertex_data_t >::type VertexDataPropertyMap
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
@ vertex_data
@ vertex_chain_id
#define MORPHOLOGINIT
Data used for the syntactic analyzis of texts.
Holds an annotation graph and gives an API to manipulate it.
void addMatching(const std::string &first, AnnotationGraphVertex firstVx, const std::string &second, AnnotationGraphVertex secondVx)
Adds a symetric matching between two vertices of two graphs identified by the two string parameters.
const GenericAnnotation & annotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
AnnotationGraphVertex createAnnotationVertex()
Creates a new annotation vertex in the graph.
Holds linguistic data for one language.
const FsaStringsPool & stringsPool(MediaId med) const
const MediaData & mediaData(MediaId media) const
Provide function to read write and check a property.
LinguisticCode readValue(const LinguisticCode &code) const
read a property in a coded int.
Lima::Common::MediaticData::ConceptType getConceptType(const std::string &typeName) const
Lima::Common::MediaticData::ConceptType getConceptForMacro(const LinguisticCode &macro) const
std::list< std::set< uint64_t > > computeCompatibilitiesWithChain(std::map< uint64_t, std::set< uint64_t > > &exclusions, std::map< uint64_t, bool > &sizes, std::vector< uint64_t > &order)
This enumeration lists the types of annotations used to specify the role of an annotation in a compou...
DepGraphCompoundsBuildVisitor(const Lima::LinguisticProcessing::Compounds::CompoundsBuilderFromSyntacticData *cpbfsd, MediaId language, SyntacticAnalysis::SyntacticData *syntacticData, DependencyGraph *depGraph, LinguisticAnalysisStructure::AnalysisGraph *iter, LinguisticGraphVertex startVertex, LinguisticGraphVertex stopVertex, Common::AnnotationGraphs::AnnotationData *annotationData)
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
This class points to a graph, its dependency graph and the structure that holds the maping between th...
LinguisticGraphVertex tokenVertexForDepVertex(const DependencyGraphVertex &v) const
DependencyGraphVertex depVertexForTokenVertex(const LinguisticGraphVertex &v) const
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
void dumpFunction(const std::string &annot, const Dumper *f)
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
bool hasAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
void annotate(AnnotationGraphVertex v, const LimaString &annot, uint64_t value)
LimaString utf8stdstring2limastring(const std::string &src)
NAUTITIA.