LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SelectionalPreferences.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
23
24#include <fstream>
25
26using namespace Lima::Common::PropertyCode;
27
28namespace Lima
29{
30namespace LinguisticProcessing
31{
32namespace SyntacticAnalysis
33{
34
36
37
42
44
45
48 Manager* manager)
49
50{
52 LDEBUG << "Creating a SelectionalPreferences (loads file)";
53 m_language=manager->getInitializationParameters().language;
54
55 try {
57 std::string preferencesFileName = Common::Misc::findFileInPaths(resourcePath.c_str(), unitConfiguration.getParamsValueAtKey("file").c_str()).toUtf8().constData();
58 loadFromFile(preferencesFileName);
59
61 {
62 LERROR << "no parameter 'file' in SelectionalPreferences group for language " << (int) m_language << " !";
64 }
65
66// display();
67}
68
69
70void SelectionalPreferences::loadFromFile(const std::string& fileName)
71{
73 LDEBUG << "Loading selectional preferences from " << fileName;
74 std::ifstream ifl(fileName.c_str(), std::ifstream::binary);
75 size_t linesCounter = 0;
76
77 const PropertyManager& macroManager = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getPropertyCodeManager().getPropertyManager("MACRO");
78 if (!ifl)
79 {
80 LWARN << "Selectional preferences definition file not found: " << fileName;
81 return;
82 }
83
84 std::string line = Lima::Common::Misc::readLine(ifl);
86 linesCounter++;
87 while (ifl.good() && !ifl.eof())
88 {
89 if ( (line != "") && (line[0] != '#') )
90 {
91 LDEBUG << "read line : " << line;
92
93 size_t index = 0;
94
95 size_t next = line.find_first_of(";", index);
96 if (next == std::string::npos)
97 {
98 LERROR << "Cannot find first delimiter in line: " << line;
100 }
101 std::string target = line.substr(0, next);
102
103 index = next+1;
104 next=line.find_first_of(";",index);
105 if (next == std::string::npos)
106 {
107 LERROR << "Cannot find second delimiter in line: " << line;
108 throw InvalidConfiguration();
109 }
110 LinguisticCode targetMacro = macroManager.getPropertyValue(line.substr(index, next-index));
111
112 index = next+1;
113 next=line.find_first_of(";",index);
114 if (next == std::string::npos)
115 {
116 LERROR << "Cannot find third delimiter in line: " << line;
117 throw InvalidConfiguration();
118 }
119 std::string dependency = line.substr(index, next-index);
120
121 index = next+1;
122 next=line.find_first_of(";",index);
123 if (next == std::string::npos)
124 {
125 LERROR << "Cannot find fourth delimiter in line: " << line;
126 throw InvalidConfiguration();
127 }
128 std::string source = line.substr(index, next-index);
129
130 index = next+1;
131 next=line.find_first_of(";",index);
132 if (next == std::string::npos)
133 {
134 LERROR << "Cannot find fifth delimiter in line: " << line;
135 throw InvalidConfiguration();
136 }
137 LinguisticCode soureceMacro = macroManager.getPropertyValue(line.substr(index, next-index));
138
139 index = next+1;
140 double probability = 0.0;
141 std::string probabilityString = line.substr(index);
142 std::istringstream probabilityStringIss(probabilityString);
143 probabilityStringIss >> probability;
144
145 boost::tuple< std::string, LinguisticCode, std::string, std::string, LinguisticCode > tuple(target,targetMacro,dependency,source,soureceMacro);
146 m_preferences.insert(std::make_pair(tuple, probability));
147 }
150 linesCounter++;
151 }
152}
153
154double SelectionalPreferences::dependencyProbability(std::string target, LinguisticCode targetCode, std::string dependency, std::string source, LinguisticCode sourceCode) const
155{
156 SALOGINIT;
157 boost::tuple< std::string, LinguisticCode, std::string, std::string, LinguisticCode > tuple = boost::make_tuple(target, targetCode, dependency, source, sourceCode);
158 std::map< boost::tuple< std::string, LinguisticCode, std::string, std::string, LinguisticCode >, double, compareTuple >::const_iterator preferencesIt = m_preferences.find(tuple);
159 if (preferencesIt == m_preferences.end())
160 {
161 return 0.0;
162 }
163 LDEBUG << "Probability of (" << target << ", " << targetCode << ", " << dependency << ", " << source << ", " << sourceCode << ") is " << (*preferencesIt).second;
164 return (*preferencesIt).second;
165}
166
167bool SelectionalPreferences::compareTuple::operator()(
168 const boost::tuple< std::string, LinguisticCode, std::string, std::string, LinguisticCode >& t1,
169 const boost::tuple< std::string, LinguisticCode, std::string, std::string, LinguisticCode >& t2) const
170{
171 return (t1.get<0>() < t2.get<0>()
172 || ( (t1.get<0>() == t2.get<0>()) && (t1.get<1>() < t2.get<1>()))
173 || ( (t1.get<0>() == t2.get<0>()) && (t1.get<1>() == t2.get<1>()) && (t1.get<2>() < t2.get<2>()))
174 || ( (t1.get<0>() == t2.get<0>()) && (t1.get<1>() == t2.get<1>()) && (t1.get<2>() == t2.get<2>()) && (t1.get<3>() < t2.get<3>()))
175 || ( (t1.get<0>() == t2.get<0>()) && (t1.get<1>() == t2.get<1>()) && (t1.get<2>() == t2.get<2>()) && (t1.get<3>() == t2.get<3>()) && (t1.get<4>() < t2.get<4>()))
176 );
177}
178
179} // namespace SyntacticAnalysis
180} // namespace LinguisticProcessing
181} // namespace Lima
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define SALOGINIT
#define SELECTIONALPREFERENCES_CLASSID
Defines a Factory to create Object of type Base.
Holds linguistic data for one language.
const std::string & getResourcesPath() const
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
double dependencyProbability(std::string target, LinguisticCode targetCode, std::string dependency, std::string source, LinguisticCode sourceCode) const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
void chomp(std::basic_string< C > &str)
std::string readLine(std::istream &inputFile)
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
SimpleFactory< AbstractResource, SelectionalPreferences > SelectionalPreferencesFactory(SELECTIONALPREFERENCES_CLASSID)
NAUTITIA.