LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EntityFeatures.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 EntityFeatures.cpp
9 * @author Besancon Romaric (romaric.besancon@cea.fr)
10 * @date Mon Jun 12 2006
11 * @version $Id$
12 * copyright Copyright (C) 2006-2012 by CEA LIST
13 *
14 ***********************************************************************/
15
16#include "EntityFeatures.h"
18
19#include <QtCore/QDate>
20#include <QtCore/QTime>
21
23
24using namespace std;
25using namespace Lima::Common::Misc;
26
27namespace Lima {
28namespace LinguisticProcessing {
29namespace Automaton {
30
31//***********************************************************************
32// EntityFeature functions
33
35m_name(""),
36m_value(),
37m_pos(UNDEFPOSITION),
38m_len(UNDEFLENGTH)
39{}
40
43
44bool EntityFeature::operator==(const std::string& name) const
45{
46 return (m_name==name);
47}
48
50{
51 if (m_name!=f.m_name) {
52 return false;
53 }
54 // have to check types
55 const type_info& type=m_value.type();
56 const type_info& typeOther=f.m_value.type();
57 if (type!=typeOther) {
58 return false;
59 }
60
61 if (type==typeid(string)) {
62 return (boost::any_cast<string>(m_value)==boost::any_cast<string>(f.m_value));
63 }
64 if (type==typeid(LimaString)) {
65 return (boost::any_cast<LimaString>(m_value)==boost::any_cast<LimaString>(f.m_value));
66 }
67 if (type==typeid(double)) {
68 return (boost::any_cast<double>(m_value)==boost::any_cast<double>(f.m_value));
69 }
70 if (type==typeid(int)) {
71 return (boost::any_cast<int>(m_value)==boost::any_cast<int>(f.m_value));
72 }
73 if (type==typeid(QDate)) {
74 return (boost::any_cast<QDate>(m_value)==boost::any_cast<QDate>(f.m_value));
75 }
76 if (type==typeid(QTime)) {
77 return (boost::any_cast<QTime>(m_value)==boost::any_cast<QTime>(f.m_value));
78 }
79 // other types not handled
80 return false;
81}
82
83std::string EntityFeature::getValueString() const {
84 std::ostringstream oss;
85 const type_info& type=m_value.type();
86 if (type==typeid(std::string)) { // no need to convert
87 return boost::any_cast<std::string>(m_value);
88 }
89 if (type==typeid(LimaString)) {
90 return limastring2utf8stdstring(boost::any_cast<LimaString>(m_value));
91 }
92 if (type==typeid(double)) {
93 std::ostringstream oss;
94 oss << boost::any_cast<double>(m_value);
95 return oss.str();
96 }
97 if (type==typeid(int)) {
98 std::ostringstream oss;
99 oss << boost::any_cast<int>(m_value);
100 return oss.str();
101 }
102 if (type==typeid(QDate)) {
103 //format YYYY-MM-DD
104 return (boost::any_cast<QDate>(m_value)).toString(Qt::ISODate).toUtf8().data();
105 }
106 if (type==typeid(QTime)) {
107 // format YYYY-MM-DDTHH:MM:SS,fffffffff
108 return (boost::any_cast<QTime>(m_value)).toString(Qt::ISODate).toUtf8().data();
109 }
110 return "unknown_type";
111}
112
114 std::ostringstream oss;
115 const type_info& type=m_value.type();
116 if (type==typeid(LimaString)) {
117 return boost::any_cast<LimaString>(m_value);
118 }
120}
121
122//***********************************************************************
123// EntityFeatures functions
124
128
131
133{
134 if (size()!=f.size()) {
135 return false;
136 }
137 for (EntityFeatures::const_iterator it1=begin(),it1_end=end(),it2=f.begin();
138 it1!=it1_end; it1++,it2++) {
139 if (! (*it1 == *it2) ) {
140 return false;
141 }
142 }
143 return true;
144}
145
146template<>
147 void EntityFeatures::appendFeature(const std::string& name,
148 const QString& value)
149 {
150 SELOGINIT;
151 // if feature with same name already exists, append to it
152 LDEBUG << "EntityFeatures<QString>::appendFeature(" << name << "," << value << ")";
153 EntityFeatures::iterator it=findLast(name);
154 if (it!=end()) {
155 QString previous = boost::any_cast<QString>(it->getValue());
156 LDEBUG << "EntityFeatures::appendFeature: previous =" << previous;
157 previous.append(" ").append(value);
158 (*it).setValue(boost::any(previous));
159 LDEBUG << "EntityFeatures::appendFeature: now =" << previous;
160 }
161 else {
162 //push empy feature and set values to avoid two copies
163 //of value (do not know the type: it may be a big class)
164 push_back(EntityFeature());
165 back().setName(name);
166 back().setValue(boost::any(value));
167 }
168 }
169
170template<>
171 void EntityFeatures::appendFeature(const std::string& name,
172 const int& value)
173 {
174 SELOGINIT;
175 LDEBUG << "EntityFeatures<int>::appendFeature(" << name << "," << value << ")";
176 // if feature with same name already exists, append to it
177 EntityFeatures::iterator it=findLast(name);
178 if (it!=end()) {
179 int previous = boost::any_cast<int>(*it);
180 previous += value;
181 (*it).setValue(boost::any(previous));
182 }
183 else {
184 //push empy feature and set values to avoid two copies
185 //of value (do not know the type: it may be a big class)
186 push_back(EntityFeature());
187 back().setName(name);
188 back().setValue(boost::any(value));
189 }
190 }
191
192template<>
193 void EntityFeatures::appendFeature(const std::string& name,
194 const double& value)
195 {
196 SELOGINIT;
197 LDEBUG << "EntityFeatures<double>::appendFeature(" << name << "," << value << ")";
198 // if feature with same name already exists, append to it
199 EntityFeatures::iterator it=findLast(name);
200 if (it!=end()) {
201 double previous = boost::any_cast<double>(*it);
202 previous += value;
203 (*it).setValue(boost::any(previous));
204 }
205 else {
206 //push empy feature and set values to avoid two copies
207 //of value (do not know the type: it may be a big class)
208 push_back(EntityFeature());
209 back().setName(name);
210 back().setValue(boost::any(value));
211 }
212 }
213
214EntityFeatures::const_iterator EntityFeatures::
215find(const std::string& featureName) const
216{
217 return std::find(begin(),end(),featureName);
218}
219
220EntityFeatures::iterator EntityFeatures::
221find(const std::string& featureName)
222{
223 return std::find(begin(),end(),featureName);
224}
225
226EntityFeatures::const_iterator EntityFeatures::
227findLast(const std::string& featureName) const
228{
229 std::vector<std::string> pattern(1,featureName);
230 return std::find_end(begin(),end(),pattern.begin(),pattern.end());
231}
232
233EntityFeatures::iterator EntityFeatures::
234findLast(const std::string& featureName)
235{
236 std::vector<std::string> pattern(1,featureName);
237 return std::find_end(begin(),end(),pattern.begin(),pattern.end());
238}
239
241std::ostream& operator<<(std::ostream& os, const EntityFeatures& f) {
242 if (f.empty()) {
243 return os;
244 }
245 EntityFeatures::const_iterator it=f.begin(),it_end=f.end();
246 os << (*it).getName() << "=" << (*it).getValueString();
247 for (it++; it!=it_end; it++) {
248 os << "/" << (*it).getName() << "=" << (*it).getValueString();
249 }
250 return os;
251}
252
253QDebug& operator<<(QDebug& os, const EntityFeatures& f) {
254 if (f.empty()) {
255 return os;
256 }
257 EntityFeatures::const_iterator it=f.begin(),it_end=f.end();
258 os << (*it).getName() << "=" << (*it).getValueString();
259 for (it++; it!=it_end; it++) {
260 os << "/" << (*it).getName() << "=" << (*it).getValueString();
261 }
262 return os;
263}
264
265
266} // end namespace
267} // end namespace
268} // end namespace
#define UNDEFLENGTH
#define UNDEFPOSITION
#define LDEBUG
Definition LimaCommon.h:157
#define SELOGINIT
a generic feature that is defined by a name and a value, which can be of any type (use boost::any typ...
a list of generic features: each feature is unique (only one feature for a name)
EntityFeatures::const_iterator findLast(const std::string &featureName) const
void appendFeature(const std::string &name, const ValueType &value)
EntityFeatures::const_iterator find(const std::string &featureName) const
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
LimaString utf8stdstring2limastring(const std::string &src)
std::ostream & operator<<(std::ostream &os, const DFFSPos &x)
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.