LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
Automaton.cpp
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// NAUTITIA
7//
8// jys 23-JUL-2002
9//
10// State is the recovery point for automatons. State is a
11// collection of Transition and eventually inner automatons.
12// Inner automatons are called once when state is entered at
13// first time. Inner automatons are called before checking
14// transition. Inner automatons can return a status which
15// ccan be used by transitions.
16
17#include "Automaton.h"
20#include "State.h"
21
26
27
28using namespace Lima;
29using namespace Lima::Common;
30using namespace Lima::Common::Misc;
31using namespace Lima::Common::MediaticData;
33
34namespace Lima
35{
36namespace LinguisticProcessing
37{
38namespace FlatTokenizer
39{
40
42
44 m_startState(0), m_states()
45{
46}
47
49{
50 for (std::vector< State* >::iterator it = m_states.begin();
51 it != m_states.end(); it++)
52 {
53 delete (*it);
54 }
55}
56
59 Manager* manager)
60
61{
63 LDEBUG << "Creating a Tokenizer Automaton (loads file)";
64 MediaId language=manager->getInitializationParameters().language;
65
66 try {
67 QString charChartFileName=Common::Misc::findFileInPaths(Common::MediaticData::MediaticData::single().getResourcesPath().c_str(),unitConfiguration.getParamsValueAtKey("automatonFile").c_str());
68 loadFromFile(charChartFileName.toUtf8().constData());
70 {
71 LERROR << "no parameter 'automatonFile' in tokenizer group for language " << (int) language << " !";
73 }
74}
75
76
77// Run-time. Returns true if a Transition was found open
78const State* Automaton::run(Text& text, const State* state) const
79{
80 const State* currentState = state;
81 if (currentState == 0)
82 {
83 if (m_startState == 0)
84 {
86 LERROR << "No start state";
87 return 0;
88 }
89 currentState = m_startState;
90 }
91#ifdef DEBUG_LP
93 LDEBUG << "Running Automaton ; currentState is '"
94 << ((currentState==0)?"":Common::Misc::limastring2utf8stdstring(currentState->name())) << "'";
95#endif
96 const State* result = currentState->run(text);
97 if (result == 0 && text.position() < text.size())
98 {
100 LWARN << "Current state failed: backtracking to start state";
101 result = m_startState;
102 }
103 return result;
104}
105
106void Automaton::loadFromFile(const std::string& fileName)
107{
108#ifdef DEBUG_LP
110 LDEBUG << "Loading tokenizer automaton from " << fileName;
111#endif
112 SpiritTokenizerLoader parser(*this, fileName);
113}
114
116{
117 std::vector<State*>::iterator it, it_end;
118 it = m_states.begin(); it_end = m_states.end();
119 for (; it != it_end; it++)
120 {
121 if ( (*it)->name() == name )
122 {
123 return *it;
124 }
125 }
126 return 0;
127}
128
129const State* Automaton::stateNamed(const LimaString& name) const
130{
131 std::vector<State*>::const_iterator it, it_end;
132 it = m_states.begin(); it_end = m_states.end();
133 for (; it != it_end; it++)
134 {
135 if ( (*it)->name() == name )
136 {
137 return *it;
138 }
139 }
140 return 0;
141}
142
143
144
145} //namespace FlatTokenizer
146} // namespace LinguisticProcessing
147} // namespace Lima
#define FLATAUTOMATON_CLASSID
Definition Automaton.h:35
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define TOKENIZERLOGINIT
Defines a Factory to create Object of type Base.
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.
Definition LimaCommon.h:345
const State * stateNamed(const LimaString &name) const
void loadFromFile(const std::string &fileName)
const State * run(Text &text, const State *state=0) const
Definition Automaton.cpp:78
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &, Manager *) override
Definition Automaton.cpp:57
const State * run(Text &text) const
Definition State.cpp:51
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
SimpleFactory< AbstractResource, Automaton > flatAutomatonFactory(FLATAUTOMATON_CLASSID)
NAUTITIA.
QString LimaString
Definition LimaString.h:33