LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
bowComplexToken.cpp
Go to the documentation of this file.
1// Copyright 2002-2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 * @file bowComplexToken.cpp
8 * @author Besancon Romaric
9 * @date Tue Oct 7 2003
10 * copyright Copyright (C) 2003 by CEA LIST
11 *
12 ***********************************************************************/
13
14#include "bowComplexToken.h"
15#include "bowTokenPrivate.h"
17
18#include "BoWRelation.h"
21#include <iostream>
22#include <fstream>
23#include <sstream>
24#include <stdexcept>
25
26namespace Lima {
27namespace Common {
28namespace BagOfWords {
29
32 m_parts(0),
33 m_head(0)
34{
35}
36
38 BoWTokenPrivate(bctp),
39 m_parts(),
40 m_head(bctp.m_head)
41{
42 for (std::deque<BoWComplexToken::Part>::const_iterator i=bctp.m_parts.begin(); i!=bctp.m_parts.end(); i++) {
43 // with the second parameter set to false, ensure that the part token will be cloned
44 addPart((*i).getBoWRelation(),(*i).getBoWToken());
45 }
46 if (m_head>m_parts.size()) {
47 m_head=0;
48 }
49}
50
52 const LinguisticCode category,
53 const uint64_t position,
54 const uint64_t length):
55 BoWTokenPrivate(lemma, category, position, length),
56 m_parts(0),
57 m_head(0)
58{
59}
60
62 const LinguisticCode category,
63 const uint64_t position,
64 const uint64_t length,
65 std::deque< boost::shared_ptr< BoWToken > >& parts,
66 const uint64_t head):
67BoWTokenPrivate(lemma, category, position, length),
68m_parts(0),
69m_head(head)
70{
71 for (auto i = parts.begin(); i != parts.end(); i++) {
72 addPart(boost::shared_ptr< BoWRelation >(),*i);
73 }
74 if (m_head>m_parts.size()) {
75 m_head=0;
76 }
77}
78
81m_parts(0),
82m_head(0)
83{
84 copy(t);
85}
86
87
91
92
94
99
101 const LinguisticCode category,
102 const uint64_t position,
103 const uint64_t length):
105{
106 m_d->m_lemma = lemma;
107 m_d->m_category = category;
108 m_d->m_position = position;
109 m_d->m_length = length;
110}
111
113 const LinguisticCode category,
114 const uint64_t position,
115 const uint64_t length,
116 std::deque< boost::shared_ptr< BoWToken > >& parts,
117 const uint64_t head):
118 BoWToken(*new BoWComplexTokenPrivate())
119{
120 m_d->m_lemma = lemma;
121 m_d->m_category = category;
122 m_d->m_position = position;
123 m_d->m_length = length;
124 static_cast<BoWComplexTokenPrivate *>(m_d)->m_head = head;
125 for (auto i = parts.begin(); i != parts.end(); i++)
126 {
127 addPart(boost::shared_ptr< BoWRelation >(), *i);
128 }
129 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_head > static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size())
130 {
131 static_cast<BoWComplexTokenPrivate *>(m_d)->m_head=0;
132 }
133}
134
135
139
141{
142 clear();
143// delete m_d;
144}
145
147 if (this != &t) {
148 clear();
150 static_cast<BoWComplexTokenPrivate *>(m_d)->copy(t);
151 }
152 return *this;
153}
154
158
159//**********************************************************************
160// helper functions for constructors, destructor and assignment operator
161//**********************************************************************
162// Careful : I create new objects for tokens that exist
163// elsewhere in the list, and put the isInList member to false
164// (safer solution than keeping the pointer)
166 m_parts.clear();
167 m_head = static_cast<BoWComplexTokenPrivate *>(t.m_d)->m_head;
171 m_lemma = t.m_d->m_lemma;
172 m_length = t.m_d->m_length;
176 m_vertex = t.m_d->m_vertex;
177 for (std::deque<BoWComplexToken::Part>::const_iterator i(t.getParts().begin());
178 i != t.getParts().end(); i++) {
179 boost::shared_ptr< BoWRelation > rel = (*i).get<0>();
180 m_parts.push_back(BoWComplexToken::Part(rel,(*i).getBoWToken()));
181 }
182}
183
184//**************************************************************
185// functions
186//**************************************************************
187std::deque<BoWComplexToken::Part>& BoWComplexToken::getParts(void)
188{
189 return static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts;
190}
191
192const std::deque<BoWComplexToken::Part>&
194{
195 return static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts;
196}
197
198std::deque<BoWComplexToken::Part>::iterator
200{
201 return static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin();
202}
203
204const std::deque<BoWComplexToken::Part>::const_iterator
206{
207 return static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin();
208}
209
211{
212 return static_cast<BoWComplexTokenPrivate *>(m_d)->m_head;
213}
214
215void BoWComplexToken::setHead(const uint64_t i) {
216 static_cast<BoWComplexTokenPrivate *>(m_d)->m_head=i;
217}
218
219boost::shared_ptr< BoWToken > BoWComplexTokenPrivate::addPart(boost::shared_ptr< BoWRelation > rel,
220 boost::shared_ptr< BoWToken > tok,
221 bool isHead)
222{
223 m_parts.push_back(BoWComplexToken::Part(rel, tok));
224 if (isHead) {
225 m_head=m_parts.size()-1;
226 }
227 uint64_t previousPosition = m_position;
228
229 // added the first part
230 if (m_position == 0 && m_length==0)
231 {
232 m_position = tok->getPosition();
233 m_length = tok->getLength();
234 }
235 // adding a part before the previous first part
236 else if (tok->getPosition() < m_position)
237 {
238 m_position = tok->getPosition();
239 m_length = previousPosition - tok->getPosition() + m_length;
240 }
241 // adding a part after the current end
242 else if (tok->getPosition() > (previousPosition + m_length))
243 {
244 m_length = tok->getPosition() - previousPosition + tok->getLength();
245 }
246 return tok;
247}
248
249
250boost::shared_ptr< BoWToken > BoWComplexToken::addPart(boost::shared_ptr< BoWToken > tok,
251 bool isHead)
252{
253 return addPart(boost::shared_ptr< BoWRelation >(), tok, isHead);
254}
255
256
257boost::shared_ptr< BoWToken > BoWComplexToken::addPart(boost::shared_ptr< BoWRelation > rel,
258 boost::shared_ptr< BoWToken > tok,
259 bool isHead)
260{
261 return static_cast<BoWComplexTokenPrivate *>(m_d)->addPart(rel,tok,isHead);
262}
263
264// BoWToken* BoWComplexToken::addPart(const BoWToken* tok,
265// bool isHead)
266// {
267// return addPart(0, const_cast<BoWToken*>(tok), false, isHead);
268// }
269//
270//
271// BoWToken* BoWComplexToken::addPart(const BoWRelation* rel,
272// const BoWToken* tok,
273// bool isHead)
274// {
275// return static_cast<BoWComplexTokenPrivate *>(m_d)->addPart(const_cast<BoWRelation*>(rel),const_cast<BoWToken*>(tok),false,isHead);
276// }
277
278
279
281{
282// for (std::deque<Part>::iterator i(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin());
283// i != static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.end(); i++)
284// {
285// if (! (*i).isInList())
286// {
287// if ((*i).getBoWToken() != 0)
288// {
289// delete ((*i).getBoWRelation());
290// delete ((*i).getBoWToken());
291// (*i).get<0>()=0;
292// (*i).get<1>()=0;
293// }
294// }
295// }
296 static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.clear();
297}
298
300{
301 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size() ==0) {
302 return m_d->m_lemma;
303 }
304 LimaString lemma;
305 lemma = static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[static_cast<BoWComplexTokenPrivate *>(m_d)->m_head].get<1>()->getLemma();
306
307 for (uint64_t i=0; i < static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size(); i++)
308 {
309 if (i != static_cast<BoWComplexTokenPrivate *>(m_d)->m_head)
310 {
311 lemma += BoWToken::getCompoundSeparator() + static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].get<1>()->getLemma();
312 }
313 }
314 return lemma;
315}
316
318{
319 LimaString infl;
320 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size() ==0) {
321 return infl;
322 }
323 infl = static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[static_cast<BoWComplexTokenPrivate *>(m_d)->m_head].get<1>()->getInflectedForm();
324
325 for (uint64_t i=0; i < static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size(); i++)
326 {
327 if (i != static_cast<BoWComplexTokenPrivate *>(m_d)->m_head)
328 {
329 infl += BoWToken::getCompoundSeparator() + static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].get<1>()->getInflectedForm();
330 }
331 }
332 return infl;
333}
334
335
336 //**********************************************************************
337uint64_t BoWComplexToken::size(void) const {
338 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size() ==0) {
339 return 1;
340 }
341 uint64_t nbParts=0;
342 for (std::deque<BoWComplexToken::Part>::const_iterator
343 part=static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin(); part!=static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.end(); part++) {
344 nbParts+=(*part).getBoWToken()->size();
345 }
346 return nbParts;
347}
348
349//**********************************************************************
350// get the list of (position,length) of the parts
351//**********************************************************************
354 for (std::deque<Part>::const_iterator i(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin());
355 i != static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.end(); i++) {
356 Common::Misc::PositionLengthList partposlen=(*i).getBoWToken()->getPositionLengthList();
357 poslenlist.insert(poslenlist.end(),partposlen.begin(),partposlen.end());
358 }
359 return poslenlist;
360}
361
362
363//**********************************************************************
364// add the offset to the position of the token and recursively to each
365// of its parts
366//**********************************************************************
367void BoWComplexToken::addToPosition(const uint64_t offset) {
368 m_d->m_position+=offset;
369 for (std::deque<Part>::const_iterator part(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.begin());
370 part != static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.end(); part++) {
371 (*part).getBoWToken()->addToPosition(offset);
372 }
373}
374
375//**********************************************************************
376// output (for debug)
377// useful function for output of derived classes
378//**********************************************************************
380
381{
382 std::ostringstream oss;
383 oss << "[";
384 for (uint64_t i(0); i<static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size(); i++) {
385 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_head == i) {
386 oss << "*";
387 }
388 //oss << m_parts[i].getBoWToken() << ":" << *(m_parts[i].getBoWToken());
389 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWRelation()!=0) {
390 oss << static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWRelation()->getOutputUTF8String();
391 }
392 oss << static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWToken()->getOutputUTF8String(macroManager);
393 }
394 oss << "]";
395 return oss.str();
396}
397
398//**********************************************************************
399// output (for debug)
400// useful function for output of derived classes
401//**********************************************************************
403{
404 std::ostringstream oss;
405 oss << "[";
406 for (uint64_t i(0); i<static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size(); i++) {
407 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_head == i) {
408 oss << "*";
409 }
410 //oss << m_parts[i].getBoWToken() << ":" << *(m_parts[i].getBoWToken());
411 if (static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWRelation()!=0) {
412 oss << *(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWRelation());
413 }
414 oss << *(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].getBoWToken());
415 }
416 oss << "]";
417 return oss.str();
418}
419
420std::set< uint64_t > BoWComplexToken::getVertices() const
421{
422 std::set< uint64_t > result;
423 for (uint64_t i(0); i<static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts.size(); i++)
424 {
425 if (boost::dynamic_pointer_cast< BoWComplexToken >(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].get<1>()) != 0)
426 {
427 std::set< uint64_t > partResult = boost::dynamic_pointer_cast< BoWComplexToken >(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].get<1>())->getVertices();
428 result.insert(partResult.begin(), partResult.end());
429 }
430 else
431 {
432 result.insert(static_cast<BoWComplexTokenPrivate *>(m_d)->m_parts[i].get<1>()->getVertex());
433 }
434 }
435 return result;
436}
437
438} // namespace BagOfWords
439} // namespace Common
440} // namespace Lima
boost::shared_ptr< BoWToken > addPart(boost::shared_ptr< BoWRelation > rel, boost::shared_ptr< BoWToken > tok, const bool isHead=false)
std::deque< BoWComplexToken::Part > m_parts
the list of the parts of the complex token
uint64_t m_head
the index of the head of the complex token in its part list
This class represents a part of a ComplexToken : it is composed of a pointer on the actual part (a Bo...
This is a complex token for an index.
virtual Lima::LimaString getLemma(void) const override
virtual Lima::LimaString getInflectedForm(void) const override
bool operator==(const BoWComplexToken &)
virtual Common::Misc::PositionLengthList getPositionLengthList() const override
get the list of (position,length) couples of the elements of the token (if several)
uint64_t getHead() const
add a part in the list of parts of the complex token.
uint64_t size(void) const override
size is the number of parts in the token
void addToPosition(const uint64_t offset) override
add the offset to the positions of the token and of each of its parts (recusrively)
boost::shared_ptr< BoWToken > addPart(boost::shared_ptr< BoWToken > tok, const bool isHead=false)
add a part in the list of parts of the complex token.
BoWComplexToken & operator=(const BoWComplexToken &)
std::deque< Part >::iterator getPartsIterator(void)
std::string getUTF8StringParts(const Common::PropertyCode::PropertyManager *macroManager=0) const
virtual std::set< uint64_t > getVertices() const override
returns the vertices of all its parts
This class contains the representation of an element of the bag of words.
Definition bowToken.h:46
static LimaChar getCompoundSeparator()
Definition bowToken.cpp:321
bool operator==(const BoWToken &) const
redefine equality
Definition bowToken.cpp:394
BoWToken & operator=(const BoWToken &tok)
Definition bowToken.cpp:213
Provide tools to manage a specific property.
std::vector< std::pair< Position, Length > > PositionLengthList
NAUTITIA.
QString LimaString
Definition LimaString.h:33