LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SelectionalRestrictionsConstraints.cpp
Go to the documentation of this file.
1// Copyright 2002-2019 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
23#include "DependencyGraph.h"
24#include "SyntacticData.h"
25#include "SyntagmaticMatrix.h"
26
40
41#include <map>
42#include <queue>
43#include <string>
44#include <tuple>
45
46using namespace boost;
47using namespace Lima::Common::MediaticData;
48using namespace Lima::Common::AnnotationGraphs;
49using namespace Lima::Common::PropertyCode;
52
53namespace Lima
54{
55namespace LinguisticProcessing
56{
57namespace SyntacticAnalysis
58{
59
60//**********************************************************************
61// factories for constraint functions defined in this file
64
67
68
69//**********************************************************************
71 MediaId language,
72 const LimaString& complement):
73 ConstraintWithRelationComplement(language,complement)
74{}
75
78 const LinguisticGraphVertex& v1,
79 const LinguisticGraphVertex& v2,
80 AnalysisContent& analysis ) const
81{
82/*
83 Critical Function : comment logging messages
84*/
85 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.getData("SyntacticData"));
86
87 if (v1 == graph.firstVertex() || v1 == graph.lastVertex() ||
88 v2 == graph.firstVertex() || v2 == graph.lastVertex() )
89 {
90 // LDEBUG << "SecondUngovernedBy: false";
91 return false;
92 }
93#ifdef DEBUG_LP
95 LDEBUG << "StoreForDisambiguation " << v1 << ", " << v2 << ", " << m_relation;
96#endif
97 syntacticData->storeRelationForSelectionalConstraint(v1, v2, m_relation);
98 return true;
99}
100
102 const LimaString& complement):
103 ConstraintWithRelationComplement(language,complement),
104 m_language(language)
105{
106 // ConstraintFunction::addFunction("GovernorOf",this);
107 try {
108 auto res = LinguisticResources::changeable().getResource(language,"selectionalPreferences");
109 if (res==0)
110 {
111 SELOGINIT;
112 LWARN << "no resource 'selectionalPreferences'";
113 }
114 else {
115 m_preferences = std::dynamic_pointer_cast<SelectionalPreferences>(res);
116 }
117 }
118 catch (std::exception& e) {
119 // resources are not loaded when compiling the rules
120 SELOGINIT;
121 LWARN << "Exception caught: " << e.what();
122 }
123
124}
125
127 const LinguisticGraphVertex& v1,
128 const LinguisticGraphVertex& v2,
129 AnalysisContent& analysis) const
130{
131/*
132 Critical function : comment logging messages
133*/
134#ifdef DEBUG_LP
136 LDEBUG << "DisambiguateWith " << v1 << ", " << v2;
137#endif
138 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.getData("SyntacticData"));
139 SyntacticData::Relation oldRelation = syntacticData->relationStoredForSelectionalConstraint();
141
142
143 if (v1 == graph.firstVertex() || v1 == graph.lastVertex() ||
144 v2 == graph.firstVertex() || v2 == graph.lastVertex() )
145 {
146 // LDEBUG << "SecondUngovernedBy: false";
147 return false;
148 }
149 // collect all data necessary
150 // dummy implementation:
151 // get old target (noun), get old target COD target (verb), get current node
152 // get current det source of detsub if any
153 auto posgraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph"));
154 if (posgraph==0)
155 {
157 LERROR << "no graph 'PosGraph' available !";
158 return false;
159 }
160 LinguisticGraph* lingGraph = const_cast<LinguisticGraph*>(posgraph->getGraph());
161 // LDEBUG << "There is " << out_degree(v2, *lingGraph) << " edges out of " << v2;
162// VertexTokenPropertyMap tokenMap = get(vertex_token, *lingGraph);
163 VertexDataPropertyMap dataMap = get(vertex_data, *lingGraph);
164// Token* v1Token = tokenMap[v1];
165 MorphoSyntacticData* v1Data = dataMap[v1];
166// Token* v2Token = tokenMap[v2];
167 MorphoSyntacticData* v2Data = dataMap[v2];
168 std::string rel = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationName(m_relation);
169// Token* ov1Token = tokenMap[oldRelation.get<0>()];
170 MorphoSyntacticData* ov1Data = dataMap[std::get<0>(oldRelation)];
171// Token* ov2Token = tokenMap[oldRelation.get<1>()];
172 MorphoSyntacticData* ov2Data = dataMap[std::get<1>(oldRelation)];
173 std::string orel = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationName(std::get<2>(oldRelation));
174
175 // compute the preferred attachment
176 // (if no clear preference, choose the closest left attachment)
177 // dummy implementation
178 // if verb = manger and old target = food and det = "" and current is food
179 // then choose old relation
180 // else (at least if current is location) choose new one
181 double preference = 0;
182 const PropertyAccessor& macroAccessor = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyAccessor("MACRO");
183
184#ifdef DEBUG_LP
185 LDEBUG << "DisambiguateWith " << Common::Misc::limastring2utf8stdstring(sp[*(v2Data->allLemma().begin())])
186 << ", " << Common::Misc::limastring2utf8stdstring(sp[*(ov2Data->allLemma().begin())])
187 << ", " << Common::Misc::limastring2utf8stdstring(sp[*(v1Data->allLemma().begin())]);
188#endif
189 std::string oldRelationName = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationName(std::get<2>(oldRelation));
190 double oldProba = m_preferences->dependencyProbability(Common::Misc::limastring2utf8stdstring(sp[*(ov2Data->allLemma().begin())]),
191 *(ov2Data->allValues(macroAccessor).begin()),
192 oldRelationName,
193 Common::Misc::limastring2utf8stdstring(sp[*(ov1Data->allLemma().begin())]),
194 *(ov1Data->allValues(macroAccessor).begin()));
195 double newProba = m_preferences->dependencyProbability(Common::Misc::limastring2utf8stdstring(sp[*(v2Data->allLemma().begin())]),
196 *(v2Data->allValues(macroAccessor).begin()),
198 Common::Misc::limastring2utf8stdstring(sp[*(v1Data->allLemma().begin())]),
199 *(v1Data->allValues(macroAccessor).begin()));
200
201/* if ( !( (Common::Misc::limastring2utf8stdstring(sp[*(v2Data->allLemma().begin())]) == "manger")
202 && (Common::Misc::limastring2utf8stdstring(sp[*(ov2Data->allLemma().begin())]) == "bonbon")
203 && (Common::Misc::limastring2utf8stdstring(sp[*(v1Data->allLemma().begin())]) == "fraise")
204 ) )*/
205#ifdef DEBUG_LP
206 LDEBUG << "Old proba=" << oldProba << "; new proba=" << newProba;
207#endif
208 preference = newProba - oldProba;
209
210 // if old one: don't do anything
211 // else if new one, remove the old dependency, return true
212 if (preference > 0)
213 {
214 return syntacticData->removeDependency(std::get<0>(oldRelation),
215 std::get<1>(oldRelation),
216 std::get<2>(oldRelation));
217 }
218
219 return false;
220}
221
222
223
224} // end namespace
225} // end namespace
226} // end namespace
This file is the main header file for the data related to annotation graphs.
A graph that stores the relations of syntactic dependency between the elements of a DependencyGraph.
#define SAPLOGINIT
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
boost::property_map< LinguisticGraph, vertex_data_t >::type VertexDataPropertyMap
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
@ vertex_data
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define SELOGINIT
this file contains the definitions of several constraint functions for the disambiguation of dependen...
#define StoreForDisambiguationId
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
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.
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.
std::set< LinguisticCode > allValues(const Common::PropertyCode::PropertyAccessor &propertyAccessor) const
Abstract constraint function that takes a complement string that represents a relation name (entity t...
bool operator()(const LinguisticAnalysisStructure::AnalysisGraph &graph, const LinguisticGraphVertex &v1, const LinguisticGraphVertex &v2, AnalysisContent &analysis) const override
binary constraint function : the constraint only applies on two vertices in the graph (the graph is a...
DisambiguateWith(MediaId language, const LimaString &complement=LimaString())
bool operator()(const LinguisticAnalysisStructure::AnalysisGraph &graph, const LinguisticGraphVertex &v1, const LinguisticGraphVertex &v2, AnalysisContent &analysis) const override
binary constraint function : the constraint only applies on two vertices in the graph (the graph is a...
StoreForDisambiguation(MediaId language, const LimaString &complement=LimaString())
std::tuple< LinguisticGraphVertex, LinguisticGraphVertex, Common::MediaticData::SyntacticRelationId > Relation
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static LinguisticResources & changeable()
singleton accessor
Definition Singleton.h:71
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
Automaton::ConstraintFunctionFactory< DisambiguateWith > DisambiguateWithFactory(DisambiguateWithId)
Automaton::ConstraintFunctionFactory< StoreForDisambiguation > StoreForDisambiguationFactory(StoreForDisambiguationId)
NAUTITIA.
QString LimaString
Definition LimaString.h:33