39using namespace boost::tuples;
53namespace LinguisticProcessing
55namespace SyntacticAnalysis
69 delete m_bowGenerator;
77 std::deque<std::string> relation_names;
87 for( std::deque<std::string>::iterator src = relation_names.begin() ; src != relation_names.end() ; src++ )
89 m_relation_names.insert(*src);
96 m_bowGenerator->
init(unitConfiguration, m_language);
104 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
107 LERROR <<
"no LinguisticMetaData ! abort";
110 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"AnalysisGraph"));
113 LERROR <<
"no AnalysisGraph ! abort";
116 auto posgraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph"));
119 LERROR <<
"no PosGraph ! abort";
122 auto syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis.
getData(
"SyntacticData"));
123 if (syntacticData==0)
125 syntacticData = std::make_shared<SyntacticData>(posgraph.get(),
nullptr);
126 syntacticData->setupDependencyGraph();
127 analysis.
setData(
"SyntacticData",syntacticData);
129 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
130 if (annotationData==0)
132 LERROR <<
"no annotation graph available !";
136 std::ofstream outputStream;
137 if (!
openLogFile(outputStream,metadata->getMetaData(
"FileName")))
140 LERROR <<
"Can't open log file ";
144 VxToTermsMap compoundsHeads = getCompoundsHeads(annotationData.get(),
145 syntacticData.get(), analysis, posgraph.get(), *(anagraph->getGraph()),
146 *(posgraph->getGraph()));
147 dumpDependencyRelations(outputStream, *(anagraph->getGraph()),
148 *(posgraph->getGraph()), syntacticData.get(),
149 annotationData.get(), compoundsHeads);
158void DepTripletLogger::dumpDependencyRelations(std::ostream& outputStream,
166 LDEBUG <<
"Address of syntactic data used to dump: " << syntData;
168 LDEBUG <<
"Address of dependency graph used to dump: " << depGraph;
176 boost::tie(it, it_end) = edges(*depGraph);
177 for (; it != it_end; it++)
179 LDEBUG <<
"Dumping dependency edge " << source(*it, *depGraph) <<
" -> " << target(*it, *depGraph);
182 LDEBUG <<
"DepTripletLogger::dumpDependencyRelations";
185 LDEBUG <<
"DepTripletLogger::dumpDependencyRelations relation = "
187 std::set<std::string>::const_iterator relationPos =
189 if( relationPos != m_relation_names.end() )
191 LDEBUG <<
"Src : Dep vertex= " << source(*it, *depGraph);
193 LDEBUG <<
"Src : Morph vertex= " << src;
194 LDEBUG <<
"Targ : Dep vertex= " << target(*it, *depGraph) ;
196 LDEBUG <<
"Targ : Morph vertex= " << dest;
198 std::set<StringsPoolIndex> srcLemmas=dataMap[src]->allLemma();
199 std::set<StringsPoolIndex> destLemmas=dataMap[dest]->allLemma();
212 dumpDepWithCompounds(outputStream, anagraph, posgraph, syntData,
213 annotationData, src, dest,
214 *relationPos, compoundsHeads);
218 LDEBUG <<
"DepTripletLogger::dumpDependencyRelations: dump nothing..";
221 catch (
const std::range_error& )
226 LDEBUG <<
"DepTripletLogger::dumpDependencyRelations: catch others.....";
233void DepTripletLogger::dumpDepWithCompounds(
234 std::ostream& outputStream,
241 const std::string& rel,
247 std::set< LinguisticGraphVertex > visited;
248 std::vector< std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation > , boost::shared_ptr< Common::BagOfWords::AbstractBoWElement > > > srcTokens =
250 std::vector< std::pair< boost::shared_ptr< Common::BagOfWords::BoWRelation >, boost::shared_ptr< Common::BagOfWords::AbstractBoWElement > > > destTokens =
253 std::map<std::string, std::set<LinguisticGraphVertex> > srcs, dests;
255 for (
auto srcItr=srcTokens.begin();
256 srcItr!=srcTokens.end();
259 std::set<LinguisticGraphVertex> theSet;
263 srcs.insert(std::make_pair((*srcItr).second->getOutputUTF8String(),theSet));
265 if (compoundsHeads.find(src) != compoundsHeads.end())
267 auto it = (*(compoundsHeads.find(src))).second.begin(),
268 it_end = (*(compoundsHeads.find(src))).second.end();
269 for (; it != it_end; it++)
271 std::ostringstream oss;
272 oss << (*it)->getOutputUTF8String();
273 std::set<LinguisticGraphVertex> theSet;
275 collectVertices(theSet, *it);
276 srcs.insert(std::make_pair(oss.str(), theSet));
280 for (
auto destItr=destTokens.begin();
281 destItr!=destTokens.end();
284 std::set<LinguisticGraphVertex> theSet;
288 dests.insert(std::make_pair((*destItr).second->getOutputUTF8String(),theSet));
290 if (compoundsHeads.find(dest) != compoundsHeads.end())
292 auto it = (*(compoundsHeads.find(dest))).second.begin(),
293 it_end = (*(compoundsHeads.find(dest))).second.end();
294 for (; it != it_end; it++)
296 std::ostringstream oss;
297 oss << (*it)->getOutputUTF8String();
298 std::set<LinguisticGraphVertex> theSet;
300 collectVertices(theSet, *it);
301 dests.insert(std::make_pair(oss.str(),theSet));
306 std::map<std::string, std::set<LinguisticGraphVertex> >::const_iterator sit = srcs.begin();
312 std::map<std::string, std::set<LinguisticGraphVertex> >::const_iterator dit = dests.begin();
317 std::set<LinguisticGraphVertex> intersection;
318 std::insert_iterator<std::set<LinguisticGraphVertex> > ii(intersection, intersection.begin());
319 std::set_intersection( (*sit).second.begin(), (*sit).second.end(),
320 (*dit).second.begin(), (*dit).second.end(),
322 if (intersection.empty())
324 outputStream << (*sit).first <<
"#" << rel <<
"#" << (*dit).first << std::endl;
340 LDEBUG <<
"DepTripletDumper: ========================================";
341 LDEBUG <<
"DepTripletDumper: getting compounds heads";
353 std::set< std::string > alreadyStored;
354 std::set<LinguisticGraphVertex> visited;
355 std::queue<LinguisticGraphVertex> toVisit;
359 while (!toVisit.empty())
367 for (boost::tie(outItr,outItrEnd)=out_edges(v,posgraph);
372 if (visited.find(next)==visited.end())
374 visited.insert(next);
379 if (v != firstVx && v != lastVx)
387 std::set< AnnotationGraphVertex > cpdsHeads = annotationData->
matches(
"PosGraph", v,
"cpdHead");
388 if (!cpdsHeads.empty())
390 std::set< AnnotationGraphVertex >::const_iterator cpdsHeadsIt, cpdsHeadsIt_end;
391 cpdsHeadsIt = cpdsHeads.begin(); cpdsHeadsIt_end = cpdsHeads.end();
392 for (; cpdsHeadsIt != cpdsHeadsIt_end; cpdsHeadsIt++)
395 std::vector<std::pair<boost::shared_ptr< BoWRelation >, boost::shared_ptr< BoWToken > > > bowTokens =
396 m_bowGenerator->
buildTermFor(agv, agv, anagraph, posgraph, 0,
397 syntacticData, annotationData, visited);
398 for (
auto bowItr=bowTokens.begin();
399 bowItr!=bowTokens.end();
402 std::string elem = (*bowItr).second->getIdUTF8String();
403 if (alreadyStored.find(elem) != alreadyStored.end())
409 boost::shared_ptr< BoWTerm > bt = boost::dynamic_pointer_cast<BoWTerm>((*bowItr).second);
412 getCompoundsHeads(result, &*bt);
414 alreadyStored.insert(elem);
421 std::vector<std::pair< boost::shared_ptr< BoWRelation >, boost::shared_ptr< AbstractBoWElement > > > bowTokens=m_bowGenerator->
createAbstractBoWElement(v, anagraph, posgraph, 0, annotationData, visited);
423 for (
auto bowItr=bowTokens.begin();
424 bowItr!=bowTokens.end();
427 std::string elem = (*bowItr).second->getIdUTF8String();
428 if (alreadyStored.find(elem) != alreadyStored.end())
434 boost::shared_ptr< BoWTerm > bt = boost::dynamic_pointer_cast< BoWTerm >((*bowItr).second);
437 getCompoundsHeads(result, &*bt);
439 alreadyStored.insert(elem);
449void DepTripletLogger::getCompoundsHeads(
453 if (result.find(bt->
getVertex()) == result.end())
455 result.insert(std::make_pair(bt->
getVertex(), std::set<const BoWTerm*>()));
458 std::deque<BoWComplexToken::Part>::const_iterator partsit, partsit_end;
460 for (; partsit!=partsit_end; partsit++)
462 if ( boost::dynamic_pointer_cast<BoWTerm>((*partsit).get<1>()) != 0)
464 getCompoundsHeads(result, &*boost::dynamic_pointer_cast<BoWTerm>((*partsit).get<1>()));
469void DepTripletLogger::collectVertices(
470 std::set<LinguisticGraphVertex>& theSet,
475 if (
dynamic_cast<const BoWTerm*
>(term) != 0)
477 std::deque<BoWComplexToken::Part>::const_iterator partsit, partsit_end;
478 partsit =
dynamic_cast<const BoWTerm*
>(term)->getParts().begin();
479 partsit_end =
dynamic_cast<const BoWTerm*
>(term)->getParts().end();
480 for (; partsit!=partsit_end; partsit++)
482 collectVertices(theSet, &*(*partsit).get<1>());
This file is the main header file for the data related to annotation graphs.
#define DEPTRIPLETLOGGER_CLASSID
DependencyGraph::edge_iterator DependencyGraphEdgeIt
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.
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::deque< Part > & getParts(void)
This is a complex token used to represent a multiword term.
This class contains the representation of an element of the bag of words.
uint64_t getVertex() const
std::deque< std::string > & getListsValueAtKey(const std::string &key)
return a message when a 'list' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
A generic process unit to log information in files: contains some common informations such as : outpu...
bool openLogFile(std::ofstream &output, const std::string &sourceFile) const
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.
virtual ~DepTripletLogger()
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
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()
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, DepTripletLogger > DepTripletLoggerFactory(DEPTRIPLETLOGGER_CLASSID)
std::map< LinguisticGraphVertex, std::set< const Common::BagOfWords::BoWTerm * > > VxToTermsMap
launch exception related to the configuration file parsing