LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
LangDetectorCore.cpp
Go to the documentation of this file.
1
//
2
// Created by tderouet on 24/05/22.
3
//
4
5
#include "
LangDetectorCore.h
"
6
7
namespace
Lima::LinguisticProcessing::LDetector
{
8
class
LangDetectorCorePrivate
{
9
public
:
10
fasttext::FastText
ftext
;
11
};
12
13
LangDetectorCore::LangDetectorCore
():m_d(new
LangDetectorCorePrivate
()){
14
15
}
16
17
LangDetectorCore::~LangDetectorCore
() {
18
delete
m_d;
19
}
20
21
/*
22
* Detect the language of the text.
23
*/
24
25
std::string
LangDetectorCore::detectLang
(
const
std::string& text)
const
{
26
std::string lang;
27
std::vector<std::pair<fasttext::real, std::string>> prediction;
28
std::stringstream ioss(text + std::string(
"\n"
));
29
m_d->
ftext
.predictLine(ioss, prediction, 1, 0);
30
lang = prediction.front().second;
31
return
lang;
32
}
33
/*
34
* Remove the "__label__" part of the language label.
35
*/
36
std::string
LangDetectorCore::labelToPrintable
(
const
std:: string &label) {
37
return
label.substr(9, std::string::npos);
38
}
39
40
void
LangDetectorCore::loadModel
(
const
std::string& modelPath) {
41
m_d->
ftext
.loadModel(modelPath);
42
}
43
}
LangDetectorCore.h
Lima::LinguisticProcessing::LDetector::LangDetectorCorePrivate
Definition
LangDetectorCore.cpp:8
Lima::LinguisticProcessing::LDetector::LangDetectorCorePrivate::ftext
fasttext::FastText ftext
Definition
LangDetectorCore.cpp:10
Lima::LinguisticProcessing::LDetector::LangDetectorCore::labelToPrintable
static std::string labelToPrintable(const std::string &label)
Definition
LangDetectorCore.cpp:36
Lima::LinguisticProcessing::LDetector::LangDetectorCore::detectLang
std::string detectLang(const std::string &text) const
Definition
LangDetectorCore.cpp:25
Lima::LinguisticProcessing::LDetector::LangDetectorCore::loadModel
void loadModel(const std::string &modelPath)
Definition
LangDetectorCore.cpp:40
Lima::LinguisticProcessing::LDetector::LangDetectorCore::LangDetectorCore
LangDetectorCore()
Definition
LangDetectorCore.cpp:13
Lima::LinguisticProcessing::LDetector::LangDetectorCore::~LangDetectorCore
virtual ~LangDetectorCore()
Definition
LangDetectorCore.cpp:17
Lima::LinguisticProcessing::LDetector
Definition
LangDetector.cpp:16
lima_linguisticprocessing
src
linguisticProcessing
core
LangDetector
LangDetectorCore.cpp
Generated by
1.9.8