LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
LinguisticResources.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/***************************************************************************
7 * Copyright (C) 2004-2019 by CEA LIST *
8 * *
9 ***************************************************************************/
10
11#include "LinguisticResources.h"
12
19
20#include <QFileInfo>
21
22using namespace std;
24
25
26namespace Lima
27{
28
29namespace LinguisticProcessing
30{
31
33{
35
36public:
39
40 void clear();
41
42 std::map<MediaId, std::shared_ptr<AbstractResource::Manager> > m_resourcesManagers;
43
44 // private member functions
47
48};
49
53
58
63
64LinguisticResources::LinguisticResources() :
67{}
68
69// LinguisticResources::LinguisticResources(const LinguisticResources& lr) :
70// Singleton<LinguisticResources>(),
71// m_d(new LinguisticResourcesPrivate(*lr.m_d))
72// {}
73
78
83
84std::shared_ptr<AbstractResource> LinguisticResources::getResource(MediaId lang,const std::string& id) const
85{
86#ifdef DEBUG_LP
88 LDEBUG << "LinguisticResources::getResource" << this << lang << id.c_str();
89#endif
90 auto it = m_d->m_resourcesManagers.find(lang);
91 if (it==m_d->m_resourcesManagers.end())
92 {
95 "LinguisticResources::getResource: language " << (int)lang << " is not initialized !",
97 }
98 return it->second->getObject(id);
99}
100
102 MediaId lang,
104 bool registerMainKeysInStringPool)
105{
106 // TODO make this function thread safe
108#ifdef DEBUG_LP
109 LDEBUG << "LinguisticResources::initLanguage" << this << confModule.getName();
110#endif
111 if (m_d->m_resourcesManagers.find(lang) != m_d->m_resourcesManagers.end())
112 {
113 LINFO << "LinguisticResources::initLanguage resources already initialized for" << lang;
114 return;
115 }
117 params.language=lang;
118 m_d->m_resourcesManagers[lang] = std::make_shared<AbstractResource::Manager>(confModule, params);
119 auto& module= m_d->m_resourcesManagers[lang]->getModuleConfigurationStructure();
120 m_d->includeResources(module, module);
121
122 if (registerMainKeysInStringPool)
123 {
124 // Initialize FsaStringsPool : main keys must be registered at beginning
125 try
126 {
127 const auto& mainkeys = confModule.getParamValueAtKeyOfGroupNamed("mainKeys","FsaStringsPool");
128 LINFO << "load mainKeys " << mainkeys << " into StringPool";
129 auto res = getResource(lang,mainkeys);
130 auto access = std::dynamic_pointer_cast<AnalysisDict::AbstractAccessResource>(res);
131 access->setMainKeys(true);
133 }
134 catch (NoSuchGroup& )
135 {
136 LERROR << "No group 'FsaStringsPool' defined in Resource module. Can't register any main keys";
137 throw InvalidConfiguration();
138 }
139 catch (NoSuchParam& )
140 {
141 LERROR << "No param 'mainKeys' defined in FsaStringsPool group. Can't register any main keys";
142 throw InvalidConfiguration();
143 }
144 }
145}
146
150{
152#ifdef DEBUG_LP
153 LDEBUG << "LinguisticResourcesPrivate::includeResources" << this << &module << module.getName()
154 << &includeModule << includeModule.getName();
155#endif
156 try
157 {
158 const auto& includeList = includeModule.getListValuesAtKeyOfGroupNamed("includeList", "include");
159 for (const auto& include: includeList)
160 {
161 auto i = include.find("/");
162 if (i==string::npos)
163 {
164 LERROR << "Cannot include resources " << include << ": must specify file and module name";
165 continue;
166 }
167 QString fileName;
168 std::string moduleName;
169 try
170 {
172#ifdef DEBUG_LP
173 LDEBUG << "i="<< i;
174#endif
175 auto configPaths = QString::fromUtf8(
176 Common::MediaticData::MediaticData::single().getConfigPath().c_str()).split(LIMA_PATH_SEPARATOR);
177 for(const auto& confPath: configPaths)
178 {
179 if (QFileInfo::exists(confPath + "/" + string(include, 0, i).c_str()))
180 {
181
182 fileName = (confPath + "/" + string(include, 0, i).c_str());
183 break;
184 }
185 }
186 if (fileName.isEmpty())
187 {
188 LERROR << "No resources" << include << "found in"
190 continue;
191 }
192 moduleName = std::string(include, i+1);
193 LINFO << "LinguisticResourcesPrivate::includeResources filename="<< fileName << "moduleName="<< moduleName;
194 XMLConfigurationFileParser parser(fileName);
195 auto& newMod = parser.getModuleConfiguration(moduleName);
196 module.addModule(newMod);
197#ifdef DEBUG_LP
198 ostringstream oss;
199 for (const auto& group: module)
200 {
201 oss << group.first << ";";
202 }
203 LDEBUG << "LinguisticResourcesPrivate::includeResources added module with the following groups: " << oss.str();
204#endif
205 // recursive inclusions
206 includeResources(module, newMod);
207 }
208 catch(NoSuchModule& )
209 {
211 LERROR << "Cannot find module " << moduleName << " in file " << fileName;
212 }
213 catch(exception& e)
214 {
216 LERROR << "Error trying to find module " << moduleName << " in file " << fileName << ":" << e.what();
217 }
218 }
219 }
220 catch(NoSuchList& ) { } // do nothing: optional
221 catch(NoSuchGroup& ) { } // do nothing: optional
222}
223
224
226{
227#ifdef DEBUG_LP
229 LDEBUG << "LinguisticResources::getModuleConfiguration" << this << lang;
230#endif
231 auto it = m_d->m_resourcesManagers.find(lang);
232 if (it == m_d->m_resourcesManagers.end())
233 {
235 LERROR << "no Resources module configuration for language "<< (uint64_t)lang;
236 throw InvalidConfiguration();
237 }
238 return (*it).second->getModuleConfigurationStructure();
239}
240
241
242} // LinguisticProcessing
243} // Lima
#define LIMA_PATH_SEPARATOR
Definition FileUtils.h:25
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
Definition LimaCommon.h:332
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define RESOURCESLOGINIT
const FsaStringsPool & stringsPool(MediaId med) const
const std::string & getConfigPath() const
std::string & getParamValueAtKeyOfGroupNamed(const std::string &key, const std::string &name)
std::deque< std::string > & getListValuesAtKeyOfGroupNamed(const std::string &key, const std::string &name)
return a message when a 'group' was not found
return a message when a 'module' is not found
return a message when a 'param' was not found
Parser class for the lima's xml configuration files.
ModuleConfigurationStructure & getModuleConfiguration(const std::string &moduleName)
void registerMainKeys(std::shared_ptr< Common::AbstractAccessByString > mainKeys)
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
std::map< MediaId, std::shared_ptr< AbstractResource::Manager > > m_resourcesManagers
void includeResources(Common::XMLConfigurationFiles::ModuleConfigurationStructure &module, Common::XMLConfigurationFiles::ModuleConfigurationStructure &includeModule)
singleton that holds all languages resources
std::shared_ptr< AbstractResource > getResource(MediaId lang, const std::string &id) const
Common::XMLConfigurationFiles::ModuleConfigurationStructure & getModuleConfiguration(MediaId lang)
void initLanguage(MediaId lang, Common::XMLConfigurationFiles::ModuleConfigurationStructure &confModule, bool registerMainKeysInStringPool)
Use this exception to signal that a media is used which has not been initialized.
Definition LimaCommon.h:369
Defines access method for the singleton pattern.
Definition Singleton.h:22
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
originally detectModuleConfigurationStructure.h in detectlibraries
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing