LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
DesagglutinationResources.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
22
23#include <iostream>
24#include <fstream>
25
26using namespace std;
27using namespace Lima::Common::MediaticData;
28
29
30namespace Lima
31{
32namespace LinguisticProcessing
33{
34namespace MorphologicAnalysis
35{
36namespace Desagglutination
37{
38
40
43 Manager* manager)
44
45{
47 m_language = manager->getInitializationParameters().language;
48 string resourcesPath=MediaticData::single().getResourcesPath();
49 try
50 {
51 QString file = Common::Misc::findFileInPaths(resourcesPath.c_str(), unitConfiguration.getParamsValueAtKey("categoriesMappingFile").c_str());
52 loadMicroCategoriesMappingFromFile(file.toUtf8().constData());
53 }
55 {
56 LERROR << "no param 'categoriesMappingFile' in DesagglutinationResources group for language " << (int)m_language;
58 }
59 try
60 {
61 QString file=Common::Misc::findFileInPaths(resourcesPath.c_str(), unitConfiguration.getParamsValueAtKey("delimiterFile").c_str());
62 loadDelimitersFromFile(file.toUtf8().constData());
63 }
65 {
66 LERROR << "no param 'delimiterFile' in DesagglutinationResources group for language " << (int)m_language;
68 }
69}
70
71
72void DesagglutinationResources::loadMicroCategoriesMappingFromFile(const std::string& microcategoriesMappingFile)
73{
75
76 LINFO << "load micro categories mapping from file " << microcategoriesMappingFile;
77 const Common::PropertyCode::PropertyManager& microManager=
78 static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyManager("MICRO");
79
80 std::ifstream fin(microcategoriesMappingFile.data(), std::ifstream::binary);
81 if (!fin)
82 {
83 LERROR << "can't open file : " << microcategoriesMappingFile;
84 throw std::runtime_error("can't open file for desagglutinator microcategories");
85 }
86 while (!fin.eof())
87 {
88 std::string line;
89 std::getline(fin,line);
90 if (line.size()>0)
91 {
92 size_t currentIndex,nextStop;
93 nextStop=line.find(" ");
94 if (nextStop==std::string::npos)
95 {
96 LERROR << "invalid desagglutinator microcategories file";
97 throw std::runtime_error("invalid desagglutinator microcategories file");
98 }
99 uint64_t cat= microManager.getPropertyValue(line.substr(0,nextStop));
100 CompoundCategories compoundCats;
101 currentIndex=nextStop+1;
102 nextStop=line.find(" ",currentIndex);
103 if (nextStop==std::string::npos)
104 {
105 LERROR << "invalid desagglutinator microcategories file";
106 throw std::runtime_error("invalid desagglutinator microcategories file");
107 }
108 compoundCats.begin=microManager.getPropertyValue(line.substr(currentIndex,nextStop-currentIndex));
109 currentIndex=nextStop+1;
110 nextStop=line.find(" ",currentIndex);
111 if (nextStop==std::string::npos)
112 {
113 LERROR << "invalid desagglutinator microcategories file";
114 throw std::runtime_error("invalid desagglutinator microcategories file");
115 }
116 compoundCats.part=microManager.getPropertyValue(line.substr(currentIndex,nextStop-currentIndex));
117 currentIndex=nextStop+1;
118 compoundCats.end=microManager.getPropertyValue(line.substr(currentIndex,line.size()-currentIndex).data());
119
120 LDEBUG << "read CategorieMapping : " << cat << " => " << compoundCats.begin << " - " << compoundCats.part << " - " << compoundCats.end;
121 m_categoriesMapping.insert(std::make_pair(LinguisticCode(cat),compoundCats));
122 }
123 }
124
125 fin.close();
126
127 LINFO << "loaded " << m_categoriesMapping.size() << " microcategory mappings";
128}
129
130void DesagglutinationResources::loadDelimitersFromFile(const std::string& delimitersFile)
131{
132
134 LINFO << "load delimiters from file : " << delimitersFile;
135
136 std::ifstream ifs (delimitersFile.c_str(), std::ifstream::binary);
137
138 while(ifs.good()&&!ifs.eof())
139 {
141 m_delimiters.insert(del);
142 LINFO << "read delimiter >" << Common::Misc::limastring2utf8stdstring(del) << "<";
143 }
144
145 ifs.close();
146}
147
148
149
150} // Desagglutination
151} // MorphologicAnalysis
152} // LinguisticProcessing
153} // Lima
Implementation of desagglutination resources.
#define DESAGGLUTINATIONRESOURCES_CLASSID
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define DESAGGLOGINIT
Defines a Factory to create Object of type Base.
Holds linguistic data for one language.
const MediaData & mediaData(MediaId media) const
Provide tools to manage a specific property.
LinguisticCode getPropertyValue(const std::string &symbolicValue) const
Get the coded property value from the symbolic value.
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 init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager)
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
std::string readLine(std::istream &inputFile)
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.
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< AbstractResource, DesagglutinationResources > desagglutinationResourcesFactory(DESAGGLUTINATIONRESOURCES_CLASSID)
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.