LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SimpleWord.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 8-OCT-2002
9//
10// SimpleWord is the implementation of the 1st module of
11// Morphological Analysis. Each token from the main tokens
12// path is searched into the specified dictionary.
13
14
15#include "SimpleWord.h"
16
32
33#include <iostream>
34
35using namespace std;
36using namespace Lima::Common::AnnotationGraphs;
37using namespace Lima::Common::MediaticData;
41
42namespace Lima
43{
44namespace LinguisticProcessing
45{
46namespace MorphologicAnalysis
47{
48
50
51
53 m_reader(0)
54{}
55
57{
58 // delete m_reader;
59}
60
63 Manager* manager)
64{
66 MediaId language = manager->getInitializationParameters().media;
68 string dico;
69 try
70 {
71 dico=unitConfiguration.getParamsValueAtKey("dictionary");
72 }
73 catch (NoSuchParam& )
74 {
75 LERROR << "no param 'dictionary' in SimpleWord group for language " << (int) language;
77 }
78
79 auto res = LinguisticResources::single().getResource(language,dico);
80 m_dictionary = std::dynamic_pointer_cast<AbstractAnalysisDictionary>(res);
81
82 try
83 {
84 string confident=unitConfiguration.getParamsValueAtKey("confidentMode");
85#ifdef DEBUG_LP
86 LDEBUG << "SimpleWord set confident mode to:" << confident;
87#endif
88 m_confidentMode=(confident=="true");
89 }
90 catch (NoSuchParam& )
91 {
92 LWARN << "no param 'confidentMode' in SimpleWord group for language " << (int) language;
93 LWARN << "use default value : 'true'";
94 m_confidentMode=true;
95 }
96
97 // initialize dictionary reader
98
99 try
100 {
101 auto chart = unitConfiguration.getParamsValueAtKey("charChart");
102 auto res = LinguisticResources::single().getResource(language,chart);
103 m_charChart = std::dynamic_pointer_cast<FlatTokenizer::CharChart>(res);
104 }
105 catch (NoSuchParam& )
106 {
107 LERROR << "no param 'charChart' in SimpleWord group for language " << (int) language;
108 throw InvalidConfiguration();
109 }
110
111 m_reader = std::make_shared<AlternativesReader>(m_confidentMode, true, true, true, m_charChart,
112 &Common::MediaticData::MediaticData::changeable().stringsPool(language));
113
114 try
115 {
116 auto concat = unitConfiguration.getParamsValueAtKey("parseConcatenated");
117 m_parseConcatenated=(concat=="true");
118 }
119 catch (NoSuchParam& )
120 {
121 LWARN << "no param 'parseConcatenated' in SimpleWord group for language " << (int) language;
122 LWARN << "use default value : 'true'";
123 m_confidentMode=true;
124 }
125
126}
127
128
130 AnalysisContent& analysis) const
131{
132 Lima::TimeUtilsController timer("SimpleWord");
134 LINFO << "starting process SimpleWord";
135
136 auto tokenList=std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("AnalysisGraph"));
137
138
139 LinguisticGraph* g=tokenList->getGraph();
141 VertexTokenPropertyMap tokenMap=get(vertex_token,*g);
142 VertexDataPropertyMap dataMap=get(vertex_data,*g);
143 boost::tie(it,itEnd)=vertices(*g);
144 for (;it!=itEnd;it++)
145 {
146 Token* currentToken=tokenMap[*it];
147 if (currentToken!=0)
148 {
149#ifdef DEBUG_LP
150 LDEBUG << "SimpleWord for token" << currentToken->stringForm();
151#endif
152 // Init handlers
153 MorphoSyntacticData* msd=dataMap[*it];
155 ConcatenatedDataHandler* concatHandler=0;
156 AccentedConcatenatedDataHandler* accentedConcatHandler=0;
157 AbstractDictionaryEntryHandler* accentedHandler=lingInfoHandler;
158
159 if (m_parseConcatenated)
160 {
161 concatHandler = new ConcatenatedDataHandler(g,currentToken,SIMPLE_WORD,m_sp);
162 accentedConcatHandler = new AccentedConcatenatedDataHandler(
163 g,
164 currentToken->stringForm(),
165 currentToken->position(),
166 currentToken->status(),
168 m_sp,
169 m_charChart.get());
171 seh->addHandler(lingInfoHandler);
172 seh->addHandler(accentedConcatHandler);
173 accentedHandler = seh;
174 }
175
176 // parse data
177 DictionaryEntry entry(m_dictionary->getEntry(currentToken->form(),
178 currentToken->stringForm()));
179 m_reader->readAlternatives(*currentToken,
180 *m_dictionary,
181 lingInfoHandler,
182 concatHandler,
183 accentedHandler);
184
185 // finalize
186 if (concatHandler && !concatHandler->getConcatVertices().empty())
187 {
188 linkConcatVertices(g,*it,concatHandler->getConcatVertices());
189 if (msd->empty())
190 {
191 clear_vertex(*it,*g);
192 }
193 }
194 if (accentedConcatHandler && !accentedConcatHandler->getConcatVertices().empty())
195 {
196 linkConcatVertices(g,*it,accentedConcatHandler->getConcatVertices());
197 if (msd->empty())
198 {
199 clear_vertex(*it,*g);
200 }
201 }
202 if (m_parseConcatenated)
203 {
204 delete concatHandler;
205 delete accentedConcatHandler;
206 delete accentedHandler;
207 }
208 delete lingInfoHandler;
209 }
210 }
211
212 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.getData("AnnotationData"));
213 if (annotationData==0)
214 {
215 LINFO << "SimpleWord::process no annotation data, creating and populating it";
216 annotationData = std::make_shared<AnnotationData>();
217 analysis.setData("AnnotationData", annotationData);
218 }
219 tokenList->populateAnnotationGraph(annotationData.get(), "AnalysisGraph");
220 if (std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph")) != 0)
221 {
222 std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph"))->populateAnnotationGraph(
223 annotationData.get(),
224 "PosGraph");
225 }
226
227#ifdef DEBUG_LP
228 LDEBUG << "ending process SimpleWord";
229#endif
230 return SUCCESS_ID;
231}
232
233void SimpleWord::linkConcatVertices(
234 LinguisticGraph* graph,
235 LinguisticGraphVertex srcToken,
236 const std::vector<std::vector<LinguisticGraphVertex> >& concats) const
237{
238 LinguisticGraphInEdgeIt ieItr,ieItrEnd;
239 for (boost::tie(ieItr,ieItrEnd) = in_edges(srcToken,*graph);
240 ieItr!=ieItrEnd;
241 ieItr++)
242 {
243 LinguisticGraphVertex pred=source(*ieItr,*graph);
244 LinguisticGraphOutEdgeIt oeItr,oeItrEnd;
245 for (boost::tie(oeItr,oeItrEnd) = out_edges(srcToken,*graph);
246 oeItr!=oeItrEnd;
247 oeItr++)
248 {
249 LinguisticGraphVertex next=target(*oeItr,*graph);
250 for (std::vector<std::vector<LinguisticGraphVertex> >::const_iterator concatItr=concats.begin();
251 concatItr!=concats.end();
252 concatItr++)
253 {
254 add_edge(pred,concatItr->front(),*graph);
255 add_edge(concatItr->back(),next,*graph);
256 }
257 }
258 }
259}
260
261
262} // MorphologicAnalysis
263} // LinguisticProcessing
264} // Lima
This file is the main header file for the data related to annotation graphs.
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
LinguisticGraph::in_edge_iterator LinguisticGraphInEdgeIt
boost::property_map< LinguisticGraph, vertex_data_t >::type VertexDataPropertyMap
LinguisticGraph::vertex_iterator LinguisticGraphVertexIt
boost::property_map< LinguisticGraph, vertex_token_t >::type VertexTokenPropertyMap
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
@ vertex_data
LinguisticGraph::out_edge_iterator LinguisticGraphOutEdgeIt
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define MORPHOLOGINIT
Defines a Factory to create Object of type Base.
#define SIMPLEWORD_CLASSID
Definition SimpleWord.h:32
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.
const FsaStringsPool & stringsPool(MediaId med) const
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 std::vector< std::vector< LinguisticGraphVertex > > & getConcatVertices() const
const std::vector< std::vector< LinguisticGraphVertex > > & getConcatVertices() const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
static const LinguisticResources & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
This file contains a class to control log of informations about time, such as logging cumulated time ...
SimpleFactory< MediaProcessUnit, SimpleWord > SimpleWordFactory(SIMPLEWORD_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
STL namespace.
launch exception related to the configuration file parsing