62QStringList
FORMATS = {
"CoNLL-U",
"CoNLL-03"};
71 return field.replace(
"\r\n",
" ").replace(
'\r',
' ').replace(
'\n',
' ').replace(
'\t',
' ');
76class ConllDumperPrivate
81 ~ConllDumperPrivate() =
default;
84 std::shared_ptr<DumperStream>& dstream,
88 std::map<LinguisticGraphVertex,int>& segmentationMapping,
89 const QString& neType,
103 std::vector<LinguisticGraphVertex>& out,
104 int depth = 0)
const;
110 std::shared_ptr<DumperStream>& dstream,
115 const QString& parentNeType,
124 std::pair<QString, QString> getConllRelName(
126 std::map<LinguisticGraphVertex,int>& segmentationMapping);
138 void flushMultiwordToken(std::shared_ptr<DumperStream>& dstream);
143 void dumpNamedEntity(std::shared_ptr<DumperStream>& dstream,
147 std::map<LinguisticGraphVertex,int>& segmentationMapping,
148 const QString& neType);
154 void collectPredicateTokens(
160 std::shared_ptr<DumperStream>& dstream,
162 const QString& inflectedToken,
163 const QString& lemmatizedToken,
164 const QString& micro,
166 const QString& features,
167 const QString& targetConllIdString,
168 const QString& conllRelName,
170 const QStringList& miscField,
171 const QString& neType,
172 const QString& previousNeType,
173 const Token* ft =
nullptr);
178 QString m_format =
"CoNLL-U";
181 bool m_withColsHeader;
183 QMap<QString, QString> m_conllLimaDepMapping;
192 const std::map< std::string, PropertyManager >* managers;
194 QMultiMap<LinguisticGraphVertex, AnnotationGraphVertex> predicates;
195 QString previousNeType;
201 struct MultiwordToken
207 std::vector<std::string> lines;
209 MultiwordToken m_multiword;
212 std::string> > vertexDependencyInformations;
215ConllDumperPrivate::ConllDumperPrivate():
217 m_withColsHeader(false),
219 m_conllLimaDepMapping(),
223 annotationData(nullptr),
251 m_d->m_language = manager->getInitializationParameters().media;
255 m_d->managers = &m_d->propertyCodeManager->getPropertyManagers();
261 m_d->m_withColsHeader= QString(
263 "withColsHeader").c_str() ).toLower() ==
"true";
272 "mappingFile").c_str());
273 std::ifstream ifs(mappingFile.toStdString(), std::ifstream::binary);
276 LERROR <<
"ERROR: cannot open" << mappingFile;
279 while (ifs.good() && !ifs.eof())
282 QStringList strs = QString::fromUtf8(line.c_str()).split(
'\t');
283 if (strs.size() == 2)
285 m_d->m_conllLimaDepMapping.insert(strs[0],strs[1]);
292 LINFO <<
"no parameter 'mappingFile' in ConllDumper group" <<
" !";
298 m_d->m_format = QString::fromStdString(
300 if (!
FORMATS.contains(m_d->m_format))
302 QString errorMessage;
303 QTextStream qts(&errorMessage);
304 qts <<
"Invalid CoNLL dumper configuration. Known formats are"
305 <<
FORMATS.join(
",") <<
". Got" << m_d->m_format;
318 LDEBUG <<
"ConllDumper::process";
324 m_d->vertexDependencyInformations.clear();
326 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
330 LERROR <<
"ConllDumper::process no LinguisticMetaData ! abort";
334 auto annotationData = std::dynamic_pointer_cast<AnnotationData>(analysis.
getData(
"AnnotationData"));
335 if (annotationData ==
nullptr)
338 LINFO <<
"ConllDumper::process no AnnotationData ! Will not contain NE nor predicates";
340 m_d->annotationData = annotationData.get();
341 auto posGraphData=std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(m_d->m_graph.toStdString()));
346 LERROR <<
"ConllDumper::process graph" << m_d->m_graph <<
"has not been produced: check pipeline";
349 auto posGraph = posGraphData->getGraph();
350 m_d->posGraph = posGraph;
352 auto anaGraphData=std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"AnalysisGraph"));
356 LERROR <<
"ConllDumper::process graph AnalysisGraph has not been produced: check pipeline";
359 auto anaGraph = anaGraphData->getGraph();
360 m_d->anaGraph = anaGraph;
362 auto sd = std::dynamic_pointer_cast<SegmentationData>(analysis.
getData(
"SentenceBoundaries"));
366 LERROR <<
"ConllDumper::process no SentenceBoundaries! abort";
370 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.
getData(
"SyntacticData"));
371 if (syntacticData ==
nullptr)
373 syntacticData = std::make_shared<SyntacticData>(posGraphData.get(),
nullptr);
374 syntacticData->setupDependencyGraph();
375 analysis.
setData(
"SyntacticData", syntacticData);
377 auto depGraph = syntacticData-> dependencyGraph();
378 m_d->depGraph = depGraph;
382 uint64_t nbSentences((sd->getSegments()).size());
383 if (nbSentences == 0)
386 LERROR <<
"ConllDumper::process 0 sentence to process";
390 auto sbItr = sd->getSegments().begin();
392 LDEBUG <<
"ConllDumper::process There are "<< nbSentences <<
" sentences";
394 auto sentenceBegin = sbItr->getFirstVertex();
395 auto sentenceEnd = sbItr->getLastVertex();
397 auto limaConllTokenIdMapping = std::dynamic_pointer_cast<LimaConllTokenIdMapping>(
398 analysis.
getData(
"LimaConllTokenIdMapping"));
399 if (limaConllTokenIdMapping ==
nullptr)
401 limaConllTokenIdMapping = std::make_shared<LimaConllTokenIdMapping>();
402 analysis.
setData(
"LimaConllTokenIdMapping", limaConllTokenIdMapping);
407 const auto originalText = std::dynamic_pointer_cast<LimaStringText>(analysis.
getData(
"Text"));
409 if (m_d->m_format ==
"CoNLL-U")
412 else if (m_d->m_format ==
"CoNLL-03")
414 dstream->out() <<
"-DOCSTART- -X- O O" << std::endl << std::endl;
419 QString errorMessage;
420 QTextStream qts(&errorMessage);
421 qts <<
"ConllDumper::process unknown format" << m_d->m_format;
425 while (sbItr != sd->getSegments().end())
429 if (m_d->m_format ==
"CoNLL-U")
431 if( sentenceNb==1 && m_d->m_withColsHeader )
434 <<
"# global.columns = ID\tFORM\tLEMMA\tUPOS\tXPOS\tFEATS\tHEAD\tDEPREL\tDEPS\tMISC"
437 dstream->out() <<
"# sent_id = " << sentenceNb << std::endl;
439 else if (m_d->m_format ==
"CoNLL-03")
445 QString errorMessage;
446 QTextStream qts(&errorMessage);
447 qts <<
"ConllDumper::process unknown format" << m_d->m_format;
451 sentenceBegin=sbItr->getFirstVertex();
452 sentenceEnd=sbItr->getLastVertex();
453 std::map<LinguisticGraphVertex,int> segmentationMapping;
454 std::map<int,LinguisticGraphVertex> segmentationMappingReverse;
457 LDEBUG <<
"ConllDumper::process begin - end: " << sentenceBegin
458 <<
" - " << sentenceEnd;
462 QQueue<LinguisticGraphVertex> toVisit;
463 QSet<LinguisticGraphVertex> visited;
464 toVisit.enqueue(sentenceBegin);
467 while (v != sentenceEnd && !toVisit.empty())
469 v = toVisit.dequeue();
471 LDEBUG <<
"ConllDumper::process Vertex index : " << v;
477 auto dcurrent = syntacticData->depVertexForTokenVertex(tv);
479 boost::tie(dit,dit_end) = boost::out_edges(dcurrent,*depGraph);
480 for (; dit != dit_end; dit++)
483 LDEBUG <<
"ConllDumper::process Dumping dependency edge "
484 << (*dit).m_source <<
" -> " << (*dit).m_target;
489 auto type = typeMap[*dit];
490 auto syntRelName = m_d->languageData->getSyntacticRelationName(type);
492 LDEBUG <<
"ConllDumper::process relation = " << syntRelName;
493 LDEBUG <<
"ConllDumper::process Src : Dep vertex= "
494 << boost::source(*dit, *depGraph);
495 auto src = syntacticData->tokenVertexForDepVertex(
496 boost::source(*dit, *depGraph));
497 LDEBUG <<
"ConllDumper::process Src : Morph vertex= " << src;
498 LDEBUG <<
"ConllDumper::process Targ : Dep vertex= "
499 << boost::target(*dit, *depGraph);
501 auto dest = syntacticData->tokenVertexForDepVertex(
502 boost::target(*dit, *depGraph));
504 LDEBUG <<
"ConllDumper::process Targ : Morph vertex= " << dest;
509 LDEBUG <<
"ConllDumper::process saving target for"
510 << tv <<
":" << dest << syntRelName;
512 m_d->vertexDependencyInformations.insert(
513 std::make_pair(tv, std::make_pair(dest, syntRelName)));
516 catch (
const std::range_error& )
522 LDEBUG <<
"ConllDumper::process: catch others.....";
534 std::vector<LinguisticGraphVertex> tokenVertices;
535 m_d->entityTokenVertices(v, tokenVertices);
536 segmentationMapping.insert(std::make_pair(v,vertexId));
537 for (
size_t i = 0; i < tokenVertices.size(); ++i)
541 segmentationMapping.insert(std::make_pair(tokenVertices[i], vertexId));
542 segmentationMappingReverse.insert(std::make_pair(vertexId, tokenVertices[i]));
544 LDEBUG <<
"ConllDumper::process conll id : " << vertexId
545 <<
" Lima id : " << tokenVertices[i];
547 collectDependencies(tokenVertices[i]);
549 if (v == sentenceEnd)
554 for (boost::tie(outItr,outItrEnd)=boost::out_edges(v, *posGraph);
555 outItr!=outItrEnd; outItr++)
558 if (!visited.contains(next) && next != posGraphData->lastVertex())
560 toVisit.enqueue(next);
573 sentenceBegin=sbItr->getFirstVertex();
574 sentenceEnd=sbItr->getLastVertex();
577 m_d->collectPredicateTokens( analysis,
581 LDEBUG <<
"ConllDumper::process predicates for sentence between"
582 << sentenceBegin <<
"and" << sentenceEnd <<
"are:" << m_d->predicates;
584 auto keys = m_d->predicates.keys();
587 bool firstTime =
true;
590 while (v != sentenceEnd)
595 if( ft !=
nullptr && v != sentenceBegin )
599 pStart = ft->position();
602 pEnd = ft->position() + ft->length();
605 for (boost::tie(outIter,outIterEnd) = boost::out_edges(v,*posGraph);
606 outIter!=outIterEnd; outIter++)
608 v = boost::target(*outIter,*posGraph);
613 LERROR <<
"ConllDumper::process sentence traversal loop stalls on" << v <<
"; breaking out.";
617 auto curSentenceText = originalText->mid(pStart-1, pEnd-pStart+1);
619 if (m_d->m_format ==
"CoNLL-U")
622 dstream->out() <<
"# text = "
623 << curSentenceText.replace(
"\r\n",
" ").replace(
"\n",
" ").toStdString()
626 else if (m_d->m_format ==
"CoNLL-03") {}
630 QString errorMessage;
631 QTextStream qts(&errorMessage);
632 qts <<
"ConllDumper::process unknown format" << m_d->m_format;
637 toVisit.enqueue(sentenceBegin);
640 while (!toVisit.empty() && v!=sentenceEnd)
642 v = toVisit.dequeue();
644 m_d->dumpPosGraphVertex(dstream,
654 LDEBUG <<
"ConllDumper::process look at out edges of" << v;
657 for (boost::tie(outIter,outIterEnd) = boost::out_edges(v,*posGraph);
658 outIter != outIterEnd; outIter++)
662 LDEBUG <<
"ConllDumper::process looking out vertex" << next;
664 if (!visited.contains(next))
667 LDEBUG <<
"ConllDumper::process enqueuing" << next;
669 visited.insert(next);
670 toVisit.enqueue(next);
673 if (v == sentenceEnd)
679 m_d->flushMultiwordToken(dstream);
680 limaConllTokenIdMapping->insert(std::make_pair(sentenceNb,
681 segmentationMappingReverse));
684 if (sbItr != sd->getSegments().end())
686 dstream->out() << std::endl;
696 if (annotationData ==
nullptr)
698 for (
const auto& vx: annotationData->
matches(m_graph.toStdString(), v,
"annot"))
700 if (annotationData->
hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
711void ConllDumperPrivate::entityTokenVertices(
713 std::vector<LinguisticGraphVertex>& out,
718 const auto se = (depth < 16) ? posGraphEntity(v) :
nullptr;
719 if (se ==
nullptr || se->vertices().empty())
724 for (
const auto& vse : se->vertices())
726 entityTokenVertices(vse, out, depth + 1);
730const SpecificEntityAnnotation* ConllDumperPrivate::getSpecificEntityAnnotation(
735 for (
const auto& vx: annotationData->matches(
"PosGraph", v,
"annot"))
737 if (annotationData->hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
740 auto se = annotationData->annotation(
742 QString::fromUtf8(
"SpecificEntity")).pointerValue<SpecificEntityAnnotation>();
754 std::shared_ptr<DumperStream>& dstream,
758 std::map<LinguisticGraphVertex,int>& segmentationMapping,
759 const QString& parentNeType,
761 const Automaton::EntityFeatures* parentFeatures)
765 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex IN" << v;
767 if (anaGraph ==
nullptr || posGraph ==
nullptr || depGraph ==
nullptr
768 || annotationData ==
nullptr)
771 LERROR <<
"ConllDumperPrivate::dumpPosGraphVertex missing data";
780 if( morphoData != 0 && ft != 0
781 && ((!morphoData->empty()) || ft->length() > 0) && notDone )
784 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex PosGraph nb different LinguisticCode"
785 << morphoData->size();
788 auto micro = getMicro(morphoData);
790 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex graphTag:" << micro;
793 auto feats = getFeats(morphoData);
795 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex feats:" << feats;
799 auto inflectedToken = ft->stringForm().toStdString();
801 QString lemmatizedToken;
802 if (morphoData != 0 && !morphoData->empty())
804 lemmatizedToken = (*sp)[(*morphoData)[0].lemma];
808 QString neType = getNeType(v);
816 const SpecificEntityAnnotation* nestedEntity =
nullptr;
817 if (neType !=
"_" && !parentNeType.isEmpty())
819 nestedEntity = posGraphEntity(v);
821 if (nestedEntity !=
nullptr)
825 bool firstNested = first;
826 for (
const auto& vse : nestedEntity->
vertices())
828 dumpPosGraphVertex(dstream, vse, tokenId, vEndDone, segmentationMapping,
829 parentNeType, firstNested, parentFeatures);
833 else if (neType !=
"_")
835 dumpNamedEntity(dstream, v, tokenId, vEndDone, segmentationMapping, neType);
839 if (!parentNeType.isEmpty())
841 neType = parentNeType;
844 QString conllRelName;
845 QString targetConllIdString;
846 std::tie(conllRelName,
847 targetConllIdString) = getConllRelName(v, segmentationMapping);
849 QStringList miscField;
853 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex specific entity type is"
856 miscField << (QString(
"NE=") + (first?
"B-":
"I-") + neType);
857 const auto annot = getSpecificEntityAnnotation(v);
859 LDEBUG <<
"ConllDumperPrivate::dumpPosGraphVertex specific entity annotation is"
864 const Automaton::EntityFeatures* entityFeatures =
865 (annot !=
nullptr) ? &annot->getFeatures() : parentFeatures;
866 if (entityFeatures !=
nullptr)
868 const auto& features = *entityFeatures;
869 for (
const auto& feature: features)
871 QString featureString;
872 QTextStream qts(&featureString);
874 && !feature.getName().empty()
875 && !feature.getValueString().empty())
877 qts <<
"NE-" << QString::fromStdString(feature.getName()) <<
"=";
879 QString::fromStdString(feature.getValueString()));
882 if (!featureString.isEmpty())
884 miscField << featureString;
890 miscField << (QString(
"Pos=") + QString::number(ft->position()) );
891 miscField << (QString(
"Len=") + QString::number(ft->length()) );
893 if(!hasSpaceAfter(v, posGraph))
895 miscField << QString(
"SpaceAfter=No");
898 miscField << getPredicate(v);
900 if (miscField.empty())
907 QString::fromStdString(inflectedToken),
920 previousNeType = neType;
926void ConllDumperPrivate::collectPredicateTokens(Lima::AnalysisContent& analysis,
933 QMultiMap<LinguisticGraphVertex, AnnotationGraphVertex> result;
935 auto annotationData = std::dynamic_pointer_cast<AnnotationData>(analysis.
getData(
"AnnotationData"));
936 if (annotationData ==
nullptr)
939 LERROR <<
"No annotation data exists: check pipeline";
944 auto tokenList = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(m_graph.toStdString()));
945 if (tokenList ==
nullptr)
948 LERROR <<
"graph" << m_graph <<
"has not been produced: check pipeline";
952 auto graph = tokenList->getGraph();
954 QQueue<LinguisticGraphVertex> toVisit;
955 QSet<LinguisticGraphVertex> visited;
956 toVisit.enqueue(sentenceBegin);
958 while (v != sentenceEnd && !toVisit.empty())
960 v = toVisit.dequeue();
962 LDEBUG <<
"ConllDumperPrivate::collectPredicateTokens vertex:" << v;
966 auto vMatches = annotationData->matches(m_graph.toStdString(), v,
"annot");
967 for (
const auto& vMatch : vMatches)
969 if (annotationData->hasStringAnnotation(vMatch,
"Predicate"))
972 LDEBUG <<
"ConllDumperPrivate::collectPredicateTokens insert"
975 result.insert(v, vMatch);
979 for (boost::tie(outItr,outItrEnd)=boost::out_edges(v,*graph);
980 outItr!=outItrEnd; outItr++)
982 auto next = boost::target(*outItr, *graph);
983 if (!visited.contains(next) && next != tokenList->lastVertex())
985 toVisit.enqueue(next);
994 auto neType = QString::fromUtf8(
"_") ;
995 if (annotationData !=
nullptr)
998 auto matches = annotationData->matches(m_graph.toStdString(), posGraphVertex,
"annot");
999 for (
const auto& vx: matches)
1001 if (annotationData->hasAnnotation(
1002 vx, QString::fromUtf8(
"SpecificEntity")))
1004 auto se = annotationData->annotation(vx, QString::fromUtf8(
"SpecificEntity")).
1005 pointerValue<SpecificEntityAnnotation>();
1014 auto anaVertices = annotationData->matches(m_graph.toStdString(), posGraphVertex,
1017 for (
const auto& anaVertex: anaVertices)
1019 auto matches = annotationData->matches(
"AnalysisGraph",
1022 for (
const auto& vx: matches)
1024 if (annotationData->hasAnnotation(
1025 vx, QString::fromUtf8(
"SpecificEntity")))
1027 auto se = annotationData->annotation(
1028 vx, QString::fromUtf8(
"SpecificEntity"))
1029 .pointerValue<SpecificEntityAnnotation>();
1034 if (neType !=
"_")
break;
1041std::pair<QString, QString> ConllDumperPrivate::getConllRelName(
1043 std::map<LinguisticGraphVertex,int>& segmentationMapping)
1047 LDEBUG <<
"ConllDumperPrivate::getConllRelName" << v;
1049 QString conllRelName =
"_";
1050 int targetConllId = 0;
1051 if (vertexDependencyInformations.count(v) != 0)
1053 auto target = vertexDependencyInformations.find(v)->second.first;
1055 LDEBUG <<
"ConllDumper::process target saved for"
1056 << v <<
"is" << target;
1058 if (segmentationMapping.find(target) != segmentationMapping.end())
1060 targetConllId = segmentationMapping.find(target)->second;
1065 LERROR <<
"ConllDumper::process target" << target
1066 <<
"not found in segmentation mapping";
1069 LDEBUG <<
"ConllDumper::process conll target saved for "
1070 << v <<
" is " << targetConllId;
1072 auto relName = QString::fromUtf8(
1073 vertexDependencyInformations.find(v)->second.second.c_str());
1075 LDEBUG <<
"ConllDumper::process the lima dependency tag for "
1076 << v <<
" is " << relName;
1078 if (m_conllLimaDepMapping.contains(relName))
1080 conllRelName = m_conllLimaDepMapping[relName];
1084 conllRelName = relName;
1089 if (conllRelName ==
"root")
1094 QString targetConllIdString = QString(QLatin1String(
"%1")).arg(targetConllId);
1096 return { conllRelName, targetConllIdString };
1103 LDEBUG <<
"ConllDumperPrivate::getPredicate" << v;
1105 QStringList miscField;
1106 if (annotationData !=
nullptr && predicates.contains(v))
1108 auto keys = predicates.keys();
1109 auto predicate = annotationData->stringAnnotation(predicates.value(v),
1114 LDEBUG <<
"ConllDumper::process output the roles for the"
1115 << keys.size() <<
"predicates";
1117 for (
int i = 0; i < keys.size(); i++)
1119 auto predicateVertex = predicates.value(keys[keys.size()-1-i]);
1121 auto vMatches = annotationData->matches(m_graph.toStdString(), v,
"annot");
1122 if (!vMatches.empty())
1125 LDEBUG <<
"ConllDumper::process there is" << vMatches.size()
1126 <<
"nodes matching PoS graph vertex" << v
1127 <<
"in the annotation graph.";
1129 QString roleAnnotation;
1130 for (
auto it = vMatches.begin(); it != vMatches.end(); it++)
1134 boost::tie(vMatchInEdgesIt, vMatchInEdgesIt_end) =
1135 boost::in_edges(vMatch,annotationData->getGraph());
1136 for (; vMatchInEdgesIt != vMatchInEdgesIt_end; vMatchInEdgesIt++)
1138 auto inVertex = boost::source(*vMatchInEdgesIt,
1139 annotationData->getGraph());
1140 auto inVertexAnnotPosGraphMatches = annotationData->matches(
1141 "annot",inVertex,m_graph.toStdString());
1142 if (inVertex == predicateVertex
1143 && !inVertexAnnotPosGraphMatches.empty())
1147 annotationData->stringAnnotation(*vMatchInEdgesIt,
1153 if (!roleAnnotation.isEmpty() )
1154 predicate = roleAnnotation +
":" + predicate;
1157 if (!predicate.isEmpty())
1159 miscField << predicate;
1169 bool SpaceAfter =
true;
1171 for (boost::tie(outIter,outIterEnd) = boost::out_edges(v, *graph);
1172 outIter!=outIterEnd; outIter++)
1174 auto next = boost::target(*outIter, *graph);
1177 && (nt->position() == ft->position()+ft->length()) )
1186QString ConllDumperPrivate::getMicro(MorphoSyntacticData* morphoData)
1188 return QString::fromUtf8(
static_cast<const LangData&
>(
1190 .getPropertyManager(
"MICRO")
1191 .getPropertySymbolicValue(morphoData->
firstValue(
1192 *m_propertyAccessor)).c_str());
1195QString ConllDumperPrivate::getFeats(MorphoSyntacticData* morphoData)
1201 QStringList featuresList;
1202 for (
auto i = managers->cbegin(); i != managers->cend(); i++)
1204 auto key = QString::fromUtf8(i->first.c_str());
1205 if (key !=
"MACRO" && key !=
"MICRO")
1207 const auto& pa = propertyCodeManager->getPropertyAccessor(key.toStdString());
1208 LinguisticCode lc = morphoData->
firstValue(pa);
1209 auto value = QString::fromUtf8(i->second.getPropertySymbolicValue(lc).c_str());
1210 if (value !=
"NONE")
1212 featuresList << QString(
"%1=%2").arg(key).arg(value);
1217 featuresList.sort();
1219 QTextStream featuresStream(&features);
1220 if (featuresList.isEmpty())
1226 for (
auto featuresListIt = featuresList.cbegin(); featuresListIt != featuresList.cend(); featuresListIt++)
1228 if (featuresListIt != featuresList.cbegin())
1230 featuresStream <<
"|";
1232 featuresStream << *featuresListIt;
1236 LDEBUG <<
"ConllDumper::process features:" << features;
1242QString
matchesS(
const std::set<AnnotationGraphVertex>& s)
1245 QTextStream qts(&result);
1246 for (
auto i: s) {qts << i <<
",";}
1250void ConllDumperPrivate::dumpNamedEntity(std::shared_ptr<DumperStream>& dstream,
1254 std::map<LinguisticGraphVertex,int>& segmentationMapping,
1255 const QString& neType)
1259 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity" << v << tokenId << vEndDone
1268 if (annotationData !=
nullptr)
1271 auto matches = annotationData->
matches(m_graph.toStdString(), v,
"annot");
1273 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity matches PosGraph" << v
1276 for (
const auto& vx: matches)
1279 vx, QString::fromUtf8(
"SpecificEntity")))
1282 QString::fromUtf8(
"SpecificEntity"))
1284 previousNeType =
"O";
1286 for (
const auto& vse : se->vertices())
1288 dumpPosGraphVertex(dstream, vse, tokenId, vEndDone,
1289 segmentationMapping, neType, first,
1290 &se->getFeatures());
1294 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity return after SpecificEntity annotation on PosGraph";
1299 auto anaVertices = annotationData->matches(m_graph.toStdString(), v,
"AnalysisGraph");
1301 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity anaVertices for" << v
1305 assert(anaVertices.size() == 1);
1306 auto anaVertex = *anaVertices.begin();
1308 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity anaVertex is" << anaVertex;
1310 if (annotationData->hasAnnotation(
1311 anaVertex, QString::fromUtf8(
"SpecificEntity")))
1313 auto se = annotationData->annotation(anaVertex,
1314 QString::fromUtf8(
"SpecificEntity"))
1315 .pointerValue<SpecificEntityAnnotation>();
1317 LDEBUG <<
"ConllDumperPrivate::dumpNamedEntity anaVertex se ("
1318 << (*sp)[se->getString()]
1319 <<
") annotation vertices are" << se->vertices()
1320 <<
"and normalized form:" << (*sp)[se->getNormalizedForm()]
1321 <<
"and features:" << se->getFeatures();
1327 previousNeType =
"O";
1329 for (
const auto& vse : se->vertices())
1331 dumpAnalysisGraphVertex(dstream, vse, v, tokenId, vEndDone, neType,
1332 first, se->getFeatures());
1335 previousNeType = neType;
1342 std::shared_ptr<DumperStream>& dstream,
1347 const QString& neType,
1349 const Automaton::EntityFeatures& features)
1354 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex" << v << posGraphVertex
1357 if (anaGraph ==
nullptr || posGraph ==
nullptr || depGraph ==
nullptr
1358 || annotationData ==
nullptr)
1361 LERROR <<
"ConllDumperPrivate::dumpAnalysisGraphVertex missing data";
1371 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex PosGraph token" << v;
1373 if( morphoData != 0 && ft != 0
1374 && ((!morphoData->empty()) || ft->length() > 0) && notDone )
1377 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex PosGraph nb different LinguisticCode"
1378 << morphoData->size();
1381 auto micro = getMicro(morphoData);
1383 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex micro:" << micro;
1386 auto feats = getFeats(morphoData);
1388 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex feats:" << feats;
1392 auto inflectedToken = ft->stringForm().toStdString();
1394 QString lemmatizedToken;
1395 if (morphoData != 0 && !morphoData->empty())
1397 lemmatizedToken = (*sp)[(*morphoData)[0].lemma];
1406 QString conllRelName =
"_";
1407 QString targetConllIdString =
"_";
1409 QStringList miscField;
1413 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex specific entity type is" << neType;
1414 LDEBUG <<
"ConllDumperPrivate::dumpAnalysisGraphVertex posGraphVertex is" << posGraphVertex;
1416 miscField << (QString(
"NE=") + (first?
"B-":
"I-") + neType);
1417 for (
const auto& feature: features)
1419 if(!feature.getName().empty()
1420 && !feature.getValueString().empty())
1422 QString featureString;
1423 QTextStream qts(&featureString);
1424 qts <<
"NE-" << QString::fromStdString(feature.getName()) <<
"=";
1426 QString::fromStdString(feature.getValueString()));
1427 miscField << featureString;
1432 miscField << (QString(
"Pos=") + QString::number(ft->position()) );
1433 miscField << (QString(
"Len=") + QString::number(ft->length()) );
1435 if(!hasSpaceAfter(v, anaGraph))
1437 miscField << QString(
"SpaceAfter=No");
1440 miscField << getPredicate(v);
1442 if (miscField.empty())
1448 QString::fromStdString(inflectedToken),
1453 targetConllIdString,
1465void ConllDumperPrivate::flushMultiwordToken(std::shared_ptr<DumperStream>& dstream)
1467 if (m_multiword.lines.empty())
1471 if (m_multiword.lines.size() > 1)
1475 bool noSpaceAfter =
false;
1476 for (
size_t k = 0; k < m_multiword.lines.size(); ++k)
1478 auto& line = m_multiword.lines[k];
1479 const auto tab = line.rfind(
'\t');
1480 if (tab == std::string::npos)
1482 auto misc = QString::fromStdString(line.substr(tab + 1)).trimmed().split(
'|');
1483 const bool had = misc.removeAll(QString(
"SpaceAfter=No")) > 0;
1484 if (k + 1 == m_multiword.lines.size())
1488 line = line.substr(0, tab + 1) + misc.join(
'|').toStdString() +
"\n";
1490 dstream->out() << m_multiword.firstId <<
"-"
1491 << (m_multiword.firstId + m_multiword.lines.size() - 1)
1492 <<
"\t" <<
oneLine(m_multiword.surface).toStdString()
1493 <<
"\t_\t_\t_\t_\t_\t_\t_"
1494 <<
"\t" << (noSpaceAfter ?
"SpaceAfter=No" :
"_")
1497 for (
const auto& line : m_multiword.lines)
1499 dstream->out() << line;
1501 m_multiword = MultiwordToken();
1504void ConllDumperPrivate::dumpToken(
1505 std::shared_ptr<DumperStream>& dstream,
1507 const QString& inflectedToken,
1508 const QString& lemmatizedToken,
1509 const QString& micro,
1510 const QString& xpos,
1511 const QString& features,
1512 const QString& targetConllIdString,
1513 const QString& conllRelName,
1514 const QString& deps,
1515 const QStringList& miscField,
1516 const QString& neType,
1517 const QString& previousNeType,
1522 LDEBUG <<
"ConllDumperPrivate::dumpToken" << tokenId;
1524 if (m_format ==
"CoNLL-U")
1551 std::ostringstream line;
1553 <<
"\t" <<
oneLine(inflectedToken).toStdString()
1554 <<
"\t" <<
oneLine(lemmatizedToken).toStdString()
1555 <<
"\t" << micro.toStdString()
1556 <<
"\t" << xpos.toStdString()
1557 <<
"\t" << features.toStdString()
1558 <<
"\t" << targetConllIdString.toStdString()
1559 <<
"\t" << conllRelName.toStdString()
1560 <<
"\t" << deps.toStdString()
1561 <<
"\t" << miscField.join(
'|').toStdString()
1566 if (!m_multiword.lines.empty())
1568 if (ft !=
nullptr && ft->
position() >= m_multiword.begin
1569 && ft->
position() < m_multiword.end)
1571 m_multiword.lines.push_back(line.str());
1574 flushMultiwordToken(dstream);
1580 m_multiword.firstId = tokenId;
1581 m_multiword.begin = ft->
position();
1582 m_multiword.end = ft->
position() + surface.size();
1583 m_multiword.surface = surface;
1584 m_multiword.lines.push_back(line.str());
1587 dstream->out() << line.str();
1589 else if (m_format ==
"CoNLL-03")
1602 LDEBUG <<
"ConllDumperPrivate::dumpToken" << tokenId
1603 << inflectedToken.toStdString() << neType << previousNeType;
1605 QString inflectedTokenEscaped =
oneLine(inflectedToken);
1606 inflectedTokenEscaped.replace(
" ",
"_");
1607 dstream->out() << inflectedTokenEscaped.toStdString()
1608 <<
" " << micro.toStdString()
1610 if (neType.isEmpty() || neType ==
"_")
1612 dstream->out() <<
" " <<
"O";
1616 if (neType == previousNeType)
1618 dstream->out() <<
" " <<
"I-";
1622 dstream->out() <<
" " <<
"B-";
1624 dstream->out() << neType.toStdString();
1626 dstream->out() << std::endl;
1631 QString errorMessage;
1632 QTextStream qts(&errorMessage);
1633 qts <<
"ConllDumper::dumpToken unknown format" << m_format;
1635 throw std::runtime_error(errorMessage.toStdString());
This file is the main header file for the data related to annotation graphs.
A graph that stores any data (annotations) referencing primarily nodes of a text anlaysis.
#define CONLLDUMPER_CLASSID
DependencyGraph::out_edge_iterator DependencyGraphOutEdgeIt
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, DepVertexProperties, DepEdgeProperties > DependencyGraph
The dependency graph class.
A graph structure for linguistic analysis.
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
LinguisticGraph::out_edge_iterator LinguisticGraphOutEdgeIt
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
Defines a Factory to create Object of type Base.
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
void setData(const QString &id, std::shared_ptr< AnalysisData > data)
set an analysisData with the given id.
Holds an annotation graph and gives an API to manipulate it.
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
ValueType * pointerValue()
Provide function to read write and check a property.
Provide tools to parse a property file, and deal with the property coding system.
const PropertyManager & getPropertyManager(const std::string &propertyName) const
Get the PropertyManager associated to a property.
Provide tools to manage a specific property.
const PropertyAccessor & getPropertyAccessor() const
give the corresponding PropertyAccessor
std::string & getParamsValueAtKey(const std::string &key)
return a message when a 'param' was not found
InitializableObjectManager< MediaProcessUnit, MediaProcessUnitInitializationParameters > Manager
Use this exception to signal an error in one of the configuration files.
AbstractTextualAnalysisDumper()
std::shared_ptr< DumperStream > initialize(AnalysisContent &analysis) const
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
a list of generic features: each feature is unique (only one feature for a name)
Holds morphosyntactic informations.
LinguisticCode firstValue(const Common::PropertyCode::PropertyAccessor &propertyAccessor) const
Return the first non empty value for the given accessor.
holds surface data of a token
uint64_t position() const
const std::vector< StringsPoolIndex > & orthographicAlternatives() const
A representation of a specific entity to store in the annotation graph.
const std::vector< LinguisticGraphVertex > & vertices() const
static const MediaticData & single()
AnnotationGraph::in_edge_iterator AnnotationGraphInEdgeIt
bool hasAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
Lima::Common::MediaticData::LanguageData LangData
Lima::Common::MediaticData::MediaticData MedData
std::string readLine(std::istream &inputFile)
LimaString transcodeToXmlEntities(const LimaString &str)
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
SimpleFactory< MediaProcessUnit, ConllDumper > conllDumperFactory(CONLLDUMPER_CLASSID)
QString matchesS(const std::set< AnnotationGraphVertex > &s)
static QString oneLine(QString field)
launch exception related to the configuration file parsing