39#include <boost/graph/properties.hpp>
60namespace LinguisticProcessing
63namespace AnalysisDumpers
68typedef boost::color_traits<boost::default_color_type>
Color;
97 m_graph=std::string(
"PosGraph");
106 LERROR <<
"Missing parameter handler in BowDumper configuration";
113 if (allEntities==
"yes" || allEntities==
"1" || allEntities==
"true") {
122 if (allEntities==
"yes" || allEntities==
"1" || allEntities==
"true") {
139 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
142 LERROR <<
"no LinguisticMetaData ! abort";
147 auto h = std::dynamic_pointer_cast<AnalysisHandlerContainer>(analysis.
getData(
"AnalysisHandlerContainer"));
156 LERROR <<
"BowDumper::process: handler " <<
m_handlerName <<
" has not been given to the core client";
160 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"AnalysisGraph"));
163 LERROR <<
"BowDumper::process: no graph 'AnaGraph' available !";
166 auto posgraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph"));
169 LERROR <<
"BowDumper::process: no graph 'PosGraph' available !";
172 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
173 if (annotationData==0)
175 LERROR <<
"BowDumper::process: no annotation graph available !";
178 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.
getData(
"SyntacticData"));
184 if (syntacticData==0)
186 syntacticData = std::make_shared<SyntacticAnalysis::SyntacticData>(
187 std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
m_graph)).get(),
nullptr);
188 syntacticData->setupDependencyGraph();
189 analysis.
setData(
"SyntacticData",syntacticData);
195 bowText.
lang=metadata->getMetaData(
"Lang");
196 uint64_t offset = metadata->getStartOffset();
197 std::set<LinguisticGraphVertex> addedEntities;
198 buildBoWText(annotationData.get(),syntacticData.get(),bowText,analysis,anagraph.get(),posgraph.get(),addedEntities,offset);
200 addAllEntities(annotationData.get(),addedEntities,bowText,anagraph.get(),posgraph.get(),offset);
206 LDEBUG <<
"BowDumper::process writing BoW text on" << &(dstream->out());
223void BowDumper::buildBoWText(
230 std::set<LinguisticGraphVertex>& addedEntities,
231 const uint64_t offset)
const
237 auto sb = std::dynamic_pointer_cast<SegmentationData>(analysis.
getData(
"SentenceBoundaries"));
241 LWARN <<
"no SentenceBounds";
249 addVerticesToBoWText(
263 for (
auto boundItr=sb->getSegments().begin(); boundItr!=sb->getSegments().end(); boundItr++)
269 LDEBUG <<
"BowDumper::buildBoWText dump sentence between " << sentenceBegin <<
" and " << sentenceEnd;
271 LDEBUG <<
"BowDumper::buildBoWText dump simple terms for this sentence";
273 addVerticesToBoWText(annotationData,
289 boost::tie(it, it_end) = boost::edges(annotGraph);
290 for (; it != it_end; it++)
293 LDEBUG <<
"BowDumper::buildBoWText on annotation edge "
294 << source(*it,annotGraph) <<
"->" << target(*it,annotGraph);
297 QString::fromUtf8(
"SemanticRelation")))
300 LDEBUG <<
"found semantic relation";
304 auto agvs = source(*it,annotGraph);
305 auto agvt = target(*it,annotGraph);
306 auto anaGraphVertices = annotationData->
matches(
"annot", agvs,
308 if (anaGraphVertices.empty())
311 LERROR <<
"Found no analysis graph vertex associated to the annotation graph vertex"
312 << agvs <<
". It will crash";
314 auto lgvs = *anaGraphVertices.begin();
315 std::set< LinguisticGraphVertex > visited;
316 bool keepAnyway =
true;
318 const auto& annot = annotationData->
annotation(
319 *it, QString::fromUtf8(
"SemanticRelation"))
322 lgvs, agvs, agvt, annot,
328 for (
const auto& predicate: predicates)
330 bowText.push_back(predicate);
333 catch (
const boost::bad_any_cast& e)
336 LERROR <<
"This annotation is not a SemanticAnnotation";
345void BowDumper::addVerticesToBoWText(
352 const uint64_t offset,
354 std::set<LinguisticGraphVertex>& addedEntities)
const
359 LDEBUG <<
"BowDumper::addVerticesToBoWText from" << begin <<
"to" << end <<
"; offset:" << offset;
371 std::set< std::string > alreadyStored;
372 std::set<LinguisticGraphVertex> visited;
374 std::set<LinguisticGraphVertex> alreadyStoredVertices;
376 std::queue<LinguisticGraphVertex> toVisit;
379 while (!toVisit.empty())
395 for (boost::tie(outItr,outItrEnd)=out_edges(v,graph);
400 if (visited.find(next)==visited.end())
402 visited.insert(next);
407 if (v != firstVx && v != lastVx)
414 std::set< AnnotationGraphVertex > cpdsHeads = annotationData->
matches(
"PosGraph", v,
"cpdHead");
415 std::set< AnnotationGraphVertex > cpdsExts = annotationData->
matches(
"PosGraph", v,
"cpdExt");
416 if (!cpdsHeads.empty())
421 auto cpdsHeadsIt = cpdsHeads.begin(), cpdsHeadsIt_end = cpdsHeads.end();
422 for (; cpdsHeadsIt != cpdsHeadsIt_end; cpdsHeadsIt++)
428 std::vector<std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > > bowTokens =
m_bowGenerator->
buildTermFor(agv, agv, beforePoSGraph, graph, offset, syntacticData, annotationData, visited);
432 for (
auto bowItr=bowTokens.begin(); bowItr!=bowTokens.end(); bowItr++)
434 std::string elem = (*bowItr).second->getIdUTF8String();
435 if (alreadyStored.find(elem) != alreadyStored.end())
443 bowText.push_back((*bowItr).second);
444 std::set<uint64_t> bowTokenVertices = (*bowItr).second->getVertices();
446 alreadyStoredVertices.insert(bowTokenVertices.begin(), bowTokenVertices.end());
447 alreadyStored.insert(elem);
464 else if (!cpdsExts.empty())
471 else if (alreadyStoredVertices.find(v) == alreadyStoredVertices.end())
477 std::vector<std::pair<boost::shared_ptr< BoWRelation>, boost::shared_ptr< AbstractBoWElement>> > bowTokens=
483 for (
auto bowItr=bowTokens.begin();
484 bowItr!=bowTokens.end();
487 std::string elem = (*bowItr).second->getIdUTF8String();
491 if (alreadyStored.find(elem) != alreadyStored.end())
502 bowText.push_back((*bowItr).second);
503 std::set<uint64_t> bowTokenVertices = (*bowItr).second->getVertices();
504 alreadyStoredVertices.insert(bowTokenVertices.begin(), bowTokenVertices.end());
505 alreadyStored.insert(elem);
530 addedEntities.insert(visited.begin(),visited.end());
535void BowDumper::addAllEntities(
537 const std::set<LinguisticGraphVertex>& addedEntities,
541 const uint64_t offset)
const
551 boost::tie(itv, itv_end) = vertices(annotationData->
getGraph());
552 for (; itv != itv_end; itv++)
560 string graph=
"PosGraph";
564 graph=
"AnalysisGraph";
576 if (graph==
"PosGraph") {
577 if (addedEntities.find(v)!=addedEntities.end()) {
584 std::set< AnnotationGraphVertex > posVertices = annotationData->
matches(graph,v,
"PosGraph");
585 if (posVertices.size()==1) {
589 if (addedEntities.find(posVertex)!=addedEntities.end()) {
596 boost::shared_ptr< Common::BagOfWords::BoWNamedEntity >
600 bowText.push_back(ne);
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.
#define BOWDUMPER_CLASSID
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.
defines callback interface
std::shared_ptr< const ShiftFrom > shiftFrom() const
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.
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
void writeBoWText(std::ostream &file, const BoWText &bowText) const
This class represents a list of elements, that are pointers on polymmorphic tokens that can be simple...
void writeBoWText(const BoWText *document, const bool useIterator, const bool useIndexIterator)
std::string & getParamsValueAtKey(const std::string &key)
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.
std::string m_handlerName
std::shared_ptr< DumperStream > initialize(AnalysisContent &analysis) const
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
Compounds::BowGenerator * m_bowGenerator
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
Parameters retrived in the configuration file:
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.
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...
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
const LinguisticGraphVertex & firstVertex(void) const
Returns the first vertex of the graph.
An annotation between two annotation graph vertices denoting the semantic relation(s) holding between...
This class points to a graph, its dependency graph and the structure that holds the maping between th...
This file contains a class to control log of informations about time, such as logging cumulated time ...
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
LimaString utf8stdstring2limastring(const std::string &src)
boost::color_traits< boost::default_color_type > Color
SimpleFactory< MediaProcessUnit, BowDumper > bowDumperFactory(BOWDUMPER_CLASSID)
launch exception related to the configuration file parsing