39#include <boost/graph/properties.hpp>
70 bool operator()(
const std::pair<boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::BoWToken > >
t1,
const std::pair<boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::BoWToken > >
t2)
const
72 return (
t1.second->getPosition()<
t2.second->getPosition()) ;
81 BowGeneratorPrivate();
83 ~BowGeneratorPrivate() =
default;
84 BowGeneratorPrivate(
const BowGeneratorPrivate&) =
delete;
85 BowGeneratorPrivate& operator=(
const BowGeneratorPrivate&) =
delete;
87 std::vector< std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >,
88 boost::shared_ptr< Common::BagOfWords::AbstractBoWElement > > >
89 createAbstractBoWElement(
93 const uint64_t offsetBegin,
95 std::set<LinguisticGraphVertex>& visited,
96 bool keepAnyway =
false)
const;
98 boost::shared_ptr< Common::BagOfWords::BoWRelation > createBoWRelationFor(
105 class NamedEntityPart
108 NamedEntityPart(): inflectedForm(), lemma(), position(0),
113 inflectedForm(fl), lemma(l), category(cat), position (pos),
123 typedef std::set< std::pair<uint64_t,uint64_t> > TokenPositions;
126 std::shared_ptr<AnalysisDumpers::StopList> m_stopList;
128 bool m_useEmptyMacro;
129 bool m_useEmptyMicro;
132 bool m_keepAllNamedEntityParts;
138 NORMALIZE_NE_INFLECTED,
140 NORMALIZE_NE_NORMALIZEDFORM,
143 NENormalization m_NEnormalization;
145 boost::shared_ptr< Common::BagOfWords::BoWNamedEntity > createSpecificEntity(
151 const uint64_t offset,
152 bool frompos =
true)
const;
154 boost::shared_ptr< Common::BagOfWords::BoWToken > createCompoundTense(
159 const uint64_t offset,
160 std::set<LinguisticGraphVertex>& visited)
const;
167 QList< boost::shared_ptr< Common::BagOfWords::BoWPredicate > > createPredicate(
173 const uint64_t offset,
174 std::set<LinguisticGraphVertex>& visited,
175 bool keepAnyway)
const;
177 bool checkStopWordInCompound(
178 boost::shared_ptr< Common::BagOfWords::BoWToken>&,
180 std::set< std::string >& alreadyStored,
183 StringsPoolIndex getNamedEntityNormalization(
199 std::vector<NamedEntityPart> createNEParts(
204 bool frompos =
true)
const;
206 void bowTokenPositions(TokenPositions& res,
207 const boost::shared_ptr< Common::BagOfWords::BoWToken > tok)
const;
209 uint64_t computeCompoundLength(
const TokenPositions& headTokPositions,
210 const TokenPositions& extensionPositions)
const;
213BowGeneratorPrivate::BowGeneratorPrivate():
217 m_useEmptyMacro(true),
218 m_useEmptyMicro(true),
219 m_keepAllNamedEntityParts(false),
222 m_NEnormalization(NORMALIZE_NE_INFLECTED)
241 m_d->m_language=language;
247 m_d->m_useStopList = (use==
"true");
251 LWARN <<
"No param 'useStopList' in"<<unitConfiguration.
getName()
252 <<
"configuration group for language " << (int)m_d->m_language;
253 LWARN <<
"use default value : true";
255 if (m_d->m_useStopList)
262 LDEBUG <<
"BowGenerator.init(): STOPLIST:";
263 for(
const auto& word: *m_d->m_stopList)
265 LDEBUG <<
"BowGenerator.init(): " << word;
271 LWARN <<
"No param 'stopList' in" << unitConfiguration.
getName()
272 <<
"configuration group for language "
273 << (int)m_d->m_language;
280 m_d->m_useEmptyMacro = (use==
"true");
284 LWARN <<
"No param 'useEmptyMacro' in" << unitConfiguration.
getName()
285 <<
"configuration group for language " << (int)m_d->m_language;
286 LWARN <<
"use default value : true";
291 m_d->m_useEmptyMicro = (use==
"true");
295 LWARN <<
"No param 'useEmptyMicro' in" << unitConfiguration.
getName()
296 <<
"configuration group for language " << (int)m_d->m_language;
297 LWARN <<
"use default value : true";
306 LERROR <<
"No param 'properNounCategory' in" << unitConfiguration.
getName()
307 <<
"configuration group for language " << (int)m_d->m_language;
317 LERROR <<
"No param 'commonNounCategory' in" << unitConfiguration.
getName()
318 <<
"configuration group for language " << (int)m_d->m_language;
325 m_d->m_keepAllNamedEntityParts = (value ==
"yes" || value ==
"true" || value ==
"1");
332 if (value ==
"useInflectedForm")
334 m_d->m_NEnormalization = BowGeneratorPrivate::NORMALIZE_NE_INFLECTED;
336 else if (value ==
"useLemma")
338 m_d->m_NEnormalization = BowGeneratorPrivate::NORMALIZE_NE_LEMMA;
340 else if (value ==
"useNENormalizedForm")
342 m_d->m_NEnormalization = BowGeneratorPrivate::NORMALIZE_NE_NORMALIZEDFORM;
344 else if (value ==
"useNEType")
346 m_d->m_NEnormalization = BowGeneratorPrivate::NORMALIZE_NE_NETYPE;
358 const uint64_t offset,
361 std::set< LinguisticGraphVertex >& visited)
const
366 LDEBUG <<
"BowGenerator::buildTermFor annot:" << vx <<
"; pointing on annot:"<<tgt;
370 *(annotationData->
matches(
"cpd", vx,
"PosGraph").begin());
373 std::vector<AnnotationGraphVertex> vxGovernors;
375 boost::tie(inIt, inIt_end) = boost::in_edges(vx, annotationData->
getGraph());
376 for (; inIt != inIt_end; inIt++)
378 vxGovernors.push_back(source(*inIt, annotationData->
getGraph()));
382 offset, annotationData, visited);
387 if (vxGovernors.empty())
390 std::vector< std::pair<boost::shared_ptr< BoWRelation >,
391 boost::shared_ptr< BoWToken > > > vxBoWTk;
395 auto relation = m_d->createBoWRelationFor(vx, tgt, annotationData,
396 posgraph, syntacticData);
400 for (
auto& vxBoWToken: vxBoWTokens)
402 vxBoWToken.first = relation;
405 for (
auto& vxBoWToken: vxBoWTokens)
407 if (boost::dynamic_pointer_cast<BoWToken>(vxBoWToken.second) != 0)
409 vxBoWTk.push_back(std::make_pair(vxBoWToken.first,
410 boost::dynamic_pointer_cast<BoWToken>(vxBoWToken.second)));
415 LERROR <<
"BowGenerator::buildTermFor vxBoWTokensIt is not a BoWToken";
425 std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > > result;
426 std::vector< std::vector< std::pair< boost::shared_ptr< BoWRelation > , boost::shared_ptr< BoWToken > > > > termsForVxGovernors;
427 for (
const auto& gov: vxGovernors)
429 auto pairs =
buildTermFor(gov, vx, anagraph, posgraph, offset,
430 syntacticData, annotationData, visited);
431 termsForVxGovernors.push_back(pairs);
439 std::vector< std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > >::iterator > begins;
440 std::vector< std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > >::iterator > ends;
441 std::vector< std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > >::iterator > stack;
442 for (
auto termsForVxGovernorsIt = termsForVxGovernors.begin();
443 termsForVxGovernorsIt != termsForVxGovernors.end();
444 termsForVxGovernorsIt++)
446 if (!(*termsForVxGovernorsIt).empty())
448 begins.push_back((*termsForVxGovernorsIt).begin());
449 ends.push_back((*termsForVxGovernorsIt).end());
450 stack.push_back((*termsForVxGovernorsIt).begin());
460 auto relation = m_d->createBoWRelationFor(vx, tgt, annotationData,
461 posgraph, syntacticData);
464 for (
auto& vxBoWToken: vxBoWTokens)
466 vxBoWToken.first = relation;
469 std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > > vxBoWTk;
470 auto vxBoWTokensIt = vxBoWTokens.begin(), vxBoWTokensIt_end = vxBoWTokens.end();
471 for (; vxBoWTokensIt != vxBoWTokensIt_end; vxBoWTokensIt++)
473 if (boost::dynamic_pointer_cast<BoWToken>((*vxBoWTokensIt).second) != 0)
475 vxBoWTk.push_back(std::make_pair((*vxBoWTokensIt).first,boost::dynamic_pointer_cast<BoWToken>((*vxBoWTokensIt).second)));
480 LERROR <<
"BowGenerator::buildTermFor vxBoWTokensIt is not a BoWToken";
488 std::vector< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > >::iterator t;
489 while (!stack.empty())
494 for (
auto vxBoWToken=vxBoWTokens.begin();
495 vxBoWToken!=vxBoWTokens.end();
498 boost::shared_ptr< BoWToken > head = boost::dynamic_pointer_cast<BoWToken>((*vxBoWToken).second);
502 LERROR <<
"BowGenerator::buildTermFor head" << &*(*vxBoWToken).second <<
"is not a BoWToken";
509 std::set< std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > >,
A > extensions;
510 auto govsIt = stack.begin(), govsIt_end = stack.end();
511 for (; govsIt != govsIt_end; govsIt++)
516 boost::shared_ptr< BoWRelation > relation( (**govsIt).first);
517 boost::shared_ptr< BoWToken > bt = (**govsIt).second;
522 extensions.insert(std::make_pair(relation,bt));
530 BowGeneratorPrivate::TokenPositions headPositions;
531 m_d->bowTokenPositions(headPositions, head);
532 BowGeneratorPrivate::TokenPositions extensionPositions;
537 auto extensionsIt = extensions.begin(),
538 extensionsIt_end = extensions.end();
539 for (; extensionsIt != extensionsIt_end; extensionsIt++)
541 boost::shared_ptr< BoWToken > extension = (*extensionsIt).second;
546 m_d->bowTokenPositions(extensionPositions, extension);
553 position=headPositions.begin()->first;
554 if (position > extensionPositions.begin()->first)
556 position = extensionPositions.begin()->first;
563 length=m_d->computeCompoundLength(headPositions,extensionPositions);
568 boost::shared_ptr< BoWTerm > complex(
new BoWTerm( lemma, head->getCategory(), position, length) );
569 complex->setVertex(head->getVertex());
570 complex->setInflectedForm(infl);
571 complex->setCategory(head->getCategory());
572 complex->addPart(head);
575 extensionsIt = extensions.begin();
576 extensionsIt_end = extensions.end();
577 for (; extensionsIt != extensionsIt_end; extensionsIt++)
579 boost::shared_ptr< BoWToken > extension = (*extensionsIt).second;
583 if ((*extensionsIt).first == 0)
584 complex->addPart(extension);
586 complex->addPart((*extensionsIt).first,extension);
593 auto relation = m_d->createBoWRelationFor(vx, tgt, annotationData,
594 posgraph, syntacticData);
599 result.push_back(std::make_pair(relation,complex));
609 while ( t == ends[stack.size()-1] )
626 for (uint64_t i = stack.size(); i < begins.size();i++)
628 stack.push_back(begins[i]);
641boost::shared_ptr< BoWRelation > BowGeneratorPrivate::createBoWRelationFor(
652 LDEBUG <<
"BowGenerator::createBoWRelationFor" << vx << tgt;
654 boost::shared_ptr<BoWRelation> relation;
660 LDEBUG <<
"BowGenerator: working on relation";
663 if (annot != 0 && !annot->empty())
669 relation = boost::shared_ptr< BoWRelation >(
new BoWRelation(realization, type));
673 relation = boost::shared_ptr< BoWRelation >(
new BoWRelation());
678 LDEBUG <<
"BowGenerator: working vx " << vxTokVertex;
679 LDEBUG <<
"BowGenerator: working tgt " << tgtTokVertex;
682 if (out_degree(depV, *depGraph) > 0){
684 boost::tie(depIt, depIt_end) = out_edges(depV, *depGraph);
685 for (; depIt != depIt_end; depIt++)
689 if (targV == tgtTokVertex){
691 relation->setSynType(relTypeMap[*depIt]);
699 LDEBUG <<
"BowGenerator: relation : " << *relation;
706std::vector< std::pair< boost::shared_ptr< BoWRelation >,
707 boost::shared_ptr< AbstractBoWElement > > >
712 const uint64_t offsetBegin,
714 std::set<LinguisticGraphVertex>& visited,
715 bool keepAnyway)
const
717 return m_d->createAbstractBoWElement(v, anagraph, posgraph, offsetBegin,
718 annotationData, visited, keepAnyway);
721std::vector< std::pair< boost::shared_ptr< BoWRelation >,
722 boost::shared_ptr< AbstractBoWElement > > >
723 BowGeneratorPrivate::createAbstractBoWElement(
727 const uint64_t offsetBegin,
729 std::set<LinguisticGraphVertex>& visited,
730 bool keepAnyway)
const
734 LDEBUG <<
"BowGenerator::createAbstractBoWElement for " << v;
736 std::vector<std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > abstractBowEl;
740 std::set< AnnotationGraphVertex > anaVertices = annotationData->
matches(
"PosGraph",v,
"AnalysisGraph");
742 LDEBUG <<
"BowGenerator::createAbstractBoWElement " << v <<
" has " << anaVertices.size() <<
" matching vertices in analysis graph";
745 bool createdSpecificEntity(
false);
748 for (
auto anaVertex = anaVertices.begin(); anaVertex != anaVertices.end(); ++anaVertex)
751 LDEBUG <<
"BowGenerator::createAbstractBoWElement Looking at analysis graph vertex " << *anaVertex;
753 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
"AnalysisGraph",*anaVertex,
"annot");
754 for (
auto matchVertex = matches.begin(); matchVertex != matches.end(); ++matchVertex)
757 LDEBUG <<
"BowGenerator::createAbstractBoWElement Looking at annotation graph vertex " << *matchVertex;
763 boost::shared_ptr< BoWToken > se = createSpecificEntity(matchv,*matchVertex, annotationData, anagraph, posgraph, offsetBegin,
false);
768 LDEBUG <<
"BowGenerator::createAbstractBoWElement created specific entity: " << QString::fromUtf8(se->getOutputUTF8String().c_str());
771 abstractBowEl.push_back(std::make_pair(boost::shared_ptr< BoWRelation >(),se));
773 createdSpecificEntity=
true;
780 LDEBUG <<
"BowGenerator::createAbstractBoWElement move on the PosGraph annot matching test";
785 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
"PosGraph",v,
"annot");
787 LDEBUG <<
"BowGenerator::createAbstractBoWElement there are " << matches.size() <<
" annotation graph vertices matching the current PsGraph vertex " << v;
789 for (
auto it = matches.begin(); it != matches.end(); ++it)
793 LDEBUG <<
"BowGenerator::createAbstractBoWElement Looking at annotation graph vertex " << vx;
797 boost::shared_ptr< BoWToken > se = createSpecificEntity(v,vx, annotationData, anagraph, posgraph, offsetBegin);
801 LDEBUG <<
"BowGenerator::createAbstractBoWElement created specific entity: " << QString::fromUtf8(se->getOutputUTF8String().c_str());
804 abstractBowEl.push_back(std::make_pair(boost::shared_ptr< BoWRelation >(),se));
806 return abstractBowEl;
811 boost::shared_ptr< BoWToken > ct = createCompoundTense(vx, annotationData, anagraph, posgraph, offsetBegin, visited);
815 LDEBUG <<
"BowGenerator::createAbstractBoWElement created compound tense: " << *ct;
818 abstractBowEl.push_back(std::make_pair(boost::shared_ptr< BoWRelation >(),ct));
820 return abstractBowEl;
827 LDEBUG <<
"BowGenerator::createAbstractBoWElement Found a predicate in the PosGraph annnotation graph matching";
830 MorphoSyntacticData* data = get(
vertex_data, posgraph, v);
834 for (
auto elem = data->begin(); elem != data->end(); ++elem)
836 if (!keepAnyway && !shouldBeKept(*elem))
845 auto pred = createPredicate(v, vx, annotationData, anagraph, posgraph,
846 offsetBegin, visited, keepAnyway);
847 for (
auto bP = pred.begin(); bP != pred.end(); ++bP)
852 LDEBUG <<
"BowGenerator::createAbstractBoWElement created a predicate" ;
854 abstractBowEl.push_back(std::make_pair(boost::shared_ptr< BoWRelation >(),*bP));
864 LDEBUG <<
"BowGenerator::createAbstractBoWElement No SpecificEntity nor CpdTense nor Predicate found";
871 if (!abstractBowEl.empty())
878 MorphoSyntacticData* data = get(
vertex_data, posgraph, v);
881 std::set<std::pair<StringsPoolIndex,LinguisticCode> > alreadyCreated;
882 std::pair<StringsPoolIndex,LinguisticCode> predNormCode = std::make_pair(StringsPoolIndex(0),
L_NONE);
884 if (createdSpecificEntity) {
888 return abstractBowEl;
893 for (
auto it=data->begin(); it!=data->end(); it++)
895 auto normCode=std::make_pair(it->normalizedForm, m_microAccessor->readValue(it->properties));
896 if (normCode != predNormCode)
898 if (alreadyCreated.find(normCode)==alreadyCreated.end())
900 if (keepAnyway || shouldBeKept(*it))
902 boost::shared_ptr< BoWToken > newbowtok(
new BoWToken(sp[it->normalizedForm],
903 m_macroAccessor->readValue(it->properties),
906 newbowtok->setVertex(v);
907 newbowtok->setInflectedForm(token->
stringForm());
909 LDEBUG <<
"BowGenerator::createAbstractBoWElement created bow token: " << *newbowtok;
911 abstractBowEl.push_back(std::make_pair(boost::shared_ptr< BoWRelation >(),newbowtok));
913 alreadyCreated.insert(normCode);
915 predNormCode=normCode;
923 return abstractBowEl;
928bool BowGeneratorPrivate::shouldBeKept(
const LinguisticAnalysisStructure::LinguisticElement& elem)
const
943 LDEBUG <<
"BowGenerator::shouldBeKept token ("
944 << sp[elem.
lemma] <<
"|"
945 << elem.
properties <<
") not kept : macro category is empty ";
954 LDEBUG <<
"BowGenerator::shouldBeKept token ("
955 << sp[elem.
lemma] <<
"|"
956 << elem.
properties <<
") not kept : micro category is empty ";
964 if (m_useStopList && m_stopList!=0 && (m_stopList->find(sp[elem.
normalizedForm]) != m_stopList->end()))
967 LDEBUG <<
"BowGenerator::shouldBeKept token ("
968 << sp[elem.
lemma] <<
"|"
969 << elem.
properties <<
") not kept : normalization "
971 <<
" is in stoplist";
977 LDEBUG <<
"BowGenerator::shouldBeKept token ("
978 << sp[elem.
lemma] <<
"|"
986bool BowGeneratorPrivate::checkStopWordInCompound(
987 boost::shared_ptr< Common::BagOfWords::BoWToken >& tok,
989 std::set< std::string >& alreadyStored,
990 Common::BagOfWords::BoWText& bowText)
const
1116void BowGeneratorPrivate::bowTokenPositions(
1117 TokenPositions& res,
1118 const boost::shared_ptr< Common::BagOfWords::BoWToken > tok)
const
1121 res.insert(poslenlist.begin(),poslenlist.end());
1124uint64_t BowGeneratorPrivate::computeCompoundLength(
1125 const TokenPositions& headTokPositions,
1126 const TokenPositions& extensionPositions)
const
1144 uint64_t positionBegin=headTokPositions.begin()->first;
1145 if (positionBegin>extensionPositions.begin()->first)
1147 positionBegin=extensionPositions.begin()->first;
1149 uint64_t positionEnd=headTokPositions.rbegin()->first+
1150 headTokPositions.rbegin()->second;
1151 uint64_t positionEndExt=extensionPositions.rbegin()->first+
1152 extensionPositions.rbegin()->second;
1153 if (positionEndExt>positionEnd)
1155 positionEnd=positionEndExt;
1157 length=positionEnd-positionBegin;
1181 const uint64_t offset,
1184 return m_d->createSpecificEntity(vertex,v,annotationData,anagraph,posgraph,offset,frompos);
1187boost::shared_ptr< BoWNamedEntity > BowGeneratorPrivate::createSpecificEntity(
1193 const uint64_t offset,
1198 return boost::shared_ptr< BoWNamedEntity >();
1202 LINFO <<
"BowGenerator: createSpecificEntity ling:" << vertex
1203 <<
"; annot:" << v << offset << frompos;
1210 pointerValue<SpecificEntityAnnotation>();
1213 LINFO <<
"BowGenerator: specific entity type is " << se->
getType();
1216 std::set< std::string > alreadyStored;
1223 catch (std::exception& e) {
1226 return boost::shared_ptr< BoWNamedEntity >();
1229 LINFO <<
"BowGenerator: specific entity type name is " << typeName;
1236 LERROR <<
"Empty data for vertex " << vertex <<
" at " << __FILE__ <<
", line " << __LINE__;
1237 LERROR <<
"This is a bug. Returning null entity for" << se->
getString() << typeName;
1238 return boost::shared_ptr< BoWNamedEntity >();
1241 LinguisticCode category=m_macroAccessor->readValue(data->begin()->properties);
1243 boost::shared_ptr< BoWNamedEntity > bowNE(
new
1244 BoWNamedEntity(sp[getNamedEntityNormalization(v, annotationData)],
1250 auto neParts = createNEParts(v, annotationData, anagraph, posgraph, frompos);
1252 if (neParts.empty())
1255 LWARN <<
"No parts kept for named entity " << (*se).getString();
1257 boost::shared_ptr< BoWToken > bowToken(
new
1258 BoWToken(sp[getNamedEntityNormalization(v, annotationData)],
1260 offset+(*se).getPosition(),
1261 (*se).getLength()));
1263 bowToken->setInflectedForm(token->
stringForm());
1265 bowNE->addPart(bowToken);
1269 for (
auto p=neParts.begin(); p!=neParts.end(); p++)
1272 boost::shared_ptr< BoWToken > bowToken(
new BoWToken((*p).lemma,(*p).category,
1273 offset+(*p).position,
1275 bowToken->setInflectedForm((*p).inflectedForm);
1277 LDEBUG <<
"BowGenerator: specific entity part infl " << (*p).inflectedForm;
1279 bowNE->addPart(bowToken);
1284 const auto& features=(*se).getFeatures();
1285 for (
auto f=features.begin(), f_end=features.end(); f!=f_end; f++)
1287 bowNE->setFeature((*f).getName(),
1288 (*f).getValueLimaString());
1291 LDEBUG <<
"CreateSpecificEntity: created features " << QString::fromUtf8(bowNE->getFeaturesUTF8String().c_str());
1294 auto elem = bowNE->getIdUTF8String();
1295 if (alreadyStored.find(elem) != alreadyStored.end())
1298 LDEBUG <<
"BowGenerator: BoWNE already stored. Skipping it.";
1300 return boost::shared_ptr< BoWNamedEntity >();
1305 alreadyStored.insert(elem);
1312QList< boost::shared_ptr< BoWPredicate > > BowGeneratorPrivate::createPredicate(
1315 const AnnotationData* annotationData,
1318 const uint64_t offset,
1319 std::set< LinguisticGraphVertex >& visited,
1320 bool keepAnyway)
const
1324 LINFO <<
"BowGenerator::createPredicate ling:" << lgv <<
"; annot:" << agv;
1326 QList< boost::shared_ptr< BoWPredicate > > result;
1333 if (predicateIds.size()>1)
1335 LDEBUG <<
"BowGenerator::createPredicate Predicate has"
1336 << predicateIds.size() <<
"values:" << predicateIds;
1345 for (
int i = 0 ; i < predicateIds.size(); i++)
1347 auto predicate = predicateIds[i];
1352 LDEBUG <<
"BowGenerator::createPredicate The role(s) related to "
1353 << predicate <<
" is/are ";
1357 boost::tie(outIt, outIt_end) = boost::out_edges(agv, annotationData->
getGraph());
1358 QMultiMap<Common::MediaticData::EntityType,
1359 boost::shared_ptr< AbstractBoWElement > > roles;
1361 for (; outIt != outIt_end; outIt++)
1364 auto semRoleVx = boost::target(*outIt, annotGraph);
1367 typeAnnot).split(
"|");
1368 if (predicateIds.size() != semRoleIds.size())
1371 LERROR <<
"BowGenerator::createPredicate predicateIds and semRoleIds sizes are different:"
1372 << predicateIds.size() <<
"and" << semRoleIds.size();
1373 LERROR <<
"BowGenerator::createPredicate abort this predicate creation";
1376 Q_ASSERT(predicateIds.size() == semRoleIds.size());
1381 if (semRole.isEmpty())
continue;
1385 auto posGraphSemRoleVertices = annotationData->
matches(
"annot", semRoleVx,
1387 if (!posGraphSemRoleVertices.empty())
1389 auto posGraphSemRoleVertex = *(posGraphSemRoleVertices.begin());
1390 if (posGraphSemRoleVertex == lgv)
1393 LERROR <<
"BowGenerator::createPredicate role vertex is the same as the trigger vertex. Abort this role.";
1397 LDEBUG <<
"BowGenerator::createPredicate Calling createAbstractBoWElement on PoS graph vertex"
1398 << posGraphSemRoleVertex;
1400 auto semRoleTokens = createAbstractBoWElement(posGraphSemRoleVertex,
1408 LDEBUG <<
"BowGenerator::createPredicate Created "
1409 << semRoleTokens.size()
1410 <<
"token for the role associated to" << predicate;
1413 if (!semRoleTokens.empty())
1415 roles.insert(semRoleEntity, semRoleTokens[0].second);
1421 LDEBUG <<
"BowGenerator::createPredicate Found no matching for the semRole in the annot graph";
1425 catch (
const Lima::LimaException& e)
1428 LERROR <<
"BowGenerator::createPredicate Unknown semantic role"
1429 << semRole <<
";" << e.
what();
1433 boost::shared_ptr< BoWPredicate > bowP(
new BoWPredicate());
1434 bowP->setPosition(offset+token->
position());
1435 bowP->setLength(token->
length());
1436 bowP->setPredicateType(predicateEntity);
1437 auto pEntityType = bowP->getPredicateType();
1439 LDEBUG <<
"BowGenerator::createPredicate Created a Predicate for the verbal class "
1444 bowP->setRoles(roles);
1445 auto pRoles = bowP->roles();
1446 for (
auto it = pRoles.begin(); it != pRoles.end(); it++)
1448 auto outputRoles = boost::dynamic_pointer_cast<BoWToken>(it.value());
1449 if (outputRoles != 0)
1453 LDEBUG <<
"BowGenerator::createPredicate Associated "
1454 << QString::fromStdString(outputRoles->getOutputUTF8String())
1455 <<
" to it" <<
"via the semantic role label "<< roleLabel ;
1460 result.append(bowP);
1462 catch (
const Lima::LimaException& e)
1465 LERROR <<
"BowGenerator::createPredicate Unknown predicate"
1466 << predicate <<
";" << e.
what();
1467 return QList< boost::shared_ptr< BoWPredicate > >();
1482 std::set< LinguisticGraphVertex >& visited,
1483 bool keepAnyway)
const
1487 LINFO <<
"BowGenerator::createSemanticRelationPredicate " << lgvs <<
", src:" << agvs <<
", trgt:"<< agvt
1488 <<
", rel:" << annot.
type().c_str();
1490 QList< boost::shared_ptr< BoWPredicate > > result;
1493 auto predicateIds = QString::fromStdString(annot.
type()).split(
"|");
1495 if (predicateIds.size()>1)
1497 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Semantic relation has"
1498 << predicateIds.size() <<
"values:" << predicateIds;
1506 for (
int i = 0 ; i < predicateIds.size(); i++)
1508 auto predicate = predicateIds[i];
1513 boost::shared_ptr< BoWPredicate > bowP(
new BoWPredicate() );
1515 bowP->setPredicateType(predicateEntity);
1517 bowP->setPosition(0);
1520 std::vector<AnnotationGraphVertex> vertices;
1521 vertices.push_back(agvs);
1522 vertices.push_back(agvt);
1524 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate The role(s) related to "
1525 << annot.
type() <<
" is/are ";
1528 boost::shared_ptr< AbstractBoWElement > > roles;
1530 for (
const auto& semRoleVx: vertices)
1532 auto anaGraphSemRoleVertices = annotationData->
matches(
"annot", semRoleVx,
1534 if (!anaGraphSemRoleVertices.empty())
1536 auto anaGraphSemRoleVertex = *anaGraphSemRoleVertices.begin();
1537 auto posGraphSemRoleVertices = annotationData->
matches(
"AnalysisGraph",
1538 anaGraphSemRoleVertex,
1540 if (!posGraphSemRoleVertices.empty())
1542 auto posGraphSemRoleVertex = *(posGraphSemRoleVertices.begin());
1543 if (posGraphSemRoleVertex == lgvs)
1546 LERROR <<
"BowGenerator::createSemanticRelationPredicate role vertex is the same as the trigger vertex. Abort this role.";
1551 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Calling createAbstractBoWElement on PoS graph vertex"
1552 << posGraphSemRoleVertex;
1554 auto semRoleTokens = m_d->createAbstractBoWElement(posGraphSemRoleVertex,
1562 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Created "
1563 << semRoleTokens.size() <<
"token for the role associated to "
1564 << annot.
type().c_str();
1566 if (!semRoleTokens.empty())
1569 roles.insert(semRoleEntity, semRoleTokens[0].second);
1575 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Found no matching for the semRole in the annot graph";
1581 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Created a Predicate for the semantic relation"
1587 bowP->setRoles(roles);
1589 for (
auto it = roles.begin(); it != roles.end(); it++)
1591 auto outputRoles=boost::dynamic_pointer_cast<BoWToken>(it.value());
1592 if (outputRoles != 0)
1594 auto roleLabel = it.key().isNull() ? QString()
1596 LDEBUG <<
"BowGenerator::createSemanticRelationPredicate Associated "
1597 << QString::fromStdString(outputRoles->getOutputUTF8String())
1598 <<
" to it" <<
"via the semantic role label "<< roleLabel ;
1603 result.append(bowP);
1608 LERROR <<
"BowGenerator::createSemanticRelationPredicate Unknown predicate"
1609 << predicate <<
";" << e.
what();
1610 return QList< boost::shared_ptr< BoWPredicate > >();
1617StringsPoolIndex BowGeneratorPrivate::getNamedEntityNormalization(
1625 LDEBUG <<
"BowGenerator::getNamedEntityNormalization: no SpecificEntity annotation for " << v <<
" ; return 0";
1627 return static_cast<StringsPoolIndex
>(0);
1631 LINFO <<
"BowGenerator::getNamedEntityNormalization: m_NEnormalization is " << m_NEnormalization;
1633 StringsPoolIndex normalizedForm(0);
1634 switch (m_NEnormalization)
1636 case NORMALIZE_NE_INFLECTED:
1640 case NORMALIZE_NE_LEMMA:
1644 case NORMALIZE_NE_NORMALIZEDFORM:
1650 case NORMALIZE_NE_NETYPE:
1657 normalizedForm=sp[typeStr];
1661 LDEBUG <<
"BowGenerator::getNamedEntityNormalization return " << normalizedForm;
1663 return normalizedForm;
1666std::vector<BowGeneratorPrivate::NamedEntityPart> BowGeneratorPrivate::createNEParts(
1679 const auto namedEntity =
1682 std::vector<BowGeneratorPrivate::NamedEntityPart> parts;
1685 bool useOnePart(
false);
1686 bool useDefaultParts(
true);
1688 uint64_t position(0);
1748 position=namedEntity->getPosition();
1749 length=namedEntity->getLength();
1750 LimaString normalizedForm = sp[getNamedEntityNormalization(v, annotationData)];
1751 LimaString str = sp[namedEntity->getNormalizedString()];
1753 push_back(NamedEntityPart(normalizedForm,
1755 m_properNounCategory,
1758 else if (useDefaultParts)
1761 for (
auto m = namedEntity->vertices().cbegin();
1762 m != namedEntity->vertices().cend(); m++)
1767 if (data !=
nullptr && !data->empty())
1769 const auto& elem = *(data->begin());
1771 if (! m_keepAllNamedEntityParts && ! shouldBeKept(elem))
1774 LDEBUG <<
"BowGenerator: part of named entity not kept: " << token->
stringForm();
1779 LinguisticCode category;
1782 category = useCategory;
1786 category = m_macroAccessor->readValue(elem.
properties);
1788 parts.push_back(NamedEntityPart(token->
stringForm(),
1800boost::shared_ptr< BoWToken > BowGeneratorPrivate::createCompoundTense(
1802 const AnnotationData* annotationData,
1805 const uint64_t offset,
1806 std::set<LinguisticGraphVertex>& visited)
const
1810 LINFO <<
"BowGenerator: createCompoundTense " << v;
1814 return boost::shared_ptr< BoWToken >();
1819 auxVertex = ppVertex = std::numeric_limits< AnnotationGraphVertex >::max();
1821 boost::tie(it, it_end) = boost::out_edges(v, annotationData->
getGraph());
1822 for (; it != it_end; it++)
1826 auxVertex = boost::target(*it, annotationData->
getGraph());
1830 ppVertex = boost::target(*it, annotationData->
getGraph());
1833 if ( auxVertex == std::numeric_limits< AnnotationGraphVertex >::max()
1834 || ppVertex == std::numeric_limits< AnnotationGraphVertex >::max())
1836 return boost::shared_ptr< BoWToken >();
1842 boost::shared_ptr< BoWToken> head, extension;
1845 head = createCompoundTense(ppVertex, annotationData, anagraph, posgraph, offset, visited);
1850 *(annotationData->
matches(
"annot", ppVertex,
"PosGraph").begin());
1852 std::vector< std::pair<boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > ppBoWTokens = createAbstractBoWElement(ppTokVertex, anagraph, posgraph, offset, annotationData, visited,
true);
1853 if (ppBoWTokens.empty())
1855 return boost::shared_ptr< BoWToken >();
1859 head = boost::dynamic_pointer_cast<BoWToken>(ppBoWTokens.back().second);
1860 ppBoWTokens.pop_back();
1865 return boost::shared_ptr< BoWToken >();
1869 extension = createCompoundTense(auxVertex, annotationData, anagraph, posgraph, offset, visited);
1874 *(annotationData->
matches(
"annot", auxVertex,
"PosGraph").begin());
1876 std::vector<std::pair<boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > auxBoWTokens = createAbstractBoWElement(auxTokVertex, anagraph, posgraph, offset, annotationData, visited,
true);
1877 if (auxBoWTokens.empty())
1879 return boost::shared_ptr< BoWToken >();
1883 extension = boost::dynamic_pointer_cast<BoWToken>(auxBoWTokens.back().second);
1884 auxBoWTokens.pop_back();
1887 if (extension == 0 && head != 0)
1889 return boost::shared_ptr< BoWToken >();
1893 boost::shared_ptr< BoWToken > complex(
1896 head->getCategory(),
1897 extension->getPosition(),
1898 (head->getPosition()+head->getLength()-extension->getPosition())));
1899 complex->setVertex(head->getVertex());
1900 complex->setInflectedForm(head->getInflectedForm());
1902 LDEBUG <<
"BowGenerator: Built complex: " << *complex;
#define LIMA_ANALYSISDUMPERS_EXPORT
This file is the main header file for the data related to annotation graphs.
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, AGVertexProperties, AGEdgeProperties > AnnotationGraph
The graph class.
DependencyGraph::out_edge_iterator DependencyGraphOutEdgeIt
DependencyGraph::vertex_descriptor DependencyGraphVertex
boost::property_map< DependencyGraph, edge_deprel_type_t >::const_type CEdgeDepRelTypePropertyMap
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, DepVertexProperties, DepEdgeProperties > DependencyGraph
The dependency graph class.
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
Data used for the syntactic analyzis of texts.
Holds an annotation graph and gives an API to manipulate it.
uint64_t intAnnotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
std::set< AnnotationGraphVertex > matches(const std::string &first, AnnotationGraphVertex firstVx, const std::string &second) const
Gets the set of vertices matched in the second graph by the given vertex of the first graph.
const GenericAnnotation & annotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
const LimaString & stringAnnotation(AnnotationGraphVertex v1, AnnotationGraphVertex v2, const LimaString &annot) const
ValueType * pointerValue()
This is a BoW element used to represent a predicate (n-ary relation, template or semantic frame).
This class is used to type the relation between a part and its enclosing complex token.
This is a complex token used to represent a multiword term.
This class represents a list of elements, that are pointers on polymmorphic tokens that can be simple...
Provide function to read write and check a property.
std::string & getParamsValueAtKey(const std::string &key)
return a message when a 'param' was not found
The main LIMA exception class.
virtual const char * what() const override
static LinguisticCode fromUInt(uint64_t v)
friend class BowGenerator
std::vector< std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::BoWToken > > > buildTermFor(const AnnotationGraphVertex &vx, const AnnotationGraphVertex &tgt, const LinguisticGraph &anagraph, const LinguisticGraph &posgraph, const uint64_t offset, const SyntacticAnalysis::SyntacticData *syntacticData, const Common::AnnotationGraphs::AnnotationData *annotationData, std::set< LinguisticGraphVertex > &visited) const
Creates the terms reachable from the given annotation vertex.
friend class BowGeneratorPrivate
boost::shared_ptr< Common::BagOfWords::BoWNamedEntity > createSpecificEntity(const LinguisticGraphVertex &vertex, const AnnotationGraphVertex &v, const Common::AnnotationGraphs::AnnotationData *annotationData, const LinguisticGraph &anagraph, const LinguisticGraph &posgraph, const uint64_t offset, bool frompos) const
std::vector< std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::AbstractBoWElement > > > createAbstractBoWElement(const LinguisticGraphVertex v, const LinguisticGraph &anagraph, const LinguisticGraph &posgraph, const uint64_t offsetBegin, const Common::AnnotationGraphs::AnnotationData *annotationData, std::set< LinguisticGraphVertex > &visited, bool keepAnyway=false) const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, MediaId language)
QList< boost::shared_ptr< Common::BagOfWords::BoWPredicate > > createSemanticRelationPredicate(const LinguisticGraphVertex &lgvs, const AnnotationGraphVertex &agvs, const AnnotationGraphVertex &agvt, const SemanticAnalysis::SemanticRelationAnnotation &annot, const Common::AnnotationGraphs::AnnotationData *annotationData, const LinguisticGraph &anagraph, const LinguisticGraph &posgraph, uint64_t offset, std::set< LinguisticGraphVertex > &visited, bool keepAnyway) const
Builds a BoWPredicate corresponding to a semantic relation (an edge in the annotation graph holding a...
This enumeration lists the types of annotations used to specify the role of an annotation in a compou...
StringsPoolIndex getRealization() const
Common::MediaticData::ConceptType getConceptType() const
Holds morphosyntactic informations.
uint64_t position() const
const LimaString & stringForm() const
An annotation between two annotation graph vertices denoting the semantic relation(s) holding between...
A representation of a specific entity to store in the annotation graph.
StringsPoolIndex getString() const
uint64_t getLength() const
Common::MediaticData::EntityType getType() const
uint64_t getPosition() const
This class points to a graph, its dependency graph and the structure that holds the maping between th...
DependencyGraphVertex depVertexForTokenVertex(const LinguisticGraphVertex &v) const
DependencyGraph * dependencyGraph()
static const MediaticData & single()
static MediaticData & changeable()
AnnotationGraph & getGraph()
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
AnnotationGraph::in_edge_iterator AnnotationGraphInEdgeIt
AnnotationGraph::out_edge_iterator AnnotationGraphOutEdgeIt
bool hasStringAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
bool hasIntAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
bool hasAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
std::vector< std::pair< Position, Length > > PositionLengthList
LimaString utf8stdstring2limastring(const std::string &src)
bool operator()(const std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::BoWToken > > t1, const std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::BoWToken > > t2) const
StringsPoolIndex normalizedForm
LinguisticCode properties
launch exception related to the configuration file parsing