16#include <QRegularExpression>
29namespace LinguisticProcessing {
30namespace SpecificEntities {
37m_graph(
"AnalysisGraph"),
55 for (
const auto& s: types) {
61 LDEBUG <<
"No param 'entityTypes' in SpecificEntitiesCompletion group: nothing to do";
79 mutable std::set<std::pair<unsigned int,unsigned int> >
occurrences;
102 else if (this->entityType==other.
entityType) {
110 os << MediaticData::single().getEntityName(ent.
entityType).toUtf8().constData()
115 os <<
"[" << occ.first <<
"-" << occ.second <<
"]";
131 unsigned int posEnd=posBegin+annot->
getLength();
133 auto token=tokenMap[v];
136 LERROR <<
"Empty token for vertex" << v;
144 if (f.getName()==
"value") {
145 entityNorm=f.getValueLimaString();
149 EntityInfo ent(entityType,entityString,entityNorm);
150 Entities::iterator it=find(ent);
159 (*it).occurrences.insert(make_pair(posBegin,posEnd));
165 for (
const auto& e: ent) {
187 const pair<unsigned int,unsigned int>& p):
193 os << MediaticData::single().getEntityName(occ.
entityType).toUtf8().constData()
200 friend QDebug&
operator<<(QDebug& os,
const std::vector<EntityOccurrence>& occ) {
201 for (
const auto& o: occ) { os << o <<
QTENDL; };
213 LINFO <<
"start process";
221 auto graphp = std::dynamic_pointer_cast<LinguisticAnalysisStructure::AnalysisGraph>(analysis.
getData(m_graph));
222 if (graphp ==
nullptr)
225 LERROR <<
"no graph "<< m_graph <<
" ! abort";
228 const auto& graph = *graphp;
234 getEntities(analysis,foundEntities,tokenMap);
235 LDEBUG <<
"SpecificEntitiesCompletion: found entities" <<
QTENDL << foundEntities;
238 std::vector<EntityOccurrence> newEntities;
239 findOccurrences(foundEntities,analysis,newEntities);
240 LDEBUG <<
"SpecificEntitiesCompletion: found new occurrences" <<
QTENDL << newEntities;
244 updateAnalysis(newEntities,analysis);
249void SpecificEntitiesCompletion::getEntities(
AnalysisContent& analysis, Entities& foundEntities,
252 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
253 if (annotationData==0) {
255 LDEBUG <<
"SpecificEntitiesCompletion: no annotation data";
261 boost::tie(itv, itv_end) = vertices(annotationData->getGraph());
262 for (; itv != itv_end; itv++)
266 const SpecificEntityAnnotation* annot = 0;
270 .pointerValue<SpecificEntityAnnotation>();
272 catch (
const boost::bad_any_cast& )
275 LERROR <<
"This annotation is not a SpecificEntity; SE not logged";
286 if (m_entityTypes.find(annot->getType())!=m_entityTypes.end()) {
287 foundEntities.add(v,annot,tokenMap);
293void SpecificEntitiesCompletion::
294findOccurrences(Entities& foundEntities,
296 std::vector<EntityOccurrence>& newEntities)
const
298 auto text = std::dynamic_pointer_cast<LimaStringText>(analysis.
getData(
"Text"));
299 for (
const auto& e: foundEntities)
301 QRegularExpression re(e.regex());
303 QRegularExpressionMatch rx;
304 while ((pos = text->indexOf(re, pos, &rx)) != -1)
306 pair<unsigned int, unsigned int> matchpos(pos+1, pos+1+rx.capturedLength());
307 if (e.occurrences.find(matchpos)==e.occurrences.end()) {
309 newEntities.push_back(EntityOccurrence(e.entityType, rx.captured(0), e.entityString, matchpos));
311 pos += rx.capturedLength();
317void SpecificEntitiesCompletion::
318updateAnalysis(std::vector<EntityOccurrence>& occurrences,
328 auto recoData = std::dynamic_pointer_cast<RecognizerData>(analysis.
getData(
"RecognizerData"));
331 recoData = std::make_shared<RecognizerData>();
332 analysis.
setData(
"RecognizerData", recoData);
336 recoData->setResultData(resultData);
339 map<EntityType,std::unique_ptr<CreateSpecificEntity> > entityCreator;
340 for (
auto t: m_entityTypes) {
341 LimaString entityName=MediaticData::single().getEntityName(t);
342 entityCreator[t]=std::make_unique<CreateSpecificEntity>(m_language,entityName);
345 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(m_graph));
349 std::queue<LinguisticGraphVertex> toVisit;
350 std::set<LinguisticGraphVertex> visited;
356 int currentOccurrence=-1;
361 toVisit.push(firstVx);
364 while (!toVisit.empty())
372 for (boost::tie(outItr,outItrEnd)=out_edges(v,*graph); outItr!=outItrEnd; outItr++)
375 if (visited.find(next)==visited.end())
377 visited.insert(next);
382 if (v != firstVx && v != lastVx)
384 processVertex(v,tokenMap,occurrences,currentOccurrence,currentMatch, entityCreator, anagraph.get(), analysis);
388 catch (std::exception &exc)
391 LWARN <<
"Exception in SpecificEntitiesCompletion : " << exc.what();
395 recoData->removeVertices(analysis);
396 recoData->clearVerticesToRemove();
397 recoData->removeEdges(analysis);
398 recoData->clearEdgesToRemove();
400 recoData->deleteResultData();
407 std::vector<EntityOccurrence>& occurrences,
408 int& currentOccurrence,
410 map<
EntityType,std::unique_ptr<CreateSpecificEntity> >& entityCreator,
415 Token* currentToken=tokenMap[currentVertex];
419 unsigned int pos=currentToken->
position();
420 unsigned int posEnd=pos+currentToken->
length();
422 if (currentOccurrence!=-1) {
424 if (pos>occurrences[currentOccurrence].posEnd) {
426 LDEBUG <<
"--at pos" << pos <<
": missed occurrence" << occurrences[currentOccurrence];
428 currentOccurrence=-1;
432 LDEBUG <<
"--at pos" << pos <<
": in occurrence" << occurrences[currentOccurrence];
438 for (
unsigned int i(0);i<occurrences.size();i++) {
439 if (pos==occurrences[i].posBegin) {
442 LDEBUG <<
"--at pos" << pos <<
": start occurrence" << occurrences[currentOccurrence];
447 if (currentOccurrence!=-1 && posEnd==occurrences[currentOccurrence].posEnd) {
448 LDEBUG <<
"--at pos" << pos <<
": -> end occurrence" << occurrences[currentOccurrence];
451 EntityOccurrence& occ=occurrences[currentOccurrence];
452 currentMatch.
setType(occ.entityType);
455 LDEBUG <<
"found match for occcurrence" << occ <<
":" << currentMatch;
456 (*entityCreator[occ.entityType])(currentMatch,analysis);
459 currentOccurrence=-1;
This file is the main header file for the data related to annotation graphs.
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.
#define SPECIFICENTITIESCOMPLETION_CLASSID
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
void removeData(const QString &id)
remove the analysisData with the given id
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.
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
void setFeature(const std::string &name, const ValueType &value)
const EntityFeatures & features() const
void setType(const Common::MediaticData::EntityType &t)
void addBackVertex(const LinguisticGraphVertex &, bool isKept=true, const LimaString &ruleElementId="")
An AnalysisData containing a LinguisticGraph with a language and an id.
holds surface data of a token
uint64_t position() const
void addOccurrence(unsigned int posBegin, unsigned int posEnd)
EntityInfo(const EntityType &t, const LimaString &e, const LimaString &n)
friend QDebug & operator<<(QDebug &os, const EntityInfo &ent)
std::set< std::pair< unsigned int, unsigned int > > occurrences
bool operator<(const EntityInfo &other) const
void add(LinguisticGraphVertex v, const SpecificEntityAnnotation *annot, const VertexTokenPropertyMap &tokenMap)
friend QDebug & operator<<(QDebug &os, const Entities &ent)
friend QDebug & operator<<(QDebug &os, const std::vector< EntityOccurrence > &occ)
friend QDebug & operator<<(QDebug &os, const EntityOccurrence &occ)
EntityOccurrence(const EntityType &t, const LimaString &s, const LimaString &n, const pair< unsigned int, unsigned int > &p)
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager)
initialize with parameters from configuration file.
SpecificEntitiesCompletion()
LimaStatusCode process(AnalysisContent &analysis) const
Process on data in analysisContent.
virtual ~SpecificEntitiesCompletion()
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
This file contains a class to control log of informations about time, such as logging cumulated time ...
AnnotationGraph::vertex_iterator AnnotationGraphVertexIt
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< MediaProcessUnit, SpecificEntitiesCompletion > specificEntitiesCompletion(SPECIFICENTITIESCOMPLETION_CLASSID)
launch exception related to the configuration file parsing