LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SimplificationConstraints.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
19#include "DependencyGraph.h"
20#include "SyntacticData.h"
21#include "SyntagmaticMatrix.h"
22#include "SimplificationData.h"
23
26
29#include "common/LimaCommon.h"
30
31#include <string>
32
33using namespace Lima::Common::MediaticData;
36
37namespace Lima
38{
39namespace LinguisticProcessing
40{
41namespace SyntacticAnalysis
42{
43#define SASLOGINIT LOGINIT("LP::SyntacticAnalysis::Simplify")
44
46
47//**********************************************************************
48// factories for constraint functions defined in this file
51
54
57
58
59//**********************************************************************
61 const LimaString& complement):
62 ConstraintWithRelationComplement(language,complement),
63 m_language(language)
64{
65}
66
73
75 const LinguisticGraphVertex& v1,
76 const LinguisticGraphVertex& v2,
77 AnalysisContent& analysis) const
78{
80 LDEBUG << "testing SubsentenceBounds for " << v1 << " and " << v2 << " with relation: " /*<< (static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).
81 getEntityNames("SyntacticSimplification")[m_relation])*/;
82
83 auto simplificationData = std::dynamic_pointer_cast<SimplificationData>(analysis.getData("SimplificationData"));
84 if (simplificationData==0)
85 {
87 LERROR << "No simplificationData in SubsentenceBounds constraint";
88 return false;
89 }
90 if (v1 == graph.firstVertex() || v1 == graph.lastVertex() ||
91 v2 == graph.firstVertex() || v2 == graph.lastVertex() )
92 {
93 LDEBUG << "SubsentenceBounds: false";
94 return false;
95 }
96 simplificationData->subSentBounds(boost::make_tuple(v1,v2, m_relation));
97 LDEBUG << "SubsentenceBounds: true";
98 return true;
99}
100
101
102Simplify::Simplify(MediaId language,
103 const LimaString& complement):
104 ConstraintWithRelationComplement(language,complement),
105 m_language(language)
106{
107}
108
111 AnalysisContent& analysis) const
112{
113
115 LDEBUG << "Doing simplification";
116
117 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.getData("SyntacticData"));
118 auto simplificationData = std::dynamic_pointer_cast<SimplificationData>(analysis.getData("SimplificationData"));
119 LinguisticGraph* graph = syntacticData->graph();
120 if (simplificationData==0)
121 {
123 LERROR << "No simplificationData in SubsentenceBounds constraint";
124 return false;
125 }
126 std::list< boost::tuple< LinguisticGraphVertex, LinguisticGraphVertex, SyntacticRelationId > >::const_iterator simplificationsIt, simplificationsIt_end;
127 simplificationsIt = simplificationData->subSentBounds().begin();
128 simplificationsIt_end = simplificationData->subSentBounds().end();
129 bool simplificationDone = false;
130 for (; simplificationsIt != simplificationsIt_end; simplificationsIt++)
131 {
132 LinguisticGraphVertex first, last;
134 boost::tie(first, last, type) = (*simplificationsIt);
135
136 LDEBUG << "Match "
137/* << (static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).
138 getEntityNames("SyntacticSimplification")[type])*/
139 /*<< " was: " << result*/ << " with bounds "
140 << first << " / "
141 << last;
142
143 if (boost::in_degree(first, *graph)==0 || boost::out_degree(last, *graph)==0)
144 {
145 return false;
146 }
147 LDEBUG << "first ("<<first<<") has " << boost::in_degree(first, *graph) << " in edges";
148 LinguisticGraphEdge inEdge = *(boost::in_edges(first, *graph).first);
149 boost::remove_edge(inEdge, *graph);
150
151 LDEBUG << "last ("<<last<<") has " << boost::out_degree(last, *graph) << " out edges";
152 LinguisticGraphEdge outEdge = *(boost::out_edges(last, *graph).first);
153 boost::remove_edge(outEdge, *graph);
154
155 LDEBUG << "Old edges are "
156 << inEdge.m_source << " -> " << inEdge.m_target << " / "
157 << outEdge.m_source << " -> " << outEdge.m_target;
158 LDEBUG << "New edge is "
159 << source(inEdge,*graph) << " -> "
160 << target(outEdge,*graph);
161 std::pair<LinguisticGraphEdge, bool> addingResult = boost::add_edge(source(inEdge,*graph), target(outEdge,*graph), *graph);
162 if (addingResult.second == false)
163 {
165 LERROR << "Was not able to add a simplification edge.";
166 simplificationData->clearBounds();
167 return false;
168 }
169
170 simplificationData->addSimplification(inEdge, outEdge, type);
171 simplificationDone = true;
172 }
173 simplificationData->clearBounds();
174 return simplificationDone;
175}
176
177//----------------------------------------------------------------------
179 const LimaString& complement):
180 ConstraintWithRelationComplement(language,complement)
181{
182}
183
185{
186
188 LDEBUG << "clearing stored subsentences";
189
190 auto simplificationData = std::dynamic_pointer_cast<SimplificationData>(analysis.getData("SimplificationData"));
191
192 simplificationData->clearBounds();
193 return true;
194}
195
196} // end namespace SyntacticAnalysis
197} // end namespace LinguisticProcessing
198} // end namespace Lima
A graph that stores the relations of syntactic dependency between the elements of a DependencyGraph.
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
boost::graph_traits< LinguisticGraph >::edge_descriptor LinguisticGraphEdge
typedefs to simplify the access to various graphs elements
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define SASLOGINIT
this file contains the definitions of several constraint functions for the detection of subsentences
#define SubsentenceBoundsId
#define ClearStoredSubsentencesId
#define SimplifyId
Data used for the simplification of sentences allowing easier heterosyntagmatic analysis.
Data used for the syntactic analyzis of texts.
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
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraphVertex & firstVertex(void) const
Returns the first vertex of the graph.
bool operator()(AnalysisContent &analysis) const override
zero-ary constraint function : applies the function without a vertex indication (used for actions,...
ClearStoredSubsentences(MediaId language, const LimaString &complement=LimaString())
Abstract constraint function that takes a complement string that represents a relation name (entity t...
bool operator()(Automaton::RecognizerMatch &, AnalysisContent &analysis) const override
Simplify(MediaId language, const LimaString &complement=LimaString())
SubsentenceBounds(MediaId language, const LimaString &complement=LimaString())
bool operator()(const Lima::LinguisticProcessing::LinguisticAnalysisStructure::AnalysisGraph &graph, const LinguisticGraphVertex &v, AnalysisContent &analysis) const override
unary constraint function : the constraint only applies on one vertices in the graph (the graph is al...
Automaton::ConstraintFunctionFactory< ClearStoredSubsentences > ClearStoredSubsentencesFactory(ClearStoredSubsentencesId)
Automaton::ConstraintFunctionFactory< SubsentenceBounds > SubsentenceBoundsFactory(SubsentenceBoundsId)
Automaton::ConstraintFunctionFactory< Simplify > SimplifyFactory(SimplifyId)
NAUTITIA.
QString LimaString
Definition LimaString.h:33