LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
BoWFeatureExtractor.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
6/************************************************************************
7 *
8 * @file BoWFeatures.cpp
9 * @author Romaric Besancon (romaric.besancon@cea.fr)
10 * @date Tue Feb 12 2013
11 * copyright Copyright (C) 2011 by CEA LIST
12 *
13 ***********************************************************************/
14
15#include "BoWFeatureExtractor.h"
20#include <sstream>
21
22using namespace std;
23using namespace boost;
24using namespace Lima::Common;
25using namespace Lima::Common::BagOfWords;
26
27namespace Lima {
28namespace LinguisticProcessing {
29
30//***********************************************************************
31// factories for feature extractors
32AbstractBoWFeatureExtractor::AbstractBoWFeatureExtractor(MediaId language, const std::string& /*complement*/):
33m_language(language)
34{
35}
36
42
49
50//***********************************************************************
51// BoWFeature list
52//***********************************************************************
54m_language(0)
55{
56}
57
59m_language(language)
60{
61}
62
64{
65 for (BoWFeatures::iterator it=begin(),it_end=end(); it!=it_end; it++) {
66 if (*it) {
67 delete (*it);
68 *it=0;
69 }
70 }
71}
72
73void BoWFeatures::initialize(const deque<std::string>& featureNames)
74{
76 for (deque<std::string>::const_iterator it=featureNames.begin(),
77 it_end=featureNames.end();it!=it_end;it++)
78 {
79 string featureName=(*it);
80 string complement;
81 string::size_type i=featureName.find(":");
82 if (i!=string::npos) {
83 complement=string(featureName,i+1);
84 featureName=string(featureName,0,i);
85 }
86 try {
87 LDEBUG << "BoWFeatures: initialize feature" << featureName;
88 push_back(BoWFeatureLemmaFactory.getFactory(featureName)->create(m_language,complement));
89 back()->setName(featureName);
90 }
91 catch (LimaException& e) {
92 LERROR << "BoWFeatures: feature" << featureName << "is not defined";
93 }
94 }
95}
96
97//***********************************************************************
98// BoWFeature accessors
99//***********************************************************************
100BoWFeaturePosition::BoWFeaturePosition(MediaId language, const std::string& complement):
101AbstractBoWFeatureExtractor(language,complement)
102{}
103
105getValue(const AbstractBoWElement* token) const
106{
107 ostringstream oss;
108 if (dynamic_cast<const BoWToken*>(token) != 0)
109 oss << dynamic_cast<const BoWToken*>(token)->getPosition();
110 return oss.str();
111}
112
113//***********************************************************************
114BoWFeatureToken::BoWFeatureToken(MediaId language, const std::string& complement):
115AbstractBoWFeatureExtractor(language,complement)
116{}
117
119getValue(const AbstractBoWElement* token) const
120{
121 if (dynamic_cast<const BoWToken*>(token) != 0)
122 return Common::Misc::limastring2utf8stdstring(dynamic_cast<const BoWToken*>(token)->getInflectedForm());
123 else
124 return token->getIdUTF8String();
125}
126
127//***********************************************************************
128BoWFeatureInflectedForm::BoWFeatureInflectedForm(MediaId language, const std::string& complement):
129AbstractBoWFeatureExtractor(language,complement)
130{}
131
133getValue(const AbstractBoWElement* token) const
134{
135 // return the same as word (in WordFeatureExtractor, return the inflected form
136 // from the dictionary, as opposed to the form in the text, but this difference is not stored
137 // in the BoWToken)
138 if (dynamic_cast<const BoWToken*>(token) != 0)
139 return Common::Misc::limastring2utf8stdstring(dynamic_cast<const BoWToken*>(token)->getInflectedForm());
140 else
141 return token->getIdUTF8String();
142
143}
144
145//***********************************************************************
146BoWFeatureLemma::BoWFeatureLemma(MediaId language, const std::string& complement):
147AbstractBoWFeatureExtractor(language,complement)
148{
149}
150
152getValue(const AbstractBoWElement* token) const
153{
154 if (dynamic_cast<const BoWToken*>(token) != 0)
155 return Common::Misc::limastring2utf8stdstring(dynamic_cast<const BoWToken*>(token)->getLemma());
156 else
157 return token->getIdUTF8String();
158}
159
160//***********************************************************************
161BoWFeatureProperty::BoWFeatureProperty(MediaId language,const std::string& complement):
163m_propertyName(complement),
164m_propertyAccessor(0),
165m_propertyManager(0)
166{
169 // for BoW, only property available is MACRO
170 m_propertyAccessor=&codeManager.getPropertyAccessor("MACRO");
171 m_propertyManager=&codeManager.getPropertyManager("MACRO");
172}
173
175getValue(const AbstractBoWElement* token) const
176{
177 ostringstream oss;
178 if (dynamic_cast<const BoWToken*>(token) != 0)
179 oss << dynamic_cast<const BoWToken*>(token)->getCategory();
180 return oss.str();
181}
182
183//***********************************************************************
184BoWFeatureTstatus::BoWFeatureTstatus(MediaId language, const std::string& complement):
185AbstractBoWFeatureExtractor(language,complement)
186{}
187
189getValue(const AbstractBoWElement* token) const
190{
191 LIMA_UNUSED(token);
192 return "";
193}
194
195} // end namespace
196} // end namespace
#define BoWFeatureToken_ID
#define BoWFeatureProperty_ID
#define BoWFeaturePosition_ID
#define BoWFeatureTstatus_ID
#define BoWFeatureLemma_ID
#define BoWFeatureInflectedForm_ID
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define DUMPERLOGINIT
This class is the abstract base class of all elements that can be stored in a BoWText.
virtual std::string getIdUTF8String() const =0
function used to identify a bowtoken in a text (used in BowDumper)
This class contains the representation of an element of the bag of words.
Definition bowToken.h:46
Holds linguistic data for one language.
const MediaData & mediaData(MediaId media) const
Provide tools to parse a property file, and deal with the property coding system.
const PropertyManager & getPropertyManager(const std::string &propertyName) const
Get the PropertyManager associated to a property.
const PropertyAccessor & getPropertyAccessor(const std::string &propertyName) const
Get the PropertyAccessor associated to a property.
The main LIMA exception class.
Definition LimaCommon.h:262
AbstractBoWFeatureExtractor(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
BoWFeatureInflectedForm(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
BoWFeatureLemma(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
BoWFeaturePosition(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
BoWFeatureProperty(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
BoWFeatureToken(MediaId language, const std::string &complement="")
BoWFeatureTstatus(MediaId language, const std::string &complement="")
std::string getValue(const Common::BagOfWords::AbstractBoWElement *token) const override
void initialize(const std::deque< std::string > &featureNames)
Defines a Factory that automatically register to a MainFactory singleton.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
BoWFeatureExtractorFactory< BoWFeatureProperty > BoWFeaturePropertyFactory(BoWFeatureProperty_ID)
BoWFeatureExtractorFactory< BoWFeatureInflectedForm > BoWFeatureInflectedormFactory(BoWFeatureInflectedForm_ID)
BoWFeatureExtractorFactory< BoWFeatureTstatus > BoWFeatureTstatusFactory(BoWFeatureTstatus_ID)
BoWFeatureExtractorFactory< BoWFeatureLemma > BoWFeatureLemmaFactory(BoWFeatureLemma_ID)
BoWFeatureExtractorFactory< BoWFeaturePosition > BoWFeaturePositionFactory(BoWFeaturePosition_ID)
BoWFeatureExtractorFactory< BoWFeatureToken > BoWFeatureTokenFactory(BoWFeatureToken_ID)
NAUTITIA.
STL namespace.