LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
Tokenizer.cpp
Go to the documentation of this file.
1// Copyright 2002-2019 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 24-JUL-2002
9//
10// Tokenizer is the main program of Tokenizer stuff.
11
12#include "Tokenizer.h"
13
14#include "Automaton.h"
15#include "State.h"
16// #include "linguisticProcessing/core/Tokenizer/CharChart.h"
27#include <string>
28
29using namespace std;
32using namespace Lima::Common::Misc;
33
34namespace Lima
35{
36namespace LinguisticProcessing
37{
38namespace FlatTokenizer
39{
40
42
44{
45public:
47 virtual ~TokenizerPrivate();
48
50 std::shared_ptr<CharChart> _charChart;
51 MediaId _language;
52
53};
54
56{
57}
58
62
65
67{
68 delete m_d;
69}
70
71const std::shared_ptr<CharChart> Tokenizer::charChart() const {return m_d->_charChart;}
72std::shared_ptr<CharChart> Tokenizer::charChart() {return m_d->_charChart;}
73void Tokenizer::setCharChart(std::shared_ptr<CharChart> charChart) {m_d->_charChart = charChart;}
74
77 Manager* manager)
78
79{
80#ifdef DEBUG_LP
82 LDEBUG << "Tokenizer::init";
83#endif
84 m_d->_language=manager->getInitializationParameters().media;
85 try
86 {
87 string charchartId=unitConfiguration.getParamsValueAtKey("charChart");
88 auto res = LinguisticResources::single().getResource(m_d->_language,charchartId);
89 m_d->_charChart = std::dynamic_pointer_cast<CharChart>(res);
90 }
91 catch (NoSuchParam& )
92 {
94 LERROR << "no param 'charChart' in Tokenizer group configuration (language="
95 << (int) m_d->_language << ")";
97 }
98
99 try
100 {
101 QString fileName=Common::Misc::findFileInPaths(Common::MediaticData::MediaticData::single().getResourcesPath().c_str(),unitConfiguration.getParamsValueAtKey("automatonFile").c_str());
103 m_d->_automaton.loadFromFile(fileName.toUtf8().constData());
104 }
105 catch (NoSuchParam& )
106 {
108 LERROR << "no param 'automatonFile' in Tokenizer group configuration (language="
109 << (int) m_d->_language << ")";
110 throw InvalidConfiguration();
111 }
112 // when input XML file is syntactically wrong
113 catch (XmlSyntaxException &exc)
114 {
115 std::ostringstream mess;
116 mess << "XmlSyntaxException at line "<<exc._lineNumber<<" cause: ";
117 switch (exc._why)
118 {
119 case XmlSyntaxException::SYNTAX_EXC : mess << "SYNTAX_EXC"; break;
120 case XmlSyntaxException::NO_DATA_EXC : mess << "NO_DATA_EXC"; break;
121 case XmlSyntaxException::DOUBLE_EXC : mess << "DOUBLE_EXC"; break;
122 case XmlSyntaxException::FWD_CLASS_EXC : mess << "FWD_CLASS_EXC"; break;
123 case XmlSyntaxException::MULT_CLASS_EXC : mess << "MULT_CLASS_EXC"; break;
124 case XmlSyntaxException::EOF_EXC : mess << "EOF_EXC"; break;
125 case XmlSyntaxException::NO_CODE_EXC : mess << "NO_CODE_EXC"; break;
126 case XmlSyntaxException::BAD_CODE_EXC : mess << "BAD_CODE_EXC"; break;
127 case XmlSyntaxException::NO_CLASS_EXC : mess << "NO_CLASS_EXC"; break;
128 case XmlSyntaxException::UNK_CLASS_EXC : mess << "UNK_CLASS_EXC"; break;
129 case XmlSyntaxException::INT_ERROR_EXC : mess << "INT_ERROR_EXC"; break;
130 case XmlSyntaxException::INV_CLASS_EXC : mess << "INV_CLASS_EXC"; break;
131 default: mess << "??";
132 }
134 LERROR << mess.str();
135 throw InvalidConfiguration();
136 }
137 catch (std::exception &exc)
138 {
139 // @todo remove all causes of InfiniteLoopException
141 LERROR << exc.what();
142 throw InvalidConfiguration();
143 }
144
145}
146
148 AnalysisContent& analysis) const
149{
150 TimeUtilsController flatTokenizerProcessTime("FlatTokenizer");
152 LINFO << "start tokenizer process";
153 LimaStringText* originalText=static_cast<LimaStringText*>(analysis.getData("Text").get());
154 AnalysisGraph* anagraph(0);
155
156 anagraph=new AnalysisGraph("AnalysisGraph",m_d->_language,true,true);
157 analysis.setData("AnalysisGraph",anagraph);
158 LinguisticGraph* graph=anagraph->getGraph();
159 // Gets transformed file into characters class string
160 Text* text=new Text(m_d->_language, m_d->_charChart);
161 text->setText(*originalText);
162 text->setGraph(anagraph->firstVertex(),graph);
163
164 LINFO << "Running automaton on" << *originalText;
165 const State* newState = m_d->_automaton.run(*text);
166 while (newState)
167 {
168 LTRACE << "Running automaton";
169 newState = m_d->_automaton.run(*text, newState);
170 }
171 if (text->position() < text->size()-1)
172 {
173 LERROR << "Tokenized up to " << text->position()
174 << " for a text of size " << text->size() ;
175 }
176 text->finalizeAndUnsetGraph();
177 remove_edge(anagraph->firstVertex(),anagraph->lastVertex(),*(anagraph->getGraph()));
178 delete text;
179 return SUCCESS_ID;
180}
181
182} //namespace FlatTokenizer
183} // namespace LinguisticProcessing
184} // namespace Lima
#define LTRACE
Definition LimaCommon.h:156
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define TOKENIZERLOGINIT
Defines a Factory to create Object of type Base.
#define FLATTOKENIZER_CLASSID
Definition Tokenizer.h:25
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.
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
void loadFromFile(const std::string &fileName)
void setCharChart(std::shared_ptr< CharChart > cc)
Definition Automaton.h:62
const State * run(Text &text, const State *state=0) const
Definition Automaton.cpp:78
void setGraph(LinguisticGraphVertex position, LinguisticGraph *graph)
Definition Text.cpp:124
void setText(const Lima::LimaString &text)
Definition Text.cpp:92
const std::shared_ptr< CharChart > charChart() const
Definition Tokenizer.cpp:71
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
Definition Tokenizer.cpp:75
void setCharChart(std::shared_ptr< CharChart > charChart)
Definition Tokenizer.cpp:73
define the limaStringText resource which is the text in limaString formal
An AnalysisData containing a LinguisticGraph with a language and an id.
const LinguisticGraphVertex & lastVertex(void) const
Returns the last vertex of the graph.
const LinguisticGraph * getGraph(void) const
Returns the underlying graph structure.
const LinguisticGraphVertex & firstVertex(void) const
Returns the first vertex of the graph.
static const LinguisticResources & single()
const singleton accessor
Definition Singleton.h:51
This file contains a class to control log of informations about time, such as logging cumulated time ...
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
static SimpleFactory< MediaProcessUnit, Tokenizer > tokenizerFactory(FLATTOKENIZER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
STL namespace.
launch exception related to the configuration file parsing