LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
bowToken.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 * @file BoWToken.cpp
8 * @author Besancon Romaric
9 * @date Tue Oct 7 2003
10 * copyright Copyright (C) 2003 by CEA LIST
11 *
12 ***********************************************************************/
13
14
15#include "bowToken.h"
16#include "bowTokenPrivate.h"
17#include "bowNamedEntity.h"
22
23#include "bowTerm.h" // for the generic
24#include "bowNamedEntity.h" // input function
25
26#include <iterator> // for debug output
27#include <algorithm> // function (on refMap)
28
29#include <string>
30
31#include <wchar.h>
32
33using namespace std;
34
35namespace Lima
36{
37namespace Common
38{
39namespace BagOfWords
40{
41
42uint8_t toInt(const BoWType& bt)
43{
44 if (bt == BoWType::BOW_NOTYPE) return 0;
45 else if (bt == BoWType::BOW_TOKEN) return 1;
46 else if (bt == BoWType::BOW_TERM) return 2;
47 else if (bt == BoWType::BOW_NAMEDENTITY) return 3;
48 else if (bt == BoWType::BOW_PREDICATE) return 4;
49 else return std::numeric_limits<uint8_t>::max();
50}
51
52
53#define DEFAULT_SEPARATOR QChar{'#'}
54#define DEFAULT_COMPOUND_SEPARATOR QChar{'_'}
55
56//***********************************************************************
57// static members initialization
58//***********************************************************************
61bool BoWTokenPrivate::m_useOnlyLemma=true; //TODO mettre false quand indexation compound terminee
62
64 m_lemma(),
65 m_inflectedForm(),
66 m_position(0),
67 m_length(0),
68 m_vertex(0)
69{
70// BOWLOGINIT;
71// LDEBUG << "Creating BoWToken : " << (*this) << " - " << this;
72}
73
75 const LinguisticCode category,
76 const uint64_t position,
77 const uint64_t length):
78 m_lemma(lemma),
79 m_inflectedForm(),
80 m_category(category),
81 m_position(position),
82 m_length(length),
83 m_vertex(0)
84{
86// BOWLOGINIT;
87// LDEBUG << "Creating BoWToken : " << (*this) << " - " << this;
88}
89
91 m_lemma(tok.m_d->m_lemma),
92 m_inflectedForm(tok.m_d->m_inflectedForm),
93 m_category(tok.m_d->m_category),
94 m_position(tok.m_d->m_position),
95 m_length(tok.m_d->m_length),
96 m_vertex(tok.m_d->m_vertex)
97{
98// BOWLOGINIT;
99// LDEBUG << "Copying BoWToken : " << tok << " - " << &tok << " ; new one is: " << this;
100}
101
103 const uint64_t position,
104 const uint64_t length):
105 m_inflectedForm(),
106 m_position(position),
107 m_length(length),
108 m_vertex(0)
109{
110 //uint64_t i(str.find(m_separator));
111 // uint64_t i(str.find(m_separator));
112 int i(str.indexOf(m_separator));
113 if (i == -1)
114 {
115 m_lemma=str;
117 }
118 else
119 {
120 m_lemma=str.mid(0,i);
121 m_category=LinguisticCode::fromString(str.mid(i+1).toStdString());
122 }
123
124// BOWLOGINIT;
125// LDEBUG << "Creating BoWToken : " << (*this) << " - " << this;
126}
127
129 m_lemma(tok.m_lemma),
130 m_inflectedForm(tok.m_inflectedForm),
131 m_category(tok.m_category),
132 m_position(tok.m_position),
133 m_length(tok.m_length),
134 m_vertex(tok.m_vertex)
135{
136}
137
139{
140 if (&tok != this)
141 {
142 m_lemma = tok.m_lemma;
146 m_length = tok.m_length;
147 m_vertex = tok.m_vertex;
148 }
149 return *this;
150}
151
153{
154 return (
155 m_lemma == tok.m_lemma &&
157 m_category == tok.m_category &&
158 m_position == tok.m_position &&
159 m_length == tok.m_length &&
160 m_vertex == tok.m_vertex
161 );
162}
163
164//***********************************************************************
165// destructor
166//***********************************************************************
169
170//***********************************************************************
171// constructors
172//***********************************************************************
174 m_d(&d)
175{
176#ifdef DEBUG_LP
178 LDEBUG << "BoWToken::BoWToken(BoWTokenPrivate): " << (*this) << " - " << this;
179#endif
180}
181
183 m_d(new BoWTokenPrivate())
184{
185#ifdef DEBUG_LP
187 LDEBUG << "BoWToken::BoWToken()" << this;
188#endif
189}
190
191BoWToken::BoWToken(const LimaString& lemma,
192 const LinguisticCode category,
193 const uint64_t position,
194 const uint64_t length):
195 m_d(new BoWTokenPrivate(lemma, category, position, length))
196{
198#ifdef DEBUG_LP
200 LDEBUG << "Creating BoWToken : " << (*this) << " - " << this;
201#endif
202}
203
205 m_d(new BoWTokenPrivate(tok))
206{
207#ifdef DEBUG_LP
209 LDEBUG << "BoWToken::BoWToken(BoWToken) : " << tok << " - " << &tok << " ; new one is: " << this;
210#endif
211}
212
214{
215#ifdef DEBUG_LP
217 LDEBUG << "BoWToken::operator=(BoWToken) : " << tok << " - " << &tok << " ; new one is: " << this;
218#endif
219 *m_d = *tok.m_d;
220 return *this;
221}
222
224{
225 return( *m_d == *tok.m_d);
226}
227
229 const uint64_t position,
230 const uint64_t length):
231 m_d(new BoWTokenPrivate(str, position, length))
232{
233#ifdef DEBUG_LP
235 LDEBUG << "BoWToken::BoWToken" << this << str << position << length;
236#endif
237 //uint64_t i(str.find(m_separator));
238 // uint64_t i(str.find(m_separator));
239 int i(str.indexOf(m_d->m_separator));
240 if (i == -1)
241 {
242 m_d->m_lemma=str;
244 }
245 else
246 {
247 m_d->m_lemma=str.left(i);
248 m_d->m_category=LinguisticCode::fromString(str.mid(i+1).toStdString());
249 }
250
251// BOWLOGINIT;
252// LDEBUG << "Creating BoWToken : " << (*this) << " - " << this;
253}
254
256{
257#ifdef DEBUG_LP
259 LDEBUG << "BoWToken::clone " << this;
260#endif
261 return new BoWToken(*(new BoWTokenPrivate(static_cast<BoWTokenPrivate&>(*(this->m_d)))));
262}
263
264// BoWToken* BoWToken::clone(const std::map<BoWToken*,BoWToken*>& /*unused refmap*/) const
265// {
266 // return new BoWToken(*this);
267 // }
268
269
270
271//***********************************************************************
272// destructor
273//***********************************************************************
275{
276 delete m_d;
277}
278
279
280//***********************************************************************
281// member functions
282//***********************************************************************
286uint64_t BoWToken::getPosition(void) const {return m_d->m_position;};
287uint64_t BoWToken::getLength(void) const {return m_d->m_length;};
288
290uint64_t BoWToken::size(void) const { return 1; }
291
292void BoWToken::setLemma(const Lima::LimaString& lemma) {m_d->m_lemma = lemma;};
294 m_d->m_inflectedForm = inflectedForm;};
296void BoWToken::setPosition(const uint64_t pos){m_d->m_position = pos;};
297void BoWToken::setLength(const uint64_t len) {m_d->m_length = len;};
298
300
301uint64_t BoWToken::getVertex() const {return m_d->m_vertex;}
302void BoWToken::setVertex(uint64_t vertex) {m_d->m_vertex = vertex;}
303
306
307
316
325
326void BoWToken::addToPosition(const uint64_t offset) {
327 m_d->m_position+=offset;
328}
329
330
331
333{
334//#ifdef DEBUG_LP
335// BOWLOGINIT;
336//#endif
337
338 if (m_d->m_useOnlyLemma)
339 {
340// #ifdef DEBUG_LP
341// LDEBUG << "BoWToken::getString: m_d->m_useOnlyLemma is 'true'";
342// LDEBUG << "BoWToken::getString: getLemma()=" << getLemma();
343// #endif
344 return getLemma();
345 }
346 else
347 {
348 ostringstream cat;
349 cat << m_d->m_category.toString();
350//#ifdef DEBUG_LP
351// LDEBUG << "BoWToken::getString: m_d->m_useOnlyLemma is 'false'";
352//#endif
353/* ostringstream len;
354 len << m_length;
355 return m_lemma + m_separator + LimaString(cat.str()) + m_separator + LimaString(len.str());*/
356//#ifdef DEBUG_LP
357// LDEBUG << "BoWToken::getString: getLemma()=" << getLemma() << ", cat=" << Misc::utf8stdstring2limastring(cat.str() );
358//#endif
360 }
361}
362
363
364// convert the spaces in lemma string to a non-space character
365// (problems in indexing)
367{
368 int current(0);
369 int i=m_lemma.indexOf(Misc::utf8stdstring2limastring(" "),current);
370 while (i != -1)
371 {
373 current=i+1;
374 i=m_lemma.indexOf(Misc::utf8stdstring2limastring(" "),current);
375 }
376}
377
378//***********************************************************************
379// get a list of (position,length) couples identifying the parts
380// of the tokens
381//***********************************************************************
383{
384 return Common::Misc::PositionLengthList(1, make_pair(
385 Common::Misc::Position(m_d->m_position),
386 Common::Misc::Length(m_d->m_length)));
387}
388
389//***********************************************************************
390// == operator
391// added by Benoit Mathieu
392//***********************************************************************
393
394bool BoWToken::operator==(const BoWToken& t) const
395{
397 {
398 const BoWNamedEntity* n1=dynamic_cast<const BoWNamedEntity*>(this);
399 const BoWNamedEntity* n2=dynamic_cast<const BoWNamedEntity*>(&t);
400 if (n1->getNamedEntityType()!=n2->getNamedEntityType()) return false;
401 }
402 return ( (getType()==t.getType()) &&
403 (m_d->m_category==t.m_d->m_category) &&
404 (m_d->m_lemma==t.m_d->m_lemma));
405}
406
407bool BoWToken::operator!=(const BoWToken& t) const
408{
409 return !(*this==t);
410}
411
412
413//***********************************************************************
414// output
415//***********************************************************************
416std::ostream& operator << (std::ostream& os, const BoWToken& tok)
417{
418 os << "(" << Misc::limastring2utf8stdstring(tok.getLemma()) << "-"
419 << tok.m_d->m_category.toString() << "-" << tok.m_d->m_position << "-" << tok.m_d->m_length << ")";
420 return os;
421}
422
423QDebug& operator << (QDebug& os, const BoWToken& tok)
424{
425 os << "(" << tok.getLemma() << "-"
426 << tok.m_d->m_category.toString() << "-" << tok.m_d->m_position << "-" << tok.m_d->m_length << ")";
427 return os;
428}
429
431{
432 std::ostringstream oss;
433 oss << "(" << Misc::limastring2utf8stdstring(getLemma()) << "-";
434 if (macroManager==0)
435 {
436 oss << m_d->m_category.toString();
437 }
438 else
439 {
440 oss << macroManager->getPropertySymbolicValue(m_d->m_category);
441 }
442 oss << "-" << m_d->m_position << ")";
443 return oss.str();
444}
445
446std::string BoWToken::getIdUTF8String() const
447{
448 std::ostringstream oss;
449 oss << "(" << Misc::limastring2utf8stdstring(m_d->m_lemma) << "-"
450 << m_d->m_category.toString() << "-" << m_d->m_position << ")";
451 return oss.str();
452}
453
454std::set< uint64_t > BoWToken::getVertices() const
455{
456 std::set< uint64_t > result;
457 result.insert(getVertex());
458 return result;
459}
460
461
462} // namespace BagOfWords
463} // namespace Common
464} // namespace Lima
#define DEFAULT_SEPARATOR
#define DEFAULT_COMPOUND_SEPARATOR
#define LDEBUG
Definition LimaCommon.h:157
#define BOWLOGINIT
Definition LimaCommon.h:201
#define L_NONE
Definition StdBitset.h:338
This is a complex token used to represent a named entity token.
Common::MediaticData::EntityType getNamedEntityType(void) const
BoWTokenPrivate & operator=(const BoWTokenPrivate &tok)
Definition bowToken.cpp:138
bool operator==(const BoWTokenPrivate &tok)
Definition bowToken.cpp:152
void convertSpaces(void)
convert the spaces in the lemma string into a non-space character (defined by the m_compoundSeparator...
Definition bowToken.cpp:366
This class contains the representation of an element of the bag of words.
Definition bowToken.h:46
virtual uint64_t size(void) const override
size of the BoWToken is the number of parts in the token
Definition bowToken.cpp:290
void setVertex(uint64_t vertex)
Definition bowToken.cpp:302
static LimaChar getInternalSeparator()
Definition bowToken.cpp:317
void setCategory(LinguisticCode c)
Definition bowToken.cpp:295
virtual std::string getIdUTF8String() const override
function used to identify a bowtoken in a text (used in BowDumper)
Definition bowToken.cpp:446
static LimaChar getCompoundSeparator()
Definition bowToken.cpp:321
LinguisticCode getCategory(void) const
Definition bowToken.cpp:285
void setPosition(const uint64_t pos) override
Definition bowToken.cpp:296
static void setInternalSeparator(const LimaChar c)
Definition bowToken.cpp:308
virtual std::set< uint64_t > getVertices() const override
returns a singleton made of the value of the getVertex function.
Definition bowToken.cpp:454
void setLength(const uint64_t len) override
Definition bowToken.cpp:297
void setLemma(const Lima::LimaString &lemma)
Definition bowToken.cpp:292
bool operator==(const BoWToken &) const
redefine equality
Definition bowToken.cpp:394
BoWToken & operator=(const BoWToken &tok)
Definition bowToken.cpp:213
static void setUseOnlyLemma(const bool b)
Definition bowToken.cpp:304
uint64_t getPosition(void) const override
Definition bowToken.cpp:286
bool operator!=(const BoWToken &) const
redefine inequality as negation of equality.
Definition bowToken.cpp:407
uint64_t getLength(void) const override
Definition bowToken.cpp:287
virtual std::string getOutputUTF8String(const Common::PropertyCode::PropertyManager *macroManager=0) const override
get a string of the BoWToken for output function
Definition bowToken.cpp:430
virtual Lima::LimaString getInflectedForm(void) const
Definition bowToken.cpp:284
virtual void addToPosition(const uint64_t offset)
add the offset to the position of the token
Definition bowToken.cpp:326
virtual BoWToken * clone() const override
Definition bowToken.cpp:255
virtual Common::Misc::PositionLengthList getPositionLengthList() const
get the list of (position,length) couples of the elements of the token (if several)
Definition bowToken.cpp:382
virtual Lima::LimaString getString(void) const override
Definition bowToken.cpp:332
static void setCompoundSeparator(const LimaChar c)
Definition bowToken.cpp:312
virtual Lima::LimaString getLemma(void) const
Definition bowToken.cpp:283
void setInflectedForm(const Lima::LimaString &inflectedForm)
Definition bowToken.cpp:293
virtual BoWType getType() const override
Definition bowToken.cpp:299
Provide tools to manage a specific property.
const std::string & getPropertySymbolicValue(const LinguisticCode &value) const
The coded property value can hold several property data.
std::string toString() const
Definition StdBitset.h:152
static LinguisticCode fromString(const std::string &str)
Definition StdBitset.h:89
LIMA_BOW_EXPORT uint8_t toInt(const BoWType &bt)
Definition bowToken.cpp:42
BoWType
enum to characterize the type of the AbstractBoWElement
@ BOW_NOTYPE
the AbstractBoWElement is an abstract one that should not be instanciated
@ BOW_TERM
the AbstractBoWElement is a multi-term
@ BOW_NAMEDENTITY
the AbstractBoWElement is a named entity
@ BOW_TOKEN
the AbstractBoWElement is a simple token
@ BOW_PREDICATE
the AbstractBoWElement is a predicate (n-ary relation, template or semantic frame
T & operator<<(T &qd, const BoWType &bt)
std::vector< std::pair< Position, Length > > PositionLengthList
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)
NAUTITIA.
QChar LimaChar
Definition LimaString.h:30
QString LimaString
Definition LimaString.h:33
STL namespace.