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
106 uint64_t addRuleInStorage(Rule* rule);
107
121 uint64_t addRule(TransitionUnit* trigger, Rule* rule);
122
130 void addRule(TransitionUnit* trigger, const uint64_t index);
131
156 uint64_t apply(const LinguisticAnalysisStructure::AnalysisGraph& graph,
157 const LinguisticGraphVertex& begin,
158 const LinguisticGraphVertex& end,
159 AnalysisContent& analysis,
160 std::vector<RecognizerMatch>& result,
161 bool testAllVertices=false,
162 bool stopAtFirstSuccess=true,
163 bool onlyOneSuccessPerType=false,
164 bool returnAtFirstSuccess=false,
165 bool applySameRuleWhileSuccess=false) const;
166
194 uint64_t apply(const LinguisticAnalysisStructure::AnalysisGraph& graph,
195 const LinguisticGraphVertex& begin,
196 const LinguisticGraphVertex& end,
197 const LinguisticGraphVertex& upstreamBound,
198 const LinguisticGraphVertex& downstreamBound,
199 AnalysisContent& analysis,
200 std::vector<RecognizerMatch>& result,
201 bool testAllVertices,
202 bool stopAtFirstSuccess,
203 bool onlyOneSuccessPerType,
204 bool returnAtFirstSuccess,
205 bool applySameRuleWhileSuccess) const;
206
220 uint64_t testOnVertex(const LinguisticAnalysisStructure::AnalysisGraph& graph,
221 LinguisticGraphVertex& current,
222 const LinguisticGraphVertex& begin,
223 const LinguisticGraphVertex& end,
224 AnalysisContent& analysis,
225 std::vector<RecognizerMatch>& result,
226 bool stopAtFirstSuccess=true,
227 bool onlyOneSuccessPerType=false,
228 bool applySameRuleWhileSuccess=false) const;
229
240 uint64_t
241 resolveOverlappingEntities(std::vector<RecognizerMatch>& listEntities,
243 strategy
245
288 uint64_t testSetOfRules(const TransitionUnit& trigger,
289 const SetOfRules& rules,
291 const LinguisticGraphVertex& position,
292 const LinguisticGraphVertex& begin,
293 const LinguisticGraphVertex& end,
294 AnalysisContent& analysis,
295 std::vector<RecognizerMatch>& match,
296 std::set<Common::MediaticData::EntityType>* forbiddenTypes=0,
297 bool stopAtFirstSuccess=true,
298 bool onlyOneSuccessPerType=false,
299 bool applySameRuleWhileSuccess=false) const ;
300
301 //read recognizer from a text file
302 //void readFromTextFile(std::string filename);
303
304 // output the list of triggers with their associated index
305 void listTriggers() const;
306
307 // text output
308 friend LIMA_AUTOMATON_EXPORT std::ostream& operator << (std::ostream&, const Recognizer&);
309
310 void initializeSearchStructure();
311
312 //***************************************************************************
313 protected:
314 // set of transitions associated with a set of pointers
315 // to rules
316 class TriggerRule : public std::pair<TransitionUnit*, SetOfRules> {
317 public:
323 TransitionUnit* transitionUnit() const { return first; }
324 void setTransitionUnit(TransitionUnit* t) { first=t; }
325 const SetOfRules& setOfRules() const { return second; }
326 SetOfRules& setOfRules() { return second; }
327 };
328 class CompareTriggerRule; // comparison function object on TriggerRule
329
330 std::vector<TriggerRule> m_rules;
331 SetOfRules m_ruleStorage; // the place where the rules are actually
332 // stored (dont really need it, but make
333 // it simpler to free the memory)
334 MediaId m_language;
335
338
339 // for debug
340 std::string m_filename;
341
342 //************************************************************
343 // a superstructure on top of m_rules to make the search faster
345
346 void clearSearchStructure();
347
348 // using TransitionSearchStructure
349 void findNextSetOfRules(const LinguisticAnalysisStructure::AnalysisGraph& graph,
350 LinguisticGraphVertex& vertex,
351 AnalysisContent& analysis,
354 std::vector<TriggerRule*>& matchingSetOfRules) const;
355
356 //**********************************************************************
357 // helper functions for constructors and destructors
358 void init();
359 void copy(const Recognizer& a);
360 void freeMem();
361
362 void setNormalizedForm(const LimaString& norm,
363 RecognizerMatch& match) const ;
364
365 // for the binary input/output
366 uint64_t findRuleIndex(Rule*) const;
367
368};
369
370//**********************************************************************
371// inline access functions
372//**********************************************************************
373inline uint64_t Recognizer::numberOfRules() const {
374 return m_rules.size();
375}
376
377} // namespace end
378} // namespace end
379} // namespace end
380
381#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,...
Manage initialization of InitializableObjects using configuration module and parameters.
␈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:320
␈rief a class for the definition of a complete recognizer
Definition recognizer.h:78
TransitionSearchStructure< TriggerRule > m_searchStructure
Definition recognizer.h:344
AutomatonControlParams m_automatonControlParams
parameters to control the search of the automaton
Definition recognizer.h:337
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
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