LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
recognizer.h
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file recognizer.h
9 * @author besancon (besanconr@zoe.cea.fr)
10 * @date Tue Oct 15 2002
11 * copyright Copyright (C) 2002 by CEA LIST
12 * Project Automaton
13 *
14 * @brief recognizer for a certain type of expression : contains a
15 * list of rules
16 *
17 ***********************************************************************/
18
19#ifndef RECOGNIZER_H
20#define RECOGNIZER_H
21
22#include "AutomatonExport.h"
23#include "rule.h"
24#include "recognizerMatch.h"
25#include "transitionUnit.h"
31#include <stdexcept>
32#include <map>
33#include <set>
34
35namespace Lima {
36namespace LinguisticProcessing {
37namespace Automaton {
38
54#define DEFAULT_OVERLAP_STRATEGY IGNORE_FIRST
55
56// several rules are attached to one trigger
57// -> maybe problems if a same unit is trigger of several rules with
58// different transition properties (nokeep, head...) : for the
59// moment, if leads to different triggers, hence problems for the index
60// in the dictionary
61
66typedef std::vector<Rule*> SetOfRules;
67
68#define RECOGNIZER_CLASSID "AutomatonRecognizer"
69
78{
79 Q_OBJECT
80
81friend class AutomatonReader;
82friend class AutomatonWriter;
83
84 public:
85 Recognizer();
86 Recognizer(const Recognizer&);
87 virtual ~Recognizer();
88 Recognizer& operator = (const Recognizer&);
89
90 // initialization function
91 void init(
93 Manager* manager) override
94 ;
95
96 // access functions
97 uint64_t numberOfRules() const;
98
105 void clearRules();
106
114 uint64_t addRuleInStorage(Rule* rule);
115
129 uint64_t addRule(TransitionUnit* trigger, Rule* rule);
130
138 void addRule(TransitionUnit* trigger, const uint64_t index);
139
165 const LinguisticGraphVertex& begin,
166 const LinguisticGraphVertex& end,
167 AnalysisContent& analysis,
168 std::vector<RecognizerMatch>& result,
169 bool testAllVertices=false,
170 bool stopAtFirstSuccess=true,
171 bool onlyOneSuccessPerType=false,
172 bool returnAtFirstSuccess=false,
173 bool applySameRuleWhileSuccess=false) const;
174
203 const LinguisticGraphVertex& begin,
204 const LinguisticGraphVertex& end,
205 const LinguisticGraphVertex& upstreamBound,
206 const LinguisticGraphVertex& downstreamBound,
207 AnalysisContent& analysis,
208 std::vector<RecognizerMatch>& result,
209 bool testAllVertices,
210 bool stopAtFirstSuccess,
211 bool onlyOneSuccessPerType,
212 bool returnAtFirstSuccess,
213 bool applySameRuleWhileSuccess) const;
214
229 LinguisticGraphVertex& current,
230 const LinguisticGraphVertex& begin,
231 const LinguisticGraphVertex& end,
232 AnalysisContent& analysis,
233 std::vector<RecognizerMatch>& result,
234 bool stopAtFirstSuccess=true,
235 bool onlyOneSuccessPerType=false,
236 bool applySameRuleWhileSuccess=false) const;
237
248 uint64_t
249 resolveOverlappingEntities(std::vector<RecognizerMatch>& listEntities,
251 strategy
253
272
296 uint64_t testSetOfRules(const TransitionUnit& trigger,
297 const SetOfRules& rules,
299 const LinguisticGraphVertex& position,
300 const LinguisticGraphVertex& begin,
301 const LinguisticGraphVertex& end,
302 AnalysisContent& analysis,
303 std::vector<RecognizerMatch>& match,
304 std::set<Common::MediaticData::EntityType>* forbiddenTypes=0,
305 bool stopAtFirstSuccess=true,
306 bool onlyOneSuccessPerType=false,
307 bool applySameRuleWhileSuccess=false) const ;
308
309 //read recognizer from a text file
310 //void readFromTextFile(std::string filename);
311
312 // output the list of triggers with their associated index
313 void listTriggers() const;
314
315 // text output
316 friend LIMA_AUTOMATON_EXPORT std::ostream& operator << (std::ostream&, const Recognizer&);
317
319
320 //***************************************************************************
321 protected:
322 // set of transitions associated with a set of pointers
323 // to rules
324 class TriggerRule : public std::pair<TransitionUnit*, SetOfRules> {
325 public:
331 TransitionUnit* transitionUnit() const { return first; }
332 void setTransitionUnit(TransitionUnit* t) { first=t; }
333 const SetOfRules& setOfRules() const { return second; }
334 SetOfRules& setOfRules() { return second; }
335 };
336 class CompareTriggerRule; // comparison function object on TriggerRule
337
338 std::vector<TriggerRule> m_rules;
339 SetOfRules m_ruleStorage; // the place where the rules are actually
340 // stored (dont really need it, but make
341 // it simpler to free the memory)
342 MediaId m_language;
343
346
347 // for debug
348 std::string m_filename;
349
350 //************************************************************
351 // a superstructure on top of m_rules to make the search faster
353
355
356 // using TransitionSearchStructure
358 LinguisticGraphVertex& vertex,
359 AnalysisContent& analysis,
362 std::vector<TriggerRule*>& matchingSetOfRules) const;
363
364 //**********************************************************************
365 // helper functions for constructors and destructors
366 void init();
367 void copy(const Recognizer& a);
368 void freeMem();
369
370 void setNormalizedForm(const LimaString& norm,
371 RecognizerMatch& match) const ;
372
373 // for the binary input/output
374 uint64_t findRuleIndex(Rule*) const;
375
376};
377
378//**********************************************************************
379// inline access functions
380//**********************************************************************
381inline uint64_t Recognizer::numberOfRules() const {
382 return m_rules.size();
383}
384
385} // namespace end
386} // namespace end
387} // namespace end
388
389#endif
#define LIMA_AUTOMATON_EXPORT
QDebug & operator<<(QDebug &qd, const std::string &str)
Definition QsLog.cpp:39
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
InitializableObjectManager< AbstractResource, ResourceInitializationParameters > Manager
␈rief a class for control parameters for the search using the automata
Definition automaton.h:44
TriggerRule(TransitionUnit *t, const SetOfRules &s)
Definition recognizer.h:328
void findNextSetOfRules(const LinguisticAnalysisStructure::AnalysisGraph &graph, LinguisticGraphVertex &vertex, AnalysisContent &analysis, const LinguisticAnalysisStructure::Token *token, const LinguisticAnalysisStructure::MorphoSyntacticData *data, std::vector< TriggerRule * > &matchingSetOfRules) const
void clearRules()
remove all the rules of the recognizer, keeping its language and its control parameters: rules can th...
uint64_t testOnVertex(const LinguisticAnalysisStructure::AnalysisGraph &graph, LinguisticGraphVertex &current, const LinguisticGraphVertex &begin, const LinguisticGraphVertex &end, AnalysisContent &analysis, std::vector< RecognizerMatch > &result, bool stopAtFirstSuccess=true, bool onlyOneSuccessPerType=false, bool applySameRuleWhileSuccess=false) const
test the recognizer on a given vertex : check if this vertex is a trigger and if a rule applies,...
TransitionSearchStructure< TriggerRule > m_searchStructure
Definition recognizer.h:352
uint64_t testSetOfRules(const TransitionUnit &trigger, const SetOfRules &rules, const LinguisticAnalysisStructure::AnalysisGraph &graph, const LinguisticGraphVertex &position, const LinguisticGraphVertex &begin, const LinguisticGraphVertex &end, AnalysisContent &analysis, std::vector< RecognizerMatch > &match, std::set< Common::MediaticData::EntityType > *forbiddenTypes=0, bool stopAtFirstSuccess=true, bool onlyOneSuccessPerType=false, bool applySameRuleWhileSuccess=false) const
TODO: toBeDeleted Parse tokens paths from the trigger point.
AutomatonControlParams m_automatonControlParams
parameters to control the search of the automaton
Definition recognizer.h:345
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
uint64_t addRuleInStorage(Rule *rule)
add a rule in the storage zone
uint64_t apply(const LinguisticAnalysisStructure::AnalysisGraph &graph, const LinguisticGraphVertex &begin, const LinguisticGraphVertex &end, AnalysisContent &analysis, std::vector< RecognizerMatch > &result, bool testAllVertices=false, bool stopAtFirstSuccess=true, bool onlyOneSuccessPerType=false, bool returnAtFirstSuccess=false, bool applySameRuleWhileSuccess=false) const
apply the recognizer on a graph
void setNormalizedForm(const LimaString &norm, RecognizerMatch &match) const
uint64_t resolveOverlappingEntities(std::vector< RecognizerMatch > &listEntities, const OverlapResolutionStrategy &strategy=DEFAULT_OVERLAP_STRATEGY) const
resolve the problem of overlapping entities in the list of entities : when two entities are overlapin...
uint64_t addRule(TransitionUnit *trigger, Rule *rule)
add a rule in the recognizer : add the rule in the storage zone, and associate the trigger to the rul...
An AnalysisData containing a LinguisticGraph with a language and an id.
OverlapResolutionStrategy
an enumerated type to indicate which kind of strategy adopt to deal with two overlapping entities
Definition recognizer.h:43
@ IGNORE_SECOND
the second entity is ignored => assumes the leftmost trigger is more important
Definition recognizer.h:46
@ IGNORE_SMALLEST
the smallest entity is ignored (the one that covers the smallest number of words is assumed to be les...
Definition recognizer.h:48
@ KEEP_ALL_OVERLAPS
do not try to avoid overlaps, find the largest number of entities possible
Definition recognizer.h:51
@ IGNORE_FIRST
the first entity is ignored => assumes the rightmost trigger is more important
Definition recognizer.h:44
std::vector< Rule * > SetOfRules
the SetOfRules type is defined as a vector of pointers on Rule
Definition recognizer.h:66
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.
#define DEFAULT_OVERLAP_STRATEGY
Definition recognizer.h:54