25#include <QRegularExpression>
38namespace LinguisticProcessing
56 m_language = manager->getInitializationParameters().language;
58 string resourcesPath=MediaticData::single().getResourcesPath();
66 LERROR <<
"no param 'trigramFile' in TrigramMatrix group for language " << (int)m_language;
74 LINFO <<
"Loading trigrams matrix file: " << fileName;
78 ifl.open(fileName.c_str(), std::ifstream::binary);
82 LERROR <<
"can't read trigrams from file " << fileName;
86 QRegularExpression linere(
"^(.+)\t(.+)\t(.+)\t(\\d+(\\.\\d+)?)$");
87 QRegularExpressionMatch what;
88 QRegularExpression numre(
"^\\d+$");
93 while (ifl.good() && !ifl.eof())
105 for (
int count = 1; count <= 3; ++count)
107 QString elem = what.captured(count);
109 auto numreMatch = numre.match(elem);
110 if (numreMatch.hasMatch())
120 QString sproba = what.captured(4);
122 proba = sproba.toDouble(&ok);
126 LERROR <<
"Invalid trigram line " <<
linenum <<
" in: " << fileName;
127 throw(std::runtime_error(
129 "invalid trigram line: %1").arg(
linenum).toUtf8().constData()));
135 LERROR <<
"Invalid trigram line " <<
linenum <<
" in: " << fileName;
136 throw(std::runtime_error(QString::fromUtf8(
"invalid trigram line: %1").arg(
linenum).toUtf8().constData()));
140 m_trigrams[trigram[0]][trigram[1]][trigram[2]] = proba;
149 map< LinguisticCode, map<LinguisticCode, map<LinguisticCode,float> > >::const_iterator it1=m_trigrams.find(cat1);
150 if (it1==m_trigrams.end())
return false;
151 map<LinguisticCode, map<LinguisticCode,float> >::const_iterator it2=it1->second.find(cat2);
152 if (it2==it1->second.end())
return false;
153 return (it2->second.find(cat3)!=it2->second.end());
158 map< LinguisticCode, map<LinguisticCode, map<LinguisticCode,float> > >::const_iterator it1=m_trigrams.find(cat1);
159 if (it1==m_trigrams.end())
return 0;
160 map<LinguisticCode, map<LinguisticCode,float> >::const_iterator it2=it1->second.find(cat2);
161 if (it2==it1->second.end())
return 0;
162 map<LinguisticCode, float>::const_iterator it3=it2->second.find(cat3);
163 if (it3==it2->second.end())
return 0;
179 m_language = manager->getInitializationParameters().language;
181 string resourcesPath=MediaticData::single().getResourcesPath();
189 LERROR <<
"no param 'bigramFile' in BigramMatrix group for language " << (int)m_language;
197 LINFO <<
"Loading bigrams matrix file: " << fileName;
201 ifl.open(fileName.c_str(), std::ifstream::binary);
205 LERROR <<
"can't read trigrams from file " << fileName;
209 QRegularExpression linere(
"^(.+)\t(.+)\t(\\d+(\\.\\d+)?)$");
210 QRegularExpressionMatch what;
211 QRegularExpression numre(
"^\\d+$");
216 while (ifl.good() && !ifl.eof())
228 for (
int count = 1; count <= 2; ++count)
230 QString elem = what.captured(count);
232 auto numreMatch = numre.match(elem);
233 if (numreMatch.hasMatch())
243 QString sproba = what.captured(3);
245 proba = sproba.toDouble(&ok);
249 LERROR <<
"Invalid bigram line " <<
linenum <<
" in: " << fileName;
250 throw(std::runtime_error(
252 "invalid bigram line: %1").arg(
linenum).toUtf8().constData()));
258 LERROR <<
"Invalid bigram line " <<
linenum <<
" in: " << fileName;
259 throw(std::runtime_error(QString::fromUtf8(
"invalid bigram line: %1").arg(
linenum).toUtf8().constData()));
263 m_bigrams[bigram[0]][bigram[1]] = proba;
272 map<LinguisticCode, map<LinguisticCode,float> >::const_iterator it1=m_bigrams.find(cat1);
273 if (it1==m_bigrams.end())
return false;
274 return (it1->second.find(cat2)!=it1->second.end());
279 map<LinguisticCode, map<LinguisticCode,float> >::const_iterator it1=m_bigrams.find(cat1);
280 if (it1==m_bigrams.end())
return 0;
281 map<LinguisticCode,float>::const_iterator it2=it1->second.find(cat2);
282 if (it2==it1->second.end())
return 0;
Defines a Factory to create Object of type Base.
int main(int argc, char *argv lineString[]) boost std::string
LinguisticCode getPropertyValue(const std::string &symbolicValue) const
Get the coded property value from the symbolic value.
std::string & getParamsValueAtKey(const std::string &key)
return a message when a 'param' was not found
Use this exception to signal an error in one of the configuration files.
static LinguisticCode fromString(const std::string &str)
void readBigramMatrixFile(const std::string &fileName)
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, AbstractResource::Manager *manager) override
initialize with parameters from configuration file.
float freq(LinguisticCode cat1, LinguisticCode cat2) const
bool exists(LinguisticCode cat1, LinguisticCode cat2) const
void readTrigramMatrixFile(const std::string &fileName)
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, AbstractResource::Manager *manager) override
initialize with parameters from configuration file.
float freq(LinguisticCode cat1, LinguisticCode cat2, LinguisticCode cat3) const
bool exists(LinguisticCode cat1, LinguisticCode cat2, LinguisticCode cat3) const
static const MediaticData & single()
const singleton accessor
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, BigramMatrix > bigramMatrixFactory(BIGRAMMATRIX_CLASSID)
SimpleFactory< AbstractResource, TrigramMatrix > trigramMatrixFactory(TRIGRAMMATRIX_CLASSID)
Defines Bigrams and Trigrams matrices *.
#define BIGRAMMATRIX_CLASSID
#define TRIGRAMMATRIX_CLASSID
launch exception related to the configuration file parsing