LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EntityFeatures.h
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.h
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 * Project s2lp - Automaton
14 *
15 * @brief generic features associated to a recognized entity :
16 * normalized information attached to the entity
17 *
18 *
19 ***********************************************************************/
20
21#ifndef ENTITYFEATURES_H
22#define ENTITYFEATURES_H
23
24#include "AutomatonExport.h"
26#include "boost/any.hpp"
28
29#include <vector>
30#include <iostream>
31
32namespace Lima {
33namespace LinguisticProcessing {
34namespace Automaton {
35
36#define DEFAULT_ATTRIBUTE "value"
37#define UNDEFLENGTH std::numeric_limits<uint64_t>::max()
38#define UNDEFPOSITION std::numeric_limits<uint64_t>::max()
39
45public:
47 template<typename ValueType>
48 EntityFeature(const std::string& name,
49 const ValueType& value):
50 m_name(name),
51 m_value(boost::any(value))
52 {}
54
55 bool operator==(const EntityFeature& f) const;
56 // comparison operator on name only
57 bool operator==(const std::string& name) const;
58
59 void setName(const std::string& name) { m_name=name; }
60 void setValue(const boost::any& val) { m_value=val; }
61
62 const std::string& getName() const { return m_name; }
63 const boost::any& getValue() const { return m_value; }
64 boost::any getValue() { return m_value; }
65
66 void setPosition(const uint64_t& pos) { m_pos=pos; }
67 void setLength(const uint64_t& val) { m_len=val; }
68 uint64_t getPosition() const { return m_pos; }
69 uint64_t getLength() const { return m_len; }
70
71 // these functions test value type using RTTI: this is not really
72 // efficient and should be avoided unless necessary (i.e. if the
73 // type of the feature is known)
74 std::string getValueString() const;
75 LimaString getValueLimaString() const;
76
77private:
78 std::string m_name;
79 boost::any m_value;
80 uint64_t m_pos;
81 uint64_t m_len;
82};
83
88class LIMA_AUTOMATON_EXPORT EntityFeatures: public std::vector<EntityFeature> {
89public:
92 bool operator==(const EntityFeatures& f) const;
93
94 template<typename ValueType>
95 void setFeature(const std::string& name,
96 const ValueType& value);
97
98 template<typename ValueType>
99 void addFeature(const std::string& name,
100 const ValueType& value);
101
102
103 template<typename ValueType>
104 void appendFeature(const std::string& name,
105 const ValueType& value);
106
107
108 EntityFeatures::const_iterator find(const std::string& featureName) const;
109 EntityFeatures::iterator find(const std::string& featureName);
110 EntityFeatures::const_iterator findLast(const std::string& featureName) const;
111 EntityFeatures::iterator findLast(const std::string& featureName);
112
113 friend LIMA_AUTOMATON_EXPORT std::ostream& operator<<(std::ostream& os, const EntityFeatures& f);
114 friend LIMA_AUTOMATON_EXPORT QDebug& operator<<(QDebug& os, const EntityFeatures& f);
115};
116
117#if defined(WIN32)
118 extern template LIMA_AUTOMATON_EXPORT void EntityFeatures::appendFeature<int>(const std::string&, const int& );
119 extern template LIMA_AUTOMATON_EXPORT void EntityFeatures::appendFeature<double>(const std::string&, const double& );
120 extern template LIMA_AUTOMATON_EXPORT void EntityFeatures::appendFeature<QString>(const std::string&, const QString& );
121#endif
122
123template<typename ValueType>
124 void EntityFeatures::setFeature(const std::string& name,
125 const ValueType& value)
126 {
127// SELOGINIT;
128// LDEBUG << "EntityFeatures::<ValueType>setFeature(" << name << "," << value << ")";
129 // if feature with same name already exists, overwrite it
130 EntityFeatures::iterator it=find(name);
131 if (it!=end()) {
132// if( (it!=end()) && (name==DEFAULT_ATTRIBUTE) ){
133 (*it).setValue(boost::any(value));
134 }
135 else {
136 //push empy feature and set values to avoid two copies
137 //of value (do not know the type: it may be a big class)
138 push_back(EntityFeature());
139 back().setName(name);
140 back().setValue(boost::any(value));
141 }
142 }
143template<typename ValueType>
144 void EntityFeatures::addFeature(const std::string& name,
145 const ValueType& value)
146 {
147// SELOGINIT;
148// LDEBUG << "EntityFeatures::<ValueType>addFeature(" << name << "," << value << ")";
149 push_back(EntityFeature());
150 back().setName(name);
151 back().setValue(boost::any(value));
152 }
153/*
154template<typename ValueType>
155 void EntityFeatures::appendFeature(const std::string& name,
156 const ValueType& value)
157 {
158 SELOGINIT;
159 // if feature with same name already exists, append to it
160 LDEBUG << "EntityFeatures::<ValueType>appendFeature(" << name << "," << value << ")";
161 // if feature with same name already exists, but type is neither
162 // int neitheer float, nor string, overwrite it
163 EntityFeatures::iterator it=findLast(name);
164 if (it!=end()) {
165 (*it).setValue(boost::any(value));
166 }
167 else {
168 //push empy feature and set values to avoid two copies
169 //of value (do not know the type: it may be a big class)
170 push_back(EntityFeature());
171 back().setName(name);
172 back().setValue(boost::any(value));
173 }
174 }
175*/
176
177
178} // end namespace
179} // end namespace
180} // end namespace
181
182#endif
#define LIMA_AUTOMATON_EXPORT
QDebug & operator<<(QDebug &qd, const std::string &str)
Definition QsLog.cpp:39
a generic feature that is defined by a name and a value, which can be of any type (use boost::any typ...
EntityFeature(const std::string &name, const ValueType &value)
a list of generic features: each feature is unique (only one feature for a name)
void setFeature(const std::string &name, const ValueType &value)
void addFeature(const std::string &name, const ValueType &value)
void appendFeature(const std::string &name, const ValueType &value)
EntityFeatures::const_iterator find(const std::string &featureName) const
bool operator==(const Constraint &c1, const Constraint &c2)
NAUTITIA.
QString LimaString
Definition LimaString.h:33