41using namespace boost::tuples;
55namespace LinguisticProcessing
57namespace AnalysisDumpers
72 delete m_bowGenerator;
80 std::deque<std::string> relation_names;
90 for( std::deque<std::string>::iterator src = relation_names.begin() ; src != relation_names.end() ; src++ )
92 m_relation_names.insert(*src);
101 LERROR <<
"DepTripleDumper::init: Missing parameter handler in DepTripleDumper configuration";
109 m_bowGenerator->
init(unitConfiguration, m_language);
117 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
120 LERROR <<
"DepTripleDumper::process no LinguisticMetaData ! abort";
123 LDEBUG <<
"handler will be: " << m_handler;
126 auto h = std::dynamic_pointer_cast<AnalysisHandlerContainer>(analysis.
getData(
"AnalysisHandlerContainer"));
130 LERROR <<
"DepTripleDumper::process: handler " << m_handler <<
" has not been given to the core client";
134 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"AnalysisGraph"));
137 LERROR <<
"DepTripleDumper::process: hno AnalysisGraph ! abort";
140 auto posgraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph"));
143 LERROR <<
"DepTripleDumper::process: hno PosGraph ! abort";
146 auto sb = std::dynamic_pointer_cast<SegmentationData>(analysis.
getData(
"SentenceBoundaries"));
149 LERROR <<
"DepTripleDumper::process: no SentenceBounds ! abort";
152 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.
getData(
"SyntacticData"));
153 if (syntacticData==0)
155 syntacticData = std::make_shared<SyntacticData>(posgraph.get(),
nullptr);
156 syntacticData->setupDependencyGraph();
157 analysis.
setData(
"SyntacticData",syntacticData);
159 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
160 if (annotationData==0)
162 LERROR <<
"DepTripleDumper::process: no annotation graph available !";
168 std::ostream outputStream(&hsb);
170 VxToTermsMap compoundsHeads = getCompoundsHeads(annotationData.get(),
171 syntacticData.get(), analysis, posgraph.get(), *(anagraph->getGraph()),
172 *(posgraph->getGraph()));
173 if (sb->getGraphId() !=
"PosGraph")
175 LERROR <<
"DepTripleDumper::process: SentenceBounds have been computed on " << sb->getGraphId() <<
" !";
176 LERROR <<
"DepTripleDumper::process: DotDependencyGraphWriter needs SentenceBounds on PosGraph";
183 for (std::vector<Segment>::const_iterator boundItr=(sb->getSegments()).begin();
184 boundItr!=(sb->getSegments()).end();
190 dumpDependencyRelations(outputStream,
193 *(anagraph->getGraph()),
194 *(posgraph->getGraph()), syntacticData.get(),
195 annotationData.get(), compoundsHeads);
196 outputStream <<
"." << std::endl;
208void DepTripleDumper::dumpDependencyRelations(std::ostream& outputStream,
218 LDEBUG <<
"Address of syntactic data used to dump: " << syntData;
220 LDEBUG <<
"Address of dependency graph used to dump: " << depGraph;
227 std::vector<DependencyGraphEdge> edges;
228 std::list<LinguisticGraphVertex> toVisit;
229 toVisit.push_back(src);
230 std::set<LinguisticGraphVertex> visited;
232 while (!toVisit.empty())
235 visited.insert(current);
238 for (boost::tie(outItr,outItrEnd)=out_edges(current,posgraph); outItr!=outItrEnd; outItr++)
241 if (visited.find(next)==visited.end() && next != dest)
243 toVisit.push_back(next);
248 boost::tie(dit,dit_end)=out_edges(dcurrent,*depGraph);
249 for (; dit != dit_end; dit++)
251 edges.push_back(*dit);
254 std::vector<DependencyGraphEdge>::const_iterator it, it_end;
255 it = edges.begin(); it_end = edges.end();
256 for (; it != it_end; it++)
258 LDEBUG <<
"Dumping dependency edge " << (*it).m_source <<
" -> " << (*it).m_target;
261 LDEBUG <<
"DepTripleDumper::dumpDependencyRelations";
264 LDEBUG <<
"DepTripleDumper::dumpDependencyRelations relation = "
266 std::set<std::string>::const_iterator relationPos =
268 if( relationPos != m_relation_names.end() )
270 LDEBUG <<
"Src : Dep vertex= " << source(*it, *depGraph);
272 LDEBUG <<
"Src : Morph vertex= " << src;
273 LDEBUG <<
"Targ : Dep vertex= " << target(*it, *depGraph) ;
275 LDEBUG <<
"Targ : Morph vertex= " << dest;
277 std::set<StringsPoolIndex> srcLemmas=dataMap[src]->allLemma();
278 std::set<StringsPoolIndex> destLemmas=dataMap[dest]->allLemma();
291 dumpDepWithCompounds(outputStream, anagraph, posgraph, syntData,
292 annotationData, src, dest,
293 *relationPos, compoundsHeads);
297 LDEBUG <<
"DepTripleDumper::dumpDependencyRelations: dump nothing..";
300 catch (
const std::range_error& )
305 LDEBUG <<
"DepTripleDumper::dumpDependencyRelations: catch others.....";
312void DepTripleDumper::dumpDepWithCompounds(
313 std::ostream& outputStream,
320 const std::string& rel,
326 std::set< LinguisticGraphVertex > visited;
327 std::vector<std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > srcTokens =
329 std::vector<std::pair<boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > destTokens =
332 std::map<std::string, std::set<LinguisticGraphVertex> > srcs, dests;
334 for (
auto srcItr=srcTokens.begin(); srcItr!=srcTokens.end(); srcItr++)
336 std::set<LinguisticGraphVertex> theSet;
340 srcs.insert(std::make_pair((*srcItr).second->getOutputUTF8String(),theSet));
342 if (compoundsHeads.find(src) != compoundsHeads.end())
344 auto it = (*(compoundsHeads.find(src))).second.begin(), it_end= (*(compoundsHeads.find(src))).second.end();
345 for (; it != it_end; it++)
347 std::ostringstream oss;
348 oss << (*it)->getOutputUTF8String();
349 std::set<LinguisticGraphVertex> theSet;
351 collectVertices(theSet, *it);
352 srcs.insert(std::make_pair(oss.str(), theSet));
356 for (
auto destItr=destTokens.begin(); destItr!=destTokens.end(); destItr++)
358 std::set<LinguisticGraphVertex> theSet;
362 dests.insert(std::make_pair((*destItr).second->getOutputUTF8String(),theSet));
364 if (compoundsHeads.find(dest) != compoundsHeads.end())
366 auto it = (*(compoundsHeads.find(dest))).second.begin(), it_end = (*(compoundsHeads.find(dest))).second.end();
367 for (; it != it_end; it++)
369 std::ostringstream oss;
370 oss << (*it)->getOutputUTF8String();
371 std::set<LinguisticGraphVertex> theSet;
373 collectVertices(theSet, *it);
374 dests.insert(std::make_pair(oss.str(),theSet));
379 std::map<std::string, std::set<LinguisticGraphVertex> >::const_iterator sit = srcs.begin();
385 std::map<std::string, std::set<LinguisticGraphVertex> >::const_iterator dit = dests.begin();
390 std::set<LinguisticGraphVertex> intersection;
391 std::insert_iterator<std::set<LinguisticGraphVertex> > ii(intersection, intersection.begin());
392 std::set_intersection( (*sit).second.begin(), (*sit).second.end(),
393 (*dit).second.begin(), (*dit).second.end(),
395 if (intersection.empty())
397 outputStream << (*sit).first <<
"#" << rel <<
"#" << (*dit).first << std::endl;
413 LDEBUG <<
"DepTripleDumper: ========================================";
414 LDEBUG <<
"DepTripleDumper: getting compounds heads";
426 std::set< std::string > alreadyStored;
427 std::set<LinguisticGraphVertex> visited;
428 std::queue<LinguisticGraphVertex> toVisit;
432 while (!toVisit.empty())
440 for (boost::tie(outItr,outItrEnd)=out_edges(v,posgraph);
445 if (visited.find(next)==visited.end())
447 visited.insert(next);
452 if (v != firstVx && v != lastVx)
460 std::set< AnnotationGraphVertex > cpdsHeads = annotationData->
matches(
"PosGraph", v,
"cpdHead");
461 if (!cpdsHeads.empty())
463 std::set< AnnotationGraphVertex >::const_iterator cpdsHeadsIt, cpdsHeadsIt_end;
464 cpdsHeadsIt = cpdsHeads.begin(); cpdsHeadsIt_end = cpdsHeads.end();
465 for (; cpdsHeadsIt != cpdsHeadsIt_end; cpdsHeadsIt++)
468 std::vector<std::pair<boost::shared_ptr< BoWRelation>, boost::shared_ptr< BoWToken > > > bowTokens =
469 m_bowGenerator->
buildTermFor(agv, agv, anagraph, posgraph, 0,
470 syntacticData, annotationData, visited);
471 for (
auto bowItr=bowTokens.begin();
472 bowItr!=bowTokens.end();
475 std::string elem = (*bowItr).second->getIdUTF8String();
476 if (alreadyStored.find(elem) != alreadyStored.end())
482 boost::shared_ptr<BoWTerm> bt = boost::dynamic_pointer_cast<BoWTerm>((*bowItr).second);
485 getCompoundsHeads(result, bt);
487 alreadyStored.insert(elem);
494 std::vector<std::pair<boost::shared_ptr< BoWRelation> , boost::shared_ptr< AbstractBoWElement > > > bowTokens=m_bowGenerator->
createAbstractBoWElement(v, anagraph, posgraph, 0, annotationData, visited);
496 for (
auto bowItr=bowTokens.begin();
497 bowItr!=bowTokens.end();
500 std::string elem = (*bowItr).second->getIdUTF8String();
501 if (alreadyStored.find(elem) != alreadyStored.end())
507 boost::shared_ptr< BoWTerm > bt = boost::dynamic_pointer_cast<BoWTerm>((*bowItr).second);
510 getCompoundsHeads(result, bt);
512 alreadyStored.insert(elem);
522void DepTripleDumper::getCompoundsHeads(
524 boost::shared_ptr< BoWTerm > bt)
const
526 if (result.find(bt->getVertex()) == result.end())
528 result.insert(std::make_pair(bt->getVertex(), std::set<boost::shared_ptr< BoWTerm > >()));
530 result[bt->getVertex()].insert(boost::dynamic_pointer_cast<BoWTerm>(bt));
531 std::deque<BoWComplexToken::Part>::const_iterator partsit, partsit_end;
532 partsit = bt->getParts().begin(); partsit_end = bt->getParts().end();
533 for (; partsit!=partsit_end; partsit++)
535 if ( boost::dynamic_pointer_cast<BoWTerm>((*partsit).get<1>()) != 0)
537 getCompoundsHeads(result, boost::dynamic_pointer_cast<BoWTerm>((*partsit).get<1>()));
542void DepTripleDumper::collectVertices(
543 std::set<LinguisticGraphVertex>& theSet,
544 boost::shared_ptr< BoWToken > term)
const
547 theSet.insert(term->getVertex());
548 if ( boost::dynamic_pointer_cast<BoWTerm>(term) != 0)
550 auto partsit = boost::dynamic_pointer_cast<BoWTerm>(term)->getParts().begin(),
551 partsit_end = boost::dynamic_pointer_cast<BoWTerm>(term)->getParts().end();
552 for (; partsit!=partsit_end; partsit++)
554 collectVertices(theSet, (*partsit).get<1>());
This file is the main header file for the data related to annotation graphs.
#define DEPTRIPLEDUMPER_CLASSID
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.
boost::property_map< LinguisticGraph, vertex_data_t >::const_type CVertexDataPropertyMap
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.
virtual void startAnalysis()=0
function called by the LIMA analyzer on start of a new document
virtual void endAnalysis()=0
function called by the LIMA analyzer on the end of a text part that is to be analyzed
defines callback interface
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.
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
Use this exception to signal an error in one of the configuration files.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual ~DepTripleDumper()
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
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.
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)
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraphVertex & firstVertex(void) const
Returns the first vertex of the graph.
This class points to a graph, its dependency graph and the structure that holds the maping between th...
LinguisticGraphVertex tokenVertexForDepVertex(const DependencyGraphVertex &v) const
LinguisticGraph * graph()
DependencyGraphVertex depVertexForTokenVertex(const LinguisticGraphVertex &v) const
static const MediaticData & 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::vertex_descriptor AnnotationGraphVertex
SimpleFactory< MediaProcessUnit, DepTripleDumper > DepTripleDumperFactory(DEPTRIPLEDUMPER_CLASSID)
std::map< LinguisticGraphVertex, std::set< boost::shared_ptr< Common::BagOfWords::BoWTerm > > > VxToTermsMap
std::map< LinguisticGraphVertex, std::set< const Common::BagOfWords::BoWTerm * > > VxToTermsMap
launch exception related to the configuration file parsing