LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EnchantSpellingAlternatives.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
8
18
19#include <enchant/enchant++.h>
20
25
26namespace Lima
27{
28namespace LinguisticProcessing
29{
30namespace MorphologicAnalysis
31{
32
34
35
54
55
60
65
68 Manager* manager)
69{
71 m_d->m_language = manager->getInitializationParameters().media;
72 try
73 {
74 m_d->m_enchantDictionary = enchant::Broker::instance()->request_dict(Common::MediaticData::MediaticData::changeable().getMediaId(m_d->m_language).substr(0,2));
75 }
76 catch (enchant::Exception& e)
77 {
78 LIMA_EXCEPTION( "Cannot get Enchant dictionary for language"
79 << Common::MediaticData::MediaticData::changeable().getMediaId(m_d->m_language).c_str() );
80 }
81 try
82 {
83 std::string dico=unitConfiguration.getParamsValueAtKey("dictionary");
84 auto res = LinguisticResources::single().getResource(m_d->m_language,dico);
85 m_d->m_dictionary = std::dynamic_pointer_cast<AbstractAnalysisDictionary>(res);
86 }
87 catch (NoSuchParam& )
88 {
89 LERROR << "no param 'dictionary' in EnchantSpellingAlternatives group for language " << (int) m_d->m_language;
90 throw InvalidConfiguration("no param 'dictionary' in EnchantSpellingAlternatives group for language ");
91 }
92}
93
94
96{
99 LINFO << "MorphologicalAnalysis: starting process EnchantSpellingAlternatives";
100
102 auto tokenList=std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("AnalysisGraph"));
103 LinguisticGraph* g=tokenList->getGraph();
104 VertexDataPropertyMap dataMap=get(vertex_data,*g);
105 VertexTokenPropertyMap tokenMap=get(vertex_token,*g);
107 for (boost::tie(it,itEnd)=vertices(*g) ; it != itEnd ; it++)
108 {
109 LDEBUG << "EnchantSpellingAlternatives::process processing vertex " << *it;
110 Token* currentToken=tokenMap[*it];
111 MorphoSyntacticData* msd=dataMap[*it];
112
113 if (currentToken!=0)
114 {
115 if (msd->empty())
116 {
118 currentToken,
119 msd,
120 sp);
121 }
122 }
123 }
124 LINFO << "MorphologicalAnalysis: ending process EnchantSpellingAlternatives";
125 return SUCCESS_ID;
126}
127
129 Token* token,
130 MorphoSyntacticData* tokenData,
131 FsaStringsPool& sp)
132{
133 // try to find simple Uncapitalization
135 // FIXME Conditions below could be process unit parameters
136 const LimaString& tokenStr=token->stringForm();
137 if (token->status().getAlphaCapital() == T_CAPITAL
138 || token->status().getAlphaCapital() == T_CAPITAL_1ST
139 || token->status().getAlphaCapital() == T_CAPITAL_SMALL
140 || token->status().isAlphaConcatAbbrev()
141 || token->status().isAlphaHyphen()
142 || token->status().isAlphaPossessive()
143 || tokenStr.toUpper() == tokenStr)
144 {
145 return;
146 }
147 std::vector<std::string> suggestions = m_enchantDictionary->suggest(tokenStr.toUtf8().constData());
148 for (std::vector<std::string>::const_iterator it = suggestions.begin(); it != suggestions.end();it++)
149 {
150 LimaString correction = LimaString::fromUtf8((*it).c_str());
151 // FIXME Conditions below could be process unit parameters
152 if ( correction.size() > 1 && correction != tokenStr )
153 {
154 DictionaryEntry* entry = new DictionaryEntry(m_dictionary->getEntry(correction));
155 MorphoSyntacticDataHandler lingInfosHandler(*tokenData, SPELLING_ALTERNATIVE);
156
157
158 if (!entry->isEmpty())
159 {
160 LINFO << "EnchantSpellingAlternativesPrivate::setEnchantSpellingAlternatives correcting" << tokenStr << "into" << correction;
161 // add orthographic alternative to Token;
162 StringsPoolIndex idx=sp[correction];
163 token->addOrthographicAlternatives(idx);
164
165 if (entry->hasLingInfos())
166 {
167 entry->parseLingInfos(&lingInfosHandler);
168 }
169 }
170 else
171 {
172 delete entry;
173 }
174 }
175 }
176}
177
178} // MorphologicAnalysis
179} // LinguisticProcessing
180} // Lima
181
182
183
#define ENCHANT_SPELLING_ALTERNATIVES_CLASSID
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
Definition LimaCommon.h:293
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
boost::property_map< LinguisticGraph, vertex_data_t >::type VertexDataPropertyMap
LinguisticGraph::vertex_iterator LinguisticGraphVertexIt
boost::property_map< LinguisticGraph, vertex_token_t >::type VertexTokenPropertyMap
@ vertex_token
@ vertex_data
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.
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
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
void parseLingInfos(AbstractDictionaryEntryHandler *handler) const
void setEnchantSpellingAlternatives(LinguisticAnalysisStructure::Token *token, LinguisticAnalysisStructure::MorphoSyntacticData *tokenData, FsaStringsPool &sp)
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.
static const LinguisticResources & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
SimpleFactory< MediaProcessUnit, EnchantSpellingAlternatives > enchantSpellingAlternativesFactory(ENCHANT_SPELLING_ALTERNATIVES_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
QString LimaString
Definition LimaString.h:33
launch exception related to the configuration file parsing