22#include <boost/algorithm/string.hpp>
37namespace LinguisticProcessing {
38namespace AnalysisDumpers {
97m_all_attributes(false),
98m_templateDefinitions(),
100m_offsetMapping(nullptr),
101m_outputGroups(false),
105 LDEBUG <<
"AbstractIEDumper::AbstractIEDumper()";
117 LDEBUG <<
"AbstractIEDumper::init";
119 std::deque<std::string> eventTemplates;
130 LWARN <<
"No 'graph' parameter in unit configuration '"
131 << unitConfiguration.
getName() <<
"' ; using PosGraph";
147 LDEBUG <<
"no parameter 'domain' in AbstractIEDumper: entities from all domains will be printed";
153 for (
const auto& v: vals) {
159 LDEBUG <<
"no list 'domains' in AbstractIEDumper";
165 for (
const auto& v: vals) {
171 LDEBUG <<
"no list 'ignore' in AbstractIEDumper: all entity types of authorized domains are printed";
177 if (val==
"1" || val==
"true" || val==
"yes") {
193 LDEBUG <<
"no list 'attributes' in AbstractIEDumper: no attributes will be printed";
200 if (val==
"1" || val==
"true" || val==
"yes") {
213 for(std::deque<std::string>::const_iterator it=eventTemplates.begin();it!=eventTemplates.end();it++)
215 std::string templateResource=*it;
221 auto templateDefinitions = std::dynamic_pointer_cast<EventTemplateDefinitionResource>(res);
233 if (str==
"1" || str==
"true" || str==
"yes") {
254 LDEBUG <<
"AbstractIEDumper::process";
257 std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>, std::size_t > mapEntities;
259 std::map <std::tuple <std::size_t, std::string , std::string >, std::size_t > mapAttributes;
261 std::string sourceFile;
263 auto originalText = std::dynamic_pointer_cast<LimaStringText>(analysis.
getData(
"Text"));
265 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
268 LERROR <<
"no LinguisticMetaData ! abort";
272 sourceFile=metadata->getMetaData(
"FileName");
276 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
277 if (annotationData == 0) {
279 LERROR <<
"no annotationData ! abort";
284 auto graphp = std::dynamic_pointer_cast<LinguisticAnalysisStructure::AnalysisGraph>(analysis.
getData(
m_graph));
291 auto eventData = std::dynamic_pointer_cast<EventTemplateData>(analysis.
getData(
"EventTemplateData"));
294 LDEBUG <<
"No data 'EventTemplateData'";
297 auto offsetData = analysis.
getData(
"OffsetMapping");
299 m_offsetMapping = std::dynamic_pointer_cast<OffsetMapping>(offsetData).get();
309 ostream& out=dstream->out();
313 offset=QString::fromStdString(metadata->getMetaData(
"StartOffset")).toUInt();
327 std::string docId(
"");
329 docId=metadata->getMetaData(
"DocId");
344 auto tokenList = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
m_graph));
346 LERROR <<
"graph " <<
m_graph <<
" has not been produced: check pipeline";
352 std::queue<LinguisticGraphVertex> toVisit;
353 std::set<LinguisticGraphVertex> visited;
354 toVisit.push(tokenList->firstVertex());
357 while (!toVisit.empty()) {
360 if (v == tokenList->lastVertex()) {
364 for (boost::tie(outItr,outItrEnd)=out_edges(v,*graph); outItr!=outItrEnd; outItr++)
367 if (visited.find(next)==visited.end())
369 visited.insert(next);
375 outputEntity(out,v,annot,tokenMap,offset,*originalText,mapEntities,mapAttributes,dumperMetadata);
382 boost::tie(itv, itv_end) = vertices(annotationData->getGraph());
383 for (; itv != itv_end; itv++)
386 if (annotationData->hasAnnotation(*itv,QString::fromUtf8(
"SpecificEntity")))
392 annot = annotationData->annotation(*itv,QString::fromUtf8(
"SpecificEntity"))
395 catch (
const boost::bad_any_cast& )
398 LERROR <<
"This annotation is not a SpecificEntity; SE not logged";
404 if (!annotationData->hasIntAnnotation(*itv,QString::fromStdString(
m_graph)))
410 v = annotationData->intAnnotation(*itv,QString::fromStdString(
m_graph));
411 outputEntity(out,v,annot,tokenMap,offset,*originalText,mapEntities,mapAttributes,dumperMetadata);
416 dumperMetadata->
nbEntities+=mapEntities.size();
423 uint nbEvents=
outputEventData(out,eventData.get(),annotationData.get(),tokenMap,mapEntities,offset,
424 *originalText, dumperMetadata);
430 uint nbRelations=
outputSemanticRelations( out, annotationData.get(), tokenMap, mapEntities, offset, dumperMetadata);
437 dumperMetadata->
save(metadata.get());
495 for (
unsigned int i(0); i<e.
eventRoleId.size(); i++) {
501 ostringstream oss; oss << e; os << oss.str();
return os;
510 std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>,std::size_t > mapEntities,
528 set<EventInfos> events;
530 stringstream bufferEvents;
531 for (std::vector<EventTemplate>::const_iterator it= eventData->begin(); it!= eventData->end();it++)
534 if (! templateElements.empty()) {
536 const std::string& templateName=it->getType();
537 LDEBUG <<
"templateName:" << templateName;
539 std::string templateMention=
"";
542 if (t.second->getName()==templateName) templateMention=t.second->getMention();
544 LDEBUG <<
"templateMention" << templateMention;
548 for(map<string,EventTemplateElement>::const_iterator it1= templateElements.begin(); it1!= templateElements.end();it1++)
550 const string typeName=it1->first;
551 LDEBUG <<
"typeName='" << typeName <<
"',templateMention='" << templateMention <<
"'";
555 std::uint64_t position= vToken->
position();
556 std::uint64_t length= vToken->
length();
557 string stringForm = originalText.mid( position+
m_posOffset,length).toUtf8().data();
562 LDEBUG <<
"add event mention as entity typeName=" << typeName;
575 const auto itE=mapEntities.find(make_tuple(position,length,entityType));
577 if (itE!=mapEntities.end()) {
579 LDEBUG <<
"AbstractIEDumper: found index" <<
id <<
"for entity (" << position <<
"," << length <<
"," << entityType <<
")";
582 LERROR <<
"AbstractIEDumper: cannot find index of entity (" << position <<
"," << length <<
"," << entityType <<
")";
586 if (templateMention.compare(typeName)==0) {
598 LDEBUG <<
"Add event infos" << eventInfos;
599 events.insert(eventInfos);
600 LDEBUG <<
"=>" << events.size() <<
"events";
606 unsigned int idEntity=mapEntities.size()+1+metadata->
nbEntities;
607 for (
auto& e: events) {
608 e.eventMentionId=idEntity;
609 std::vector<pair<uint64_t,uint64_t> > positions;
610 LimaString eventMentionString=QString::fromStdString(e.eventMentionString);
611 computePositions(positions,eventMentionString,e.eventMentionPosition,e.eventMentionLength);
623 for (set<EventInfos>::iterator it=events.begin();it!=events.end();) {
624 bool isIncluded(
false);
625 for (set<EventInfos>::iterator it2=std::next(it); it2!=events.end(); it2++) {
626 if ((*it).isIncluded(*it2)) {
627 LDEBUG <<
"=> filter event" << *it <<
"(included in" << *it2 <<
")";
638 unsigned int eventId(1+metadata->
nbEvents);
639 for (
const auto& e: events) {
640 LDEBUG <<
"=> output event"<< eventId <<
"/" << events.size();
641 outputEventString(out, eventId, e.eventMentionId, e.eventMentionType, e.eventRoleId, e.eventRoleType);
644 return events.size();
655 string::size_type prev(0),i=stringForm.indexOf(
"\n");
656 vector<unsigned int> toErase;
657 while (i!=string::npos) {
665 stringForm.replace(i,1,
' ');
666 if (prev!=0 && i==prev+1) {
667 positions.back().first++;
668 toErase.push_back(i);
672 unsigned int nbCharsIgnored=1;
685 uint64_t previousEnd=positions.back().second;
687 positions.back().second=positions.back().first+i;
690 positions.back().second=positions.back().first+(i-prev-1);
692 positions.push_back(make_pair(positions.back().second+nbCharsIgnored,previousEnd));
697 i=stringForm.indexOf(
"\n",prev+1);
700 unsigned int shift=0;
701 for (
const auto i: toErase) {
702 stringForm.remove(i-shift,1);
714 std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>,std::size_t>& mapEntities,
715 std::map <std::tuple <std::size_t, std::string , std::string >, std::size_t >& mapAttributes,
724 LERROR <<
"Vertex " << v <<
" has no entry in the analysis graph token map. This should not happen !!";
731 auto stringForm=originalText.mid( pos+
m_posOffset,len);
732 std::vector<pair<uint64_t,uint64_t> > positions;
739 std::string domainType=MediaticData::single().getEntityGroupName(eType.
getGroupId()).toUtf8().data();
740 std::string entityName= MediaticData::single().getEntityName(eType).toUtf8().data();
741 std::string entityType(entityName);
742 std::size_t posG=entityType.find(
".");
744 entityType = entityType.substr(posG+1);
756 if (positions.size()>0) {
757 len=positions.back().second-pos+1;
760 auto entity=std::make_tuple(pos,len,entityName);
762 if (mapEntities.find(entity)!=mapEntities.end())
766 LWARN <<
"AbstractIEDumper: duplicate entity: " << pos <<
"," << len <<
" " << entityName;
770 std::size_t index = mapEntities.size()+1 + metadata->
nbEntities;
776 LDEBUG <<
"AbstractIEDumper: ignore entity of domain" << domainType;
784 bool forceNoNorm=
false;
789 outputEntityString(out, index, entityType, stringForm.toUtf8().data(), positions, entityFeatures,forceNoNorm);
792 for(Automaton::EntityFeatures::const_iterator featureItr=entityFeatures.cbegin(),features_end=entityFeatures.cend();
793 featureItr!=features_end; featureItr++) {
794 const std::string featName = featureItr->getName();
796 (entityName==
"Numex.NUMBER" && featName ==
"numvalue") ||
797 (entityName==
"Numex.NUMEX" && featName ==
"numvalue") ||
798 (entityName==
"Numex.NUMEX" && featName ==
"unit") ) {
800 std::size_t feat_index = mapAttributes.size()+1 +metadata->
nbAttributes;
801 std::string featValue = featureItr->getValueString();
802 auto feature = std::make_tuple(index,featName,featValue);
803 mapAttributes.insert( std::make_pair(feature, feat_index ) );
810 LDEBUG <<
"AbstractIEDumper: add entity (" << get<0>(entity) <<
"," << get<1>(entity) <<
"," << get<2>(entity) <<
"): index=" << index;
812 mapEntities.insert(std::make_pair(entity,index));
827 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
m_graph,v,
"annot");
828 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
829 it != matches.end(); it++)
832 if (annotationData->
hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
835 se = annotationData->
annotation(vx, QString::fromUtf8(
"SpecificEntity")).
836 pointerValue<SpecificEntityAnnotation>();
846 std::set< AnnotationGraphVertex > anaVertices = annotationData->
matches(
"PosGraph",v,
"AnalysisGraph");
849 for (
const auto& anaVertex : anaVertices) {
851 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
"AnalysisGraph",anaVertex,
"annot");
853 for (
const auto& vx: matches)
855 if (annotationData->
hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
857 se = annotationData->
annotation(vx, QString::fromUtf8(
"SpecificEntity")).
858 pointerValue<SpecificEntityAnnotation>();
875 std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>,std::size_t > mapEntities,
877 uint64_t offset)
const
902 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
m_graph,v,
"annot");
903 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
904 it != matches.end(); it++)
906 if (annotationData->
hasAnnotation(*it,QString::fromUtf8(
"SpecificEntity"))) {
909 annot = annotationData->
annotation(*it,QString::fromUtf8(
"SpecificEntity"))
910 .pointerValue<SpecificEntityAnnotation>();
912 catch (
const boost::bad_any_cast& e) {
917 std::uint64_t position= offset+vToken->
position() ;
918 std::uint64_t length= vToken->
length();
919 for(std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>,std::size_t > ::const_iterator itE=mapEntities.begin();itE!=mapEntities.end();itE++)
921 std::tuple<std::uint64_t,std::uint64_t,std::string> entityIndex= itE->first;
922 if (std::get<0>(entityIndex)==position &&
923 std::get<1>(entityIndex)==length &&
924 typeName.compare(
LimaString(std::get<2>(entityIndex).c_str()))==0) oss << itE->second;
939 std::map<std::tuple<std::uint64_t,std::uint64_t,std::string>,std::size_t > mapEntities,
950 boost::tie(it, it_end) = edges(annotGraph);
951 for (; it != it_end; it++) {
953 if (annotationData->
hasAnnotation(*it,QString::fromUtf8(
"SemanticRelation")))
959 annot = annotationData->
annotation(*it,QString::fromUtf8(
"SemanticRelation"))
962 catch (
const boost::bad_any_cast& e)
968 std::string annotType( annot->
type() );
969 std::size_t posG=annotType.find(
".");
971 annotType = annotType.substr(posG+1);
975 LDEBUG <<
"AbstractIEDumper: add relation (type" << annotType <<
", source" << source(*it,annotGraph) <<
", target " << target(*it,annotGraph) <<
"): index=" << index;
994 std::set< AnnotationGraphVertex > matches = annotationData->
matches(
m_graph,v,
"annot");
995 for (std::set< AnnotationGraphVertex >::const_iterator it = matches.begin();
996 it != matches.end(); it++)
999 if (annotationData->
hasAnnotation(vx, QString::fromUtf8(
"SemanticRelation")))
1002 sr = annotationData->
annotation(vx, QString::fromUtf8(
"SemanticRelation")).
1003 pointerValue<SemanticRelationAnnotation>();
1030 ostringstream prevPos;
1031 for (
const auto& p: positions) { prevPos <<
"(" << p.first <<
"," << p.second <<
") "; }
1033 for (
auto& p: positions) {
1039 for (
auto& p: positions) {
1045 ostringstream newPos;
1046 for (
const auto& p: positions) { newPos <<
"(" << p.first <<
"," << p.second <<
") "; }
1047 LDEBUG <<
"AbstractIEDumper::adjustPositions" << prevPos.str() <<
"->" << newPos.str();
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.
boost::property_map< LinguisticGraph, vertex_token_t >::type VertexTokenPropertyMap
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.
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 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
ValueType * pointerValue()
std::string & getParamsValueAtKey(const std::string &key)
std::deque< std::string > & getListsValueAtKey(const std::string &key)
return a message when a 'list' was not found
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
std::shared_ptr< DumperStream > initialize(AnalysisContent &analysis) const
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
std::set< std::string > m_domains
std::string outputSemanticRelationArg(const std::string &vertexRole, const AnnotationGraphVertex &vertex, const VertexTokenPropertyMap &tokenMap, std::map< std::tuple< std::uint64_t, std::uint64_t, std::string >, std::size_t > mapEntities, const Common::AnnotationGraphs::AnnotationData *annotationData, uint64_t offset) const
std::set< std::string > m_ignore
std::set< std::string > m_templateNames
virtual void outputEventsHeader(std::ostream &) const
virtual ~AbstractIEDumper()
OffsetMapping * m_offsetMapping
virtual void outputGlobalHeader(std::ostream &, const std::string &="", const LimaStringText &=LimaStringText("")) const
specific output functions to deal with actual output format these functions are called inside the pro...
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
uint outputEventData(std::ostream &out, const EventAnalysis::EventTemplateData *eventData, const Common::AnnotationGraphs::AnnotationData *annotationData, const VertexTokenPropertyMap &tokenMap, std::map< std::tuple< std::uint64_t, std::uint64_t, std::string >, std::size_t > mapEntities, uint64_t offset, LimaStringText originalText, IEDumperMetaData *metadata) const
virtual bool addEventMentionAsEntity() const
std::deque< std::string > m_attributes
const SpecificEntities::SpecificEntityAnnotation * getSpecificEntityAnnotation(LinguisticGraphVertex v, const Common::AnnotationGraphs::AnnotationData *annotationData) const
virtual void outputEntitiesFooter(std::ostream &) const
virtual void outputRelationsHeader(std::ostream &) const
virtual void outputEventsFooter(std::ostream &) const
virtual unsigned int outputRelationString(std::ostream &out, unsigned int relationId, const std::string &relationType, const std::string &sourceArgString, const std::string &targetArgString) const =0
virtual void outputEventString(std::ostream &out, unsigned int eventId, unsigned int eventMentionId, const std::string &eventMentionType, const std::vector< unsigned int > &eventRoleId, const std::vector< std::string > &eventRoleType) const =0
virtual void outputEntitiesHeader(std::ostream &) const
bool outputEntity(std::ostream &out, LinguisticGraphVertex v, const SpecificEntities::SpecificEntityAnnotation *annot, const VertexTokenPropertyMap &tokenMap, uint64_t offset, LimaStringText originalText, std::map< std::tuple< uint64_t, uint64_t, std::string >, std::size_t > &mapEntities, std::map< std::tuple< std::size_t, std::string, std::string >, std::size_t > &mapAttributes, IEDumperMetaData *metadata) const
virtual void outputEntityString(std::ostream &out, unsigned int entityId, const std::string &entityType, const std::string &entityString, const std::vector< std::pair< uint64_t, uint64_t > > &positions, const Automaton::EntityFeatures &entityFeatures, bool noNorm=false) const =0
uint outputSemanticRelations(std::ostream &out, const Common::AnnotationGraphs::AnnotationData *annotationData, const VertexTokenPropertyMap &tokenMap, std::map< std::tuple< std::uint64_t, std::uint64_t, std::string >, std::size_t > mapEntities, uint64_t offset, IEDumperMetaData *metadata) const
virtual void outputGlobalFooter(std::ostream &) const
void adjustPositions(std::vector< std::pair< uint64_t, uint64_t > > &positions, uint64_t offset=0) const
virtual void outputAttributesString(std::ostream &out, unsigned int entityId, std::map< std::tuple< std::size_t, std::string, std::string >, std::size_t > &mapAttributes) const =0
virtual LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
virtual void outputRelationsFooter(std::ostream &) const
void computePositions(std::vector< std::pair< uint64_t, uint64_t > > &positions, LimaString &stringForm, uint64_t pos, uint64_t len) const
std::map< std::string, std::shared_ptr< EventAnalysis::EventTemplateDefinitionResource > > m_templateDefinitions
const SemanticAnalysis::SemanticRelationAnnotation * getSemanticRelationAnnotation(LinguisticGraphVertex v, const Common::AnnotationGraphs::AnnotationData *annotationData) const
void adjustPosition(std::uint64_t &position, uint64_t offset=0) const
unsigned int getOriginalOffset(unsigned int newOffset) const
a list of generic features: each feature is unique (only one feature for a name)
define the limaStringText resource which is the text in limaString formal
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
holds surface data of a token
uint64_t position() 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.
const Automaton::EntityFeatures & getFeatures() const
uint64_t getLength() const
Common::MediaticData::EntityType getType() const
uint64_t getPosition() const
static const LinguisticResources & single()
const singleton accessor
static void logElapsedTime(const std::string &mess, const std::string &taskCategory=std::string(""))
log the number of microseconds since last UpdateCurrentTime
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
AnnotationGraph & getGraph()
AnnotationGraph::vertex_iterator AnnotationGraphVertexIt
AnnotationGraph::edge_iterator AnnotationGraphEdgeIt
AnnotationGraph::vertex_descriptor AnnotationGraphVertex
bool hasIntAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
bool hasAnnotation(AnnotationGraphVertex v, const LimaString &annot) const
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
std::multimap< std::string, EventTemplateElement > TemplateElements
bool operator<(const EventInfos &other) const
std::vector< unsigned int > eventRoleId
std::vector< std::string > eventRoleType
unsigned int eventMentionId
std::string eventMentionString
std::string eventMentionType
bool isIncluded(const EventInfos &other) const
uint64_t eventMentionPosition
uint64_t eventMentionLength
friend std::ostream & operator<<(std::ostream &os, const EventInfos e)
friend QDebug & operator<<(QDebug &os, const EventInfos e)