LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
bowDocument.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 bowDocument.cpp
9 * @author Besancon Romaric (besanconr@zoe.cea.fr)
10 * @date Wed Oct 8 2003
11 * copyright Copyright (C) 2003 by CEA LIST
12 *
13 ***********************************************************************/
14
15#include "bowDocument.h"
17#include <fstream>
18#include <deque>
19
20using namespace std;
21
22namespace Lima
23{
24namespace Common
25{
26namespace BagOfWords
27{
28
29//***********************************************************************
30// stream operators
31//***********************************************************************
32
33std::ostream& operator<<(std::ostream& out,const BoWBlocType& blocType)
34{
35 unsigned char blocTypeCode=static_cast<unsigned char>(blocType);
36 out.write((char*) &(blocTypeCode), sizeof(unsigned char));
37 return out;
38}
39
40QDebug& operator<<(QDebug& out,const BoWBlocType& blocType)
41{
42 unsigned char blocTypeCode=static_cast<unsigned char>(blocType);
43 out << blocTypeCode;
44 return out;
45}
46
47std::istream& operator>>(std::istream& in,BoWBlocType& blocType)
48{
49 unsigned char typeCode;
50 in.read((char*) &(typeCode), sizeof(unsigned char));
51 blocType=static_cast<BoWBlocType>(typeCode);
52 return in;
53}
54
55std::string getBlocTypeString(BoWBlocType blocType)
56{
57 switch (blocType) {
59 return "DOCUMENT_PROPERTIES_BLOC";
61 return "BOW_TEXT_BLOC";
63 return "BIN_NUM_BLOC";
65 return "ST_BLOC";
67 return "END_BLOC";
69 return "NODE_PROPERTIES_BLOC";
71 return "HIERARCHY_BLOC";
73 return "INDEXING_BLOC";
74 }
75 return "";
76}
77
78//***********************************************************************
79// constructors
80//***********************************************************************
82 GenericDocumentProperties(),
83 BoWText()
84{
85#ifdef DEBUG_LP
87 LDEBUG << "BoWDocument::BoWDocument()" << this;
88#endif
89}
90
92 GenericDocumentProperties(d),
93 BoWText(d)
94{
95#ifdef DEBUG_LP
97 LDEBUG << "BoWDocument::BoWDocument(BoWDocument)" << this;
98#endif
99}
100
101//***********************************************************************
102// destructor
103//***********************************************************************
108
109//***********************************************************************
110// assignment operator
111//***********************************************************************
113{
114 if (&d != this)
115 {
117 GenericDocumentProperties::operator=(d);
118 }
119 return *this;
120}
121
127
128//***********************************************************************
129// binary read/write
130//***********************************************************************
131void BoWDocument::setProperties(const GenericDocumentProperties& properties)
132{
133 GenericDocumentProperties::operator=(properties);
134}
135
136//***********************************************************************
137// binary read/write
138//***********************************************************************
140{
141 GenericDocumentProperties::reinit();
143}
144
145//***********************************************************************
146// binary read/write
147//***********************************************************************
148/*
149void BoWDocument::read(std::istream& file)
150{
151 // enum type with small number of values coded on one byte;
152 BoWBlocType blocType = readOneByteInt(file);
153 // old format
154 if (blocType==DOCUMENT_PROPERTIES_BLOC)
155 {
156 Misc::GenericDocumentProperties::read(file);
157 file >> blocType;
158 }
159 while (blocType==BOW_TEXT_BLOC)
160 {
161 BoWText::read(file);
162 if (file.eof()) break;
163 file >> blocType;
164 }
165}
166*/
167
168/*
169void BoWDocument::readPart(std::istream& file, AbstractBoWXMLWriter& writer, bool useIterator, std::ostream& os)
170{
171 BoWBlocType blocType = readOneByteInt(file);
172
173 BOWLOGINIT;
174 LDEBUG << "BoWDocument::readPart: read "
175 << (int)blocType;
176
177 // new format
178 switch( blocType )
179 {
180 case HIERARCHY_BLOC:
181 {
182 std::string elementName;
183 Misc::readStringField(file,elementName);
184 writer.openSBoWNode(os, this, elementName);
185 break;
186 }
187 case INDEXING_BLOC:
188 {
189 std::string elementName;
190 Misc::readStringField(file,elementName);
191 writer.openSBoWIndexingNode(os, this, elementName);
192 break;
193 }
194 case BOW_TEXT_BLOC:
195 {
196 BoWText::clear();
197 BoWText::read(file);
198 writer.processSBoWText(os, this, useIterator);
199 break;
200 }
201 case NODE_PROPERTIES_BLOC:
202 {
203 Misc::GenericDocumentProperties::read(file);
204 writer.processProperties(os, this, useIterator);
205 break;
206 }
207 case END_BLOC:
208 {
209 writer.closeSBoWNode(os);
210 break;
211 }
212 }
213}
214*/
215
216/*
217void BoWDocument::write(std::ostream& file) const
218{
219 // enum type with small number of values coded on one byte;
220 file << DOCUMENT_PROPERTIES_BLOC;
221 Misc::GenericDocumentProperties::write(file);
222 writeOneByteInt(file, BOW_TEXT_BLOC)
223 BoWText::write(file);
224 writeOneByteInt(file, END_BLOC)
225}
226*/
227
228//***********************************************************************
229// output
230//***********************************************************************
231std::ostream& operator << (std::ostream& os, const BoWDocument& d)
232{
233 os << static_cast<Misc::GenericDocumentProperties>(d) << std::endl;
234 os << static_cast<BoWText>(d);
235 return os;
236}
237
238QDebug& operator << (QDebug& os, const BoWDocument& d)
239{
240 os << static_cast<Misc::GenericDocumentProperties>(d);
241 os << static_cast<BoWText>(d);
242 return os;
243}
244
245} // end namespace
246} // end namespace Common
247} // end namespace
#define LDEBUG
Definition LimaCommon.h:157
#define BOWLOGINIT
Definition LimaCommon.h:201
represent a document as a Bag Of Word, with associated document properties.
Definition bowDocument.h:57
virtual void setProperties(const Misc::GenericDocumentProperties &properties)
BoWDocument & operator=(const BoWDocument &)
This class represents a list of elements, that are pointers on polymmorphic tokens that can be simple...
Definition bowText.h:48
BoWText & operator=(const BoWText &)
Definition bowText.cpp:52
std::string getBlocTypeString(BoWBlocType blocType)
std::istream & operator>>(std::istream &in, BoWBlocType &blocType)
T & operator<<(T &qd, const BoWType &bt)
NAUTITIA.
STL namespace.