LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SyntagmaticMatrix.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
16#include "SyntagmaticMatrix.h"
20
21#include <string>
22#include <algorithm>
23
24using namespace Lima::Common::Misc;
25using namespace Lima::Common::MediaticData;
27
28namespace Lima
29{
30namespace LinguisticProcessing
31{
32namespace SyntacticAnalysis
33{
34
36
37
39 m_language(),
40 m_nominalMatrix(),
41 m_verbalMatrix(),
42 m_nomChainBeg(),
43 m_nomChainEnd(),
44 m_verbChainBeg(),
45 m_verbChainEnd()
46{
47}
48
50
51
54 Manager* manager)
55
56{
58 LDEBUG << "Creating a SyntagmDefStruct (loads file)";
59 m_language=manager->getInitializationParameters().language;
60 m_macroAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyAccessor("MACRO"));
61 m_microAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyAccessor("MICRO"));
62 m_nominalMatrix.language(m_language);
63 m_verbalMatrix.language(m_language);
64
65 try {
66 QString matricesFileName = findFileInPaths(Common::MediaticData::MediaticData::single().getResourcesPath().c_str(),unitConfiguration.getParamsValueAtKey("file").c_str());
67 loadFromFile(matricesFileName.toUtf8().constData());
68
70 {
71 LERROR << "no parameter 'file' in SyntagmDefinitionStructure group for language " << (int) m_language << " !";
73 }
74
75// display();
76}
77
78
80{
81 m_nominalMatrix.deleteMatrix();
82 m_verbalMatrix.deleteMatrix();
83}
84
85
86void SyntagmDefStruct::loadFromFile(const std::string& fileName)
87{
89 LDEBUG << "Loading matrices from " << fileName;
90 //
91 // Create a SAX parser object. Then, according to what we were told on
92 // the command line, set it to validate or not.
93 //
94 //
95 // Create the handler object and install it as the document and error
96 // handler for the parser-> Then parse the file and catch any exceptions
97 // that propogate out
98 //
99 XmlSyntagmaticMatrixFileReader handler(*this,m_language);
100 QFile file(fileName.c_str());
101 if (!file.open(QFile::ReadOnly))
102 {
103 LIMA_EXCEPTION_SELECT_LOGINIT(XMLCFGLOGINIT, "Error opening " << fileName.c_str(), XMLException);
104 }
105 if (!handler.parse(&file))
106 {
107 LIMA_EXCEPTION_SELECT("SyntagmDefStruct::loadFromFile Unable to parse " << fileName.c_str() << " : "
108 << handler.errorString(),
110 }
111}
112
114{
115 SyntagmaticMatrixFilter::iterator it, it_end;
116
117 it = m_filters.begin(); it_end = m_filters.end();
118 for (; it != it_end; it++)
119 {
120 (*it).second.deleteRow();
121 }
122}
123
125{
126 std::cout << "Displaying a matrix" << std::endl;
127 for (SyntagmaticMatrixFilter::const_iterator it(m_filters.begin()); it != m_filters.end(); it++)
128 {
129 std::cout << ((*it).first) << " : ";
130 for (std::set< TokenFilter, tfless >::const_iterator it2((*it).second.m_filters.begin()); it2 != (*it).second.m_filters.end(); it2++)
131 {
132 std::cout << (*it2) << ", ";
133 }
134 std::cout << std::endl << std::endl;
135 }
136}
137
139{
140 return m_filters;
141}
142
144{
145 return m_filters;
146}
147
148SyntagmaticMatrixFilter::const_iterator SyntagmaticMatrix::find(const TokenFilter& f) const
149{
150 SyntagmaticMatrixFilter::const_iterator it = m_filters.begin();
151 SyntagmaticMatrixFilter::const_iterator it_end = m_filters.end();
152 for (; it != it_end; it++)
153 {
154 if ( tf_dwless()( (*it).first, f))
155 return it;
156 }
157 return it_end;
158}
159
161{
162 m_nominalMatrix.display();
163 std::cout << std::endl << std::endl;
164 m_verbalMatrix.display();
165 std::cout << std::endl << std::endl;
166 std::cout << m_nomChainBeg << std::endl << std::endl;
167 std::cout << m_nomChainEnd << std::endl << std::endl;
168 std::cout << m_verbChainBeg << std::endl << std::endl;
169 std::cout << m_verbChainEnd << std::endl << std::endl;
170}
171
173{
174// SALOGINIT;
175// LDEBUG << "SyntagmDefStruct::canChainBeginBy(" << filter << "," << type << ")";
176 if (type == NOMINAL)
177 return canNominalChainBeginBy(filter);
178 else if (type == VERBAL)
179 return canVerbalChainBeginBy(filter);
180 else return false;
181}
182
184{
185 if (type == NOMINAL)
186 return canNominalChainEndBy(filter);
187 else if (type == VERBAL)
188 return canVerbalChainEndBy(filter);
189 else return false;
190}
191
193{
194 if (type == NOMINAL)
195 return belongsToNominalMatrix(src, dest);
196 else if (type == VERBAL)
197 return belongsToVerbalMatrix(src, dest);
198 else return false;
199}
200
202{
203 TokenFilter f(filter,m_microAccessor);
204 return (m_nomChainBeg.find(f) != m_nomChainBeg.m_filters.end());
205}
206
208{
209 TokenFilter f(filter,m_microAccessor);
210 return (m_nomChainEnd.find(f) != m_nomChainEnd.m_filters.end());
211}
212
214{
215 TokenFilter f(filter,m_microAccessor);
216 return (m_verbChainBeg.find(f) != m_verbChainBeg.m_filters.end());
217}
218
220{
221 TokenFilter f(filter,m_microAccessor);
222 return (m_verbChainEnd.find(f) != m_verbChainEnd.m_filters.end());
223}
224/*
225bool SyntagmDefStruct::belongsToNominalMatrix(const MorphoSyntacticData* src, const MorphoSyntacticData* dest) const
226{
227 SALOGINIT;
228 SyntagmaticMatrixFilter::const_iterator it, it_end;
229 it = m_nominalMatrix.begin();
230 it_end = m_nominalMatrix.end();
231 for (; it != it_end; it++)
232 {
233 if ( (*it).first == src )
234 {
235 LDEBUG << "Found a row for " << src << " : " << ((*it).first);
236 const SyntagmaticMatrixRow& row = (*it).second;
237 SyntagmaticMatrixRow::iterator itRow = row.find(dest);
238 LDEBUG << "Searching " << dest << " in " << row;
239 if (itRow != row.end())
240 {
241 LDEBUG << "Cell OK found: " << (*itRow);
242 return true;
243 }
244 }
245 }
246 LDEBUG << "No row found";
247 return false;
248}
249*/
251{
252 TokenFilter srcTf(src,m_microAccessor);
253 TokenFilter destTf(dest,m_microAccessor);
254 auto ends = std::make_pair(m_nominalMatrix.filters().begin(),
255 m_nominalMatrix.filters().end());
256 for (; ends.first != ends.second; (ends.first)++)
257 {
258 if ( tf_dwless()(ends.first->first,srcTf) )
259 {
260 const SyntagmaticMatrixRow& row = (*(ends.first)).second;
261 std::set< TokenFilter, tfless >::const_iterator itRow = row.find(destTf);
262 if (itRow != row.m_filters.end()) return true;
263 }
264 }
265 return false;
266}
267
269{
270/*
271 SyntagmaticMatrixFilter::const_iterator it = m_verbalMatrix.find(src);
272 if ( it == m_verbalMatrix.end() ) return false;
273 const SyntagmaticMatrixRow& row = (*it).second;
274 SyntagmaticMatrixRow::iterator itRow = row.find(dest);
275 return ( itRow != row.end() );
276*/
277 TokenFilter srcTf(src,m_microAccessor);
278 TokenFilter destTf(dest,m_microAccessor);
279 auto ends = std::make_pair(m_verbalMatrix.filters().begin(),
280 m_verbalMatrix.filters().end());
281// @todo this equal_range does not work. See why.
282// std::equal_range(m_verbalMatrix.begin(),m_verbalMatrix.end(),
283// TokenFilter(src), tf_dwless());
284 for (; ends.first != ends.second; (ends.first)++)
285 {
286 if ( tf_dwless()(ends.first->first , srcTf) )
287 {
288 const SyntagmaticMatrixRow& row = (*(ends.first)).second;
289 std::set< TokenFilter, tfless >::const_iterator itRow = row.find(destTf);
290 if (itRow != row.m_filters.end()) return true;
291 }
292 }
293 return false;
294}
295
298bool tf_dwless::operator()(const TokenFilter& x, const TokenFilter& y) const
299{
300// std::cout << "In tf_dwless(" << x << ", " << y << ") : ";
301 bool attribFound = false;
302 if (x.attributes().size() > 0)
303 {
304 std::set< StringsPoolIndex >::const_iterator itx, ity, itx_end, ity_end;
305 boost::tie(itx, itx_end) = x.attributesIterators();
306 boost::tie(ity, ity_end) = y.attributesIterators();
307/* for (;ity!=ity_end;ity++)
308 {
309 if (x.attributes().find( *ity ) != itx_end)
310 {
311 attribFound = true;
312 break;
313 }
314 }*/
315 while ((itx != itx_end) && (ity != ity_end))
316 {
317 if (*itx == *ity) {
318 attribFound = true;
319 break;
320 }
321 if (*itx < *ity) {
322 itx++;
323 } else {
324 ity++;
325 }
326 }
327 } else {
328 attribFound = true;
329 }
330 if (!attribFound) {
331 return false;
332 }
333 bool categFound = false;
334 std::set< LinguisticCode >::const_iterator itx, ity, itx_end, ity_end;
335 boost::tie(itx, itx_end) = x.categoriesIterators();
336 boost::tie(ity, ity_end) = y.categoriesIterators();
337/* for (;ity!=ity_end;ity++) {
338 if (x.categories().find( *ity ) != itx_end) {
339 categFound = true;
340 break;
341 }
342 }*/
343 while ((itx != itx_end) && (ity != ity_end))
344 {
345 if (*itx == *ity) {
346 categFound = true;
347 break;
348 }
349 if (*itx < *ity) {
350 itx++;
351 } else {
352 ity++;
353 }
354 }
355// std::cout << std::boolalpha << ( categFound && attribFound ) << std::endl;
356 return ( categFound );
357}
358
361bool tf_dwless::operator()(const TokenFilter& x, const std::pair<TokenFilter,SyntagmaticMatrixRow>& p) const
362{
363 bool attribFound = false;
364 const TokenFilter& y = p.first;
365// std::cout << "In tf_dwless(" << x << ", " << y << ") : " ;
366 if (x.attributes().size() > 0)
367 {
368 std::set< StringsPoolIndex >::const_iterator itx, ity, itx_end, ity_end;
369 boost::tie(itx, itx_end) = x.attributesIterators();
370 boost::tie(ity, ity_end) = y.attributesIterators();
371/* for (;ity!=ity_end;ity++)
372 {
373 if (x.attributes().find( *ity ) != itx_end)
374 {
375 attribFound = true;
376 break;
377 }
378 }*/
379 while ((itx != itx_end) && (ity != ity_end))
380 {
381 if (*itx == *ity) {
382 attribFound = true;
383 break;
384 }
385 if (*itx < *ity) {
386 itx++;
387 } else {
388 ity++;
389 }
390 }
391 } else {
392 attribFound = true;
393 }
394 if (!attribFound) {
395 return false;
396 }
397 bool categFound = false;
398 std::set< LinguisticCode >::const_iterator itx, ity, itx_end, ity_end;
399 boost::tie(itx, itx_end) = x.categoriesIterators();
400 boost::tie(ity, ity_end) = y.categoriesIterators();
401/* for (;ity!=ity_end;ity++) {
402 if (x.categories().find( *ity ) != itx_end) {
403 categFound = true;
404 break;
405 }
406 }*/
407 while ((itx != itx_end) && (ity != ity_end))
408 {
409 if (*itx == *ity) {
410 categFound = true;
411 break;
412 }
413 if (*itx < *ity) {
414 itx++;
415 } else {
416 ity++;
417 }
418 }
419// std::cout << std::boolalpha << ( categFound && attribFound ) << std::endl;
420 return ( categFound );
421}
422
425bool tf_dwless::operator()(const std::pair<TokenFilter,SyntagmaticMatrixRow>& p, const TokenFilter& y) const
426{
427 bool attribFound = false;
428 const TokenFilter& x = p.first;
429 if (x.attributes().size() > 0)
430 {
431 std::set< StringsPoolIndex >::const_iterator itx, ity, itx_end, ity_end;
432 boost::tie(itx, itx_end) = x.attributesIterators();
433 boost::tie(ity, ity_end) = y.attributesIterators();
434/* for (;ity!=ity_end;ity++)
435 {
436 if (x.attributes().find( *ity ) != itx_end)
437 {
438 attribFound = true;
439 break;
440 }
441 }*/
442 while ((itx != itx_end) && (ity != ity_end))
443 {
444 if (*itx == *ity) {
445 attribFound = true;
446 break;
447 }
448 if (*itx < *ity) {
449 itx++;
450 } else {
451 ity++;
452 }
453 }
454 } else {
455 attribFound = true;
456 }
457 if (!attribFound) {
458 return false;
459 }
460 bool categFound = false;
461 std::set< LinguisticCode >::const_iterator itx, ity, itx_end, ity_end;
462 boost::tie(itx, itx_end) = x.categoriesIterators();
463 boost::tie(ity, ity_end) = y.categoriesIterators();
464/* for (;ity!=ity_end;ity++) {
465 if (x.categories().find( *ity ) != itx_end) {
466 categFound = true;
467 break;
468 }
469 }*/
470 while ((itx != itx_end) && (ity != ity_end))
471 {
472 if (*itx == *ity) {
473 categFound = true;
474 break;
475 }
476 if (*itx < *ity) {
477 itx++;
478 } else {
479 ity++;
480 }
481 }
482// std::cout << std::boolalpha << ( categFound && attribFound ) << std::endl;
483 return ( categFound );
484}
485
486std::set< TokenFilter, tfless >::const_iterator SyntagmaticMatrixRow::find(const TokenFilter& f) const
487{
488 std::set< TokenFilter, tfless >::const_iterator it = m_filters.begin();
489 std::set< TokenFilter, tfless >::const_iterator it_end = m_filters.end();
490 for (; it != it_end; it++)
491 {
492 if ( tf_dwless()(*it, f))
493 return it;
494 }
495 return it_end;
496}
497
498} // namespace SyntacticAnalysis
499} // namespace LinguisticProcessing
500} // namespace Lima
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
Definition LimaCommon.h:332
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define LIMA_EXCEPTION_SELECT(X, Y)
This macro writes the message X to a previously configured error stream before throwing a Y exception...
Definition LimaCommon.h:306
#define SALOGINIT
Defines a Factory to create Object of type Base.
#define SYNTAGMDEFSTRUCT_CLASSID
Holds linguistic data for one language.
const MediaData & mediaData(MediaId media) const
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
bool belongsToNominalMatrix(const LinguisticAnalysisStructure::MorphoSyntacticData *src, const LinguisticAnalysisStructure::MorphoSyntacticData *dest) const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
bool canNominalChainEndBy(const LinguisticAnalysisStructure::MorphoSyntacticData *cat) const
bool canVerbalChainEndBy(const LinguisticAnalysisStructure::MorphoSyntacticData *cat) const
bool canChainEndBy(const LinguisticAnalysisStructure::MorphoSyntacticData *filter, Common::MediaticData::ChainsType type) const
bool canVerbalChainBeginBy(const LinguisticAnalysisStructure::MorphoSyntacticData *cat) const
bool canChainBeginBy(const LinguisticAnalysisStructure::MorphoSyntacticData *filter, Common::MediaticData::ChainsType type) const
bool belongsToMatrix(const LinguisticAnalysisStructure::MorphoSyntacticData *src, const LinguisticAnalysisStructure::MorphoSyntacticData *dest, Common::MediaticData::ChainsType type) const
bool belongsToVerbalMatrix(const LinguisticAnalysisStructure::MorphoSyntacticData *src, const LinguisticAnalysisStructure::MorphoSyntacticData *dest) const
bool canNominalChainBeginBy(const LinguisticAnalysisStructure::MorphoSyntacticData *cat) const
std::set< TokenFilter, tfless >::const_iterator find(const TokenFilter &f) const
SyntagmaticMatrixFilter::const_iterator find(const TokenFilter &f) const
std::pair< std::set< StringsPoolIndex >::iterator, std::set< StringsPoolIndex >::iterator > attributesIterators()
std::pair< std::set< LinguisticCode >::iterator, std::set< LinguisticCode >::iterator > categoriesIterators()
bool operator()(const TokenFilter &x, const TokenFilter &y) const
Used to search a DicoWord inside syntagmatic structure.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
ChainsType
syntactic chains type
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
std::map< TokenFilter, SyntagmaticMatrixRow, tfless > SyntagmaticMatrixFilter
SimpleFactory< AbstractResource, SyntagmDefStruct > syntagmDefStructFactory(SYNTAGMDEFSTRUCT_CLASSID)
NAUTITIA.