LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
ltrText.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
16#include "ltrText.h"
17
19
20
21using namespace Lima::Common::Misc;
22
23
24namespace Lima {
25namespace Common {
26namespace BagOfWords {
27
28
34// -----------------------------------------------------------------------------
35// -- copying
36// -----------------------------------------------------------------------------
37
39std::vector<boost::shared_ptr<LTR_Token>>()
40{
41 this->reserve(text.size());
42 for (LTR_Text::const_iterator itTok = text.begin();
43 itTok != text.end(); ++ itTok) {
44 this->push_back( *itTok );
45 }
46 for (SENTENCE_BOUNDS_T::const_iterator itSentBd = text.m_sentenceBounds.begin();
47 itSentBd != text.m_sentenceBounds.end(); ++ itSentBd) {
48 m_sentenceBounds.push_back(*itSentBd);
49 }
50}
51
52
53// -----------------------------------------------------------------------------
54// -- adding
55// -----------------------------------------------------------------------------
56
57void LTR_Text::addToken(boost::shared_ptr<LTR_Token>& token) {
58 this->push_back(token);
59}
60
61void LTR_Text::addSentenceBound(const uint64_t& sb) {
62 m_sentenceBounds.push_back(sb);
63}
64
66
67 LTR_Text::size_type origSize = this->size();
68 // copy of tokens
69 this->reserve(origSize + ltr.size());
70 for (LTR_Text::const_iterator itTok = ltr.begin();
71 itTok != ltr.end(); ++ itTok) {
72 this->push_back( *itTok );
73 }
74 // add sentence bounds
76 itSentBd != ltr.endSentenceBounds(); ++ itSentBd) {
77 m_sentenceBounds.push_back(*itSentBd + origSize);
78 }
79}
80
81
82// -----------------------------------------------------------------------------
83// -- reading/writing
84// -----------------------------------------------------------------------------
85
86void LTR_Text::binaryWriteOn(std::ostream& os) const {
87
88 uint64_t tokenCounter = 0;
89 writeCodedInt(os, this->size());
90 if (this->size()==0) {
91 return;
92 }
93 SENTENCE_BOUNDS_T:: const_iterator itSb = m_sentenceBounds.begin();
94 writeCodedInt(os, *itSb);
95 for (LTR_Text::const_iterator itTok = this->begin();
96 itTok != this->end(); itTok ++) {
97 (*itTok)->binaryWriteOn(os);
98 tokenCounter ++;
99 if (tokenCounter == *itSb) {
100 itSb ++;
101 if (itSb != m_sentenceBounds.end()) {
102 writeCodedInt(os, *itSb);
103 }
104 }
105 }
106 // dump named entities
107//std::cerr << "-- number of NE: " << m_namedEntities.size() << std::endl;
108/* writeCodedInt(os, m_namedEntities.size());
109 for (NAMED_ENTITIES_T::const_iterator itNe = m_namedEntities.begin();
110 itNe != m_namedEntities.end(); itNe ++) {
111 itNe->first->write(os);
112 writeCodedInt(os, itNe->second);
113 }*/
114}
115
116void LTR_Text::binaryReadFrom(std::istream& is) {
117
118 uint64_t tokenNb = readCodedInt(is);
119 uint64_t tokenCounter = 1;
120 while (tokenCounter <= tokenNb) {
121 uint64_t sentenceBound = readCodedInt(is);
122 m_sentenceBounds.push_back(sentenceBound);
123 for (; tokenCounter <= sentenceBound; tokenCounter ++) {
124 boost::shared_ptr<LTR_Token> tok( new LTR_Token() );
125 tok->binaryReadFrom(is);
126 this->push_back(tok);
127 }
128 }
129}
130
131
132// -----------------------------------------------------------------------------
133// -- printing
134// -----------------------------------------------------------------------------
135
136std::ostream& operator<<(std::ostream& os, const LTR_Text& text) {
137
138 uint64_t tokenCounter = 1;
139 LTR_Text::SENTENCE_BOUNDS_T::const_iterator itSb = text.m_sentenceBounds.begin();
140 for (LTR_Text::const_iterator itTok = text.begin();
141 itTok != text.end(); itTok ++) {
142 if (*itTok==nullptr) { os << "NULL" << std::endl; }
143 else if ((*itTok)->size()==0) { os << "EMPTY" << std::endl; }
144 else { os << **itTok << std::endl; }
145
146 if (itSb != text.m_sentenceBounds.end()) {
147 if (tokenCounter == *itSb) {
148 os << "==SB==========" << std::endl;
149 itSb ++;
150 }
151 }
152 tokenCounter ++;
153 }
154 return os;
155}
156
157QDebug& operator<<(QDebug& os, const LTR_Text& text) {
158
159 uint64_t tokenCounter = 1;
160 LTR_Text::SENTENCE_BOUNDS_T::const_iterator itSb = text.m_sentenceBounds.begin();
161 for (LTR_Text::const_iterator itTok = text.begin();
162 itTok != text.end(); itTok ++) {
163 if (*itTok==nullptr) { os << "NULL"; }
164 else if ((*itTok)->size()==0) { os << "EMPTY"; }
165 else { os << **itTok; }
166
167 if (itSb != text.m_sentenceBounds.end()) {
168 if (tokenCounter == *itSb) {
169 os << "==SB==========";
170 itSb ++;
171 }
172 }
173 tokenCounter ++;
174 }
175 return os;
176}
177
178} // BagOfWords
179} // Common
180} // Lima
a linear text representation
Definition ltrText.h:41
SENTS_CONST_ITER_T endSentenceBounds() const
Definition ltrText.h:92
SENTS_CONST_ITER_T beginSentenceBounds() const
Definition ltrText.h:89
void addSentenceBound(const uint64_t &sb)
Definition ltrText.cpp:61
void appendLTR_Text(const LTR_Text &ltr)
Definition ltrText.cpp:65
void addToken(boost::shared_ptr< LTR_Token > &token)
Definition ltrText.cpp:57
void binaryReadFrom(std::istream &is)
Definition ltrText.cpp:116
void binaryWriteOn(std::ostream &os) const
Definition ltrText.cpp:86
SENTENCE_BOUNDS_T::const_iterator SENTS_CONST_ITER_T
Definition ltrText.h:49
a token of a linear text representation Each LTR_Token represents the set of words that start at a po...
Definition ltrToken.h:48
=======================================================================
T & operator<<(T &qd, const BoWType &bt)
uint64_t readCodedInt(std::istream &file)
read a integer coded in variable-byte format in a file
void writeCodedInt(std::ostream &file, const uint64_t number)
write a integer coded in variable-byte format in a file
NAUTITIA.
STL namespace.