LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
MultimediaDocumentReaderWriter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
2//
3// SPDX-License-Identifier: MIT
4
13#include <sstream>
14
15using namespace Lima;
16using namespace Common;
17using namespace std;
18// using namespace Common::XMLConfigurationFiles;
19using namespace Common::MediaticData;
20using namespace Common::BagOfWords;
21using namespace Lima::Handler;
22
23namespace Lima {
24namespace Handler {
25
27{
28 friend class MultimediaXMLWriter;
29
30 MultimediaXMLWriterPrivate(std::ostream& os): m_outputStream(os){};
31
32 void writeMultimediaFeature(std::vector<float> vect);
33 void writeVectorFloat(std::vector<float> vect);
34
35 std::ostream& m_outputStream;
36 std::string m_spaces;
38 // for indentations
39 void incIndent() { m_spaces+=" "; }
40 void decIndent() { m_spaces.erase(m_spaces.size()-2); }
41 void setSpaces(const std::string& s){m_spaces=s;}
42};
43}
44}
45
47 BoWXMLWriter(os), m_dmult(new MultimediaXMLWriterPrivate(os))
48{
49
50}
51
53{
54 delete m_dmult;
55}
56
58{
59 m_dmult->m_outputStream << "<?xml-stylesheet type=\"text/xsl\" href=\"bow.xslt\"?>" << std::endl;
60 m_dmult->m_outputStream <<"<MultimediaDocuments>" << std::endl;
61 incIndent();
62}
63
65 decIndent();
66 m_dmult->m_outputStream <<"</MultimediaDocuments>" << std::endl;
67}
68
69void MultimediaXMLWriter::setSpaces(const std::string& s)
70{
72 m_dmult->setSpaces(s);
73}
74
80
86
88 const Common::Misc::GenericDocumentProperties* /*unused properties*/,
89 const std::string& elementName )
90{
91 m_dmult->m_outputStream << m_dmult->m_spaces << "<node elementName=\""
92 << elementName << "\">" << std::endl;
93 incIndent();
94}
95
97 const Common::Misc::GenericDocumentProperties* /*unused properties*/,
98 const std::string& elementName )
99{
100 m_dmult->m_outputStream << m_dmult->m_spaces << "<node elementName=\""
101 << elementName << "\" indexingNode=\"yes\">" << std::endl;
102 incIndent();
103}
104
106{
107 decIndent();
108 m_dmult->m_outputStream << m_dmult->m_spaces << "</node>" << std::endl;
109}
110
111//***********************************************************************
112// reader
114 Common::BagOfWords::BoWBinaryReader()
115{
116 m_content_id=1;
117}
118
122
123
125 unsigned int& cid,
126 string& path,
127 string& name,
128 std::vector<float>& vect)
129{
130 unsigned int size;
131 unsigned int contentId;
132 file.read(( char* ) &contentId,sizeof ( unsigned int ));
133 cid=contentId;
134 file.read(( char* ) &size,sizeof (unsigned int ));
135
136 char* cstr=new char[size+1]; // + 1 for '\0' (not in file)
137 file.read((char *) cstr,size*sizeof(char));
138 cstr[size]='\0'; // add the end of string character
139 name=string(cstr);
140 delete []cstr;
141
142 file.read(( char* ) &size,sizeof (unsigned int));
143
144 char* cstr2=new char[size+1]; // + 1 for '\0' (not in file)
145 file.read((char *) cstr2,size*sizeof(char));
146 cstr2[size]='\0'; // add the end of string character
147 path=string(cstr2);
148 delete []cstr2;
149
150 file.read(( char* ) &size,sizeof ( unsigned int ));
151
152 for (unsigned int i=0;i<size;i++)
153 {
154 float val;
155 file.read(( char* ) &val,sizeof ( float ));
156 vect.push_back(val);
157 }
158}
159
161 std::istream& file,
162 BoWDocument& document,
163 MultimediaXMLWriter& handler,
164 bool useIterator,
165 bool useIndexIterator)
166{
167 BoWBlocType blocType = static_cast<BoWBlocType>(Misc::readOneByteInt(file));
168
169#ifdef DEBUG_LP
171 LDEBUG << "MultimediaBinaryReader::readBoWDocumentBlock: read blocType" << blocType;
172#endif
173 // new format
174 switch ( blocType )
175 {
176 case BoWBlocType::HIERARCHY_BLOC:
177 {
178#ifdef DEBUG_LP
179 LDEBUG << "HIERARCHY_BLOC";
180#endif
181 std::string elementName;
182 Misc::readStringField(file,elementName);
183 handler.openSNode(&document, elementName);
184// m_content_id++;
185 break;
186 }
187 case BoWBlocType::INDEXING_BLOC:
188 {
189#ifdef DEBUG_LP
190 LDEBUG << "INDEXING_BLOC";
191#endif
192 std::string elementName;
193 Misc::readStringField(file,elementName);
194 handler.openSIndexingNode(&document, elementName);
195// m_content_id++;
196 break;
197 }
198 case BoWBlocType::BIN_NUM_BLOC:
199 {
200#ifdef DEBUG_LP
201 LDEBUG << "BIN_NUM_BLOC";
202#endif
203 document.clear();
204 unsigned int cid;string name,path;std::vector<float> vect;
205
206 readBinNum(file,cid,path,name,vect);
207 document.setStringValue("type","numerical_feature");
208 document.setIntValue("ContentId",m_content_id);
209 document.setStringValue("PathImage",path);
210 document.setStringValue("name",name);
211 handler.processSContent(&document);
212 handler.processSBinNum(vect,useIterator);
213 handler.processProperties(&document, useIterator, useIndexIterator);
214 handler.closeSContent();
215 m_content_id++;
216 break;
217 }
218 case BoWBlocType::BOW_TEXT_BLOC:
219 {
220 #ifdef DEBUG_LP
221 LDEBUG << "BOW_TEXT_BLOC";
222 #endif
223 document.clear();
224 readBoWText(file,document);
225 document.setStringValue("type","tokens");
226 document.setIntValue("ContentId",m_content_id);
227 handler.processSContent(&document);
228 handler.processSBoWText(&document, useIterator, useIndexIterator);
229 handler.processProperties(&document, useIterator, useIndexIterator);
230 handler.closeSContent();
231 m_content_id++;
232 break;
233 }
234 case BoWBlocType::NODE_PROPERTIES_BLOC:
235 {
236#ifdef DEBUG_LP
237 LDEBUG << "NODE_PROPERTIES_BLOC";
238#endif
239 document.Misc::GenericDocumentProperties::read(file);
240 handler.processProperties(&document, useIterator, useIndexIterator);
241 break;
242 }
243 case BoWBlocType::END_BLOC:
244 {
245#ifdef DEBUG_LP
246 LDEBUG << "END_BLOC";
247#endif
248 handler.closeSNode();
249 break;
250 }
251 case BoWBlocType::DOCUMENT_PROPERTIES_BLOC:
252 { // do nothing ?
253#ifdef DEBUG_LP
254 LDEBUG << "DOCUMENT_PROPERTIES_BLOC";
255#endif
256 break;
257 }
258 case BoWBlocType::ST_BLOC:
259 { // do nothing ?
260#ifdef DEBUG_LP
261 LDEBUG << "ST_BLOC";
262#endif
263 break;
264 }
265 default:;
266 }
267
268 file.peek(); // Try to read next byte to force update end-of-file flag on windows
269 if (file.eof())
270 {
271#ifdef DEBUG_LP
273 LDEBUG << "MultimediaBinaryReader::readMultimediaDocumentBlock EOF reached but last blockType was" << blocType << getBlocTypeString(blocType);
274#endif
275 if(blocType!= BoWBlocType::END_BLOC){
277 LERROR << "MultimediaBinaryReader::readMultimediaDocumentBlock EOF reached but last blockType was not END_BLOC : " << blocType << getBlocTypeString(blocType);
278 }
279 }
280 return blocType;
281}
282
284{
285 m_dmult->m_outputStream << m_dmult->m_spaces << "<content type=\""
286 << properties->getStringValue("type").first << "\">" << std::endl;
287 incIndent();
288}
289
291{
292 decIndent();
293 m_dmult->m_outputStream <<m_dmult->m_spaces <<"</content>" << std::endl;
294}
295
296
297void MultimediaXMLWriter::processSBinNum(std::vector<float> vect, bool useIterator)
298{
299 LIMA_UNUSED(useIterator);
300 m_dmult->writeMultimediaFeature(vect);
301}
302
303void MultimediaXMLWriterPrivate::writeVectorFloat(std::vector<float> vect)
304{
305 unsigned int size = vect.size();
306 m_outputStream << m_spaces << "<vector type=\"float\" n=\""
307 << size << "\">" << std::endl;
308 m_outputStream<<m_spaces;
309 for (unsigned int i=0;i<size-1;i++)
310 {
311 m_outputStream<<vect.at(i)<<" ";
312 }
313 m_outputStream<<vect.at(size-1)<<std::endl;
314 m_outputStream<<m_spaces<<"</vector>"<<std::endl ;
315}
316void MultimediaXMLWriterPrivate::writeMultimediaFeature(std::vector<float> vect)
317{
318 incIndent();
319 m_outputStream<<m_spaces<< "<feature>"<<std::endl;
320 incIndent();
321 writeVectorFloat(vect);
322 decIndent();
323 m_outputStream<<m_spaces <<"</feature>"<<std::endl;
324 decIndent();
325}
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define BOWLOGINIT
Definition LimaCommon.h:201
void readBoWText(std::istream &file, BoWText &bowText)
represent a document as a Bag Of Word, with associated document properties.
Definition bowDocument.h:57
void setSpaces(const std::string &s)
void processSBoWText(const BoWText *boWText, bool useIterators, bool useIndexIterator) override
void processProperties(const Misc::GenericDocumentProperties *properties, bool useIterators, bool useIndexIterator) override
std::pair< std::string, bool > getStringValue(std::string propName) const
void setStringValue(const std::string &propName, const std::string &val)
void setIntValue(const std::string &propName, const uint64_t &val)
Common::BagOfWords::BoWBlocType readMultimediaDocumentBlock(std::istream &file, Common::BagOfWords::BoWDocument &document, Handler::MultimediaXMLWriter &handler, bool useIterator, bool useIndexIterator)
void readBinNum(std::istream &file, unsigned int &cid, std::string &path, std::string &name, std::vector< float > &vect)
void openSNode(const Common::Misc::GenericDocumentProperties *, const std::string &elementName) override
void processSBinNum(std::vector< float > vect, bool useIterator)
void processSContent(const Common::Misc::GenericDocumentProperties *properties) override
void openSIndexingNode(const Common::Misc::GenericDocumentProperties *, const std::string &elementName) override
std::string getBlocTypeString(BoWBlocType blocType)
uint8_t readOneByteInt(std::istream &file)
read a integer coded on one byte in a file
void readStringField(istream &file, string &s)
NAUTITIA.
STL namespace.