LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
MultimediaStructureReaderClasses.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
9#include <sstream>
10
11using namespace Lima;
12using namespace Lima::Common;
13using namespace Lima::Common::BagOfWords;
14using namespace Lima::Handler;
15
16using namespace std;
17
20 ContentHandler<Common::Misc::GenericDocumentProperties>(),
21 ContentHandler< std::vector<float> >(),
22 ContentHandler< Common::BagOfWords::BoWText >(),
23 m_lastNode(0),
24 m_openedNodes(),
25 m_metadata(),
26 m_mapNodes()
27{
29}
30
40
42 const Misc::GenericDocumentProperties* /*unused properties*/,
43 const std::string& elementName )
44{
45 LIMA_UNUSED(elementName);
46 m_lastNode++;
47 m_openedNodes.push_back(m_lastNode);
48}
49
51 const Misc::GenericDocumentProperties* /*unused properties*/,
52 const std::string& elementName )
53{
54 LIMA_UNUSED(elementName);
55 m_lastNode++;
56 m_openedNodes.push_back(m_lastNode);
57}
58
62//***********************************************************************
63// reader
67
71
73 uint64_t& cid,
74 string& path,
75 string& name,
76 std::vector< float >& vect)
77{
78
79 unsigned int size;
80 unsigned int contentId;
81 file.read(( char* ) &contentId,sizeof ( unsigned int ));
82 cid=contentId;
83 file.read(( char* ) &size,sizeof (unsigned int ));
84
85 char* cstr=new char[size+1]; // + 1 for '\0' (not in file)
86 file.read((char *) cstr,size*sizeof(char));
87 cstr[size]='\0'; // add the end of string character
88 name=string(cstr);
89 delete []cstr;
90
91 file.read(( char* ) &size,sizeof (unsigned int));
92
93 char* cstr2=new char[size+1]; // + 1 for '\0' (not in file)
94 file.read((char *) cstr2,size*sizeof(char));
95 cstr2[size]='\0'; // add the end of string character
96 path=string(cstr2);
97 delete []cstr2;
98
99 file.read(( char* ) &size,sizeof ( unsigned int ));
100
101 for (unsigned int i=0;i<size;i++)
102 {
103 float val;
104 file.read(( char* ) &val,sizeof ( float ));
105 vect.push_back(val);
106 }
107}
108
110{
111
112#ifdef DEBUG_LP
114 LDEBUG << "MultimediaXMLStructureReader::closePropertiesNode()";
115#endif
116 int NodeId = -1;
117 int StructId = -1;
118 int offBegPrpty = -1, offEndPrpty = -1;
119 string encodPrpty, langPrpty, srcePrpty;
120 string indexDatePrpty;
121 // get values of properties whose type is int
122 auto beginEnd = prop->getIntProperties();
123 for(auto elmtIt = beginEnd.first; elmtIt != beginEnd.second; elmtIt++ )
124 {
125 const auto& elmt = *elmtIt;
126 if (elmt.first == "StructureId")
127 StructId = elmt.second ;
128 else if (elmt.first == "offBegPrpty")
129 offBegPrpty = elmt.second ;
130 else if (elmt.first == "offEndPrpty")
131 offEndPrpty = elmt.second ;
132 else if (elmt.first == "NodeId")
133 NodeId = elmt.second ;
134 }
135 // get values of properties whose type is string
136 auto beginEndS = prop->getStringProperties();
137 for(auto elmtIt = beginEndS.first; elmtIt != beginEndS.second; elmtIt++)
138 {
139 const auto& elmt = *elmtIt;
140 if (elmt.first == "encodPrpty")
141 encodPrpty=elmt.second ;
142 else if (elmt.first == "langPrpty")
143 langPrpty=elmt.second ;
144 else if (elmt.first == "srcePrpty")
145 srcePrpty=elmt.second ;
146 }
147 // get values of properties whose type is date
148 auto beginEndD = prop->getDateProperties();
149 for(auto elmtIt = beginEndD.first; elmtIt != beginEndD.second; elmtIt++)
150 {
151 const auto& elmt = *elmtIt;
152 if (elmt.first == "indexDatePrpty")
153 indexDatePrpty = elmt.second.toString(Qt::ISODate).toUtf8().data();
154 }
155
156 // openedNodes.pop_back();
157
158 const std::map<uint64_t,Node> Nodes=m_mapNodes[StructId][NodeId];
159 // cout<<"ici "<<StructId<<" "<<NodeId<<endl;
160 if (!m_openedNodes.empty())
161 m_openedNodes.pop_back();
162 else
163 m_lastNode=0;
164
165 for (auto ItrNodes = Nodes.cbegin(); ItrNodes!=Nodes.cend(); ItrNodes++)
166 {
167 const Node& laNode = ItrNodes->second;
168 const auto& uri = laNode.uri();
169 if (uri.length()!=0)
170 srcePrpty = uri;
171
172 // TODO: replace MULTIMEDIA_DOCUMENT_READER0 with consistent value
173 Node node("MULTIMEDIA_DOCUMENT_READER0", // docName
174 StructId, // structId
175 NodeId, // nodeId
176 laNode.get_ContentId(), // contentId
177 laNode.indexId(), // indexid
178 srcePrpty, // uri
179 "", // nodeMedia
180 "", // nodeType
181 laNode.descrId(), // descrId
182 offBegPrpty, // nodeStart
183 offEndPrpty, // nodeEnd
184 offEndPrpty-offBegPrpty, // nodeLength
185 m_lastNode // nodeParent
186 );
187 // cout<<"la "<<StructId<<"insertion dans la structure "<<node<<endl;
189 }
190 if (!m_openedNodes.empty())
191 {
192 m_openedNodes.pop_back();
193 }
194}
195
197{
198 Node node = noeud;
199 node.set_NodeId(m_lastNode);
200// cout<<"ici ---0--- "<<m_lastNode<<" "<<m_mapNodes.size() <<" "<<node.get_StructId()<<" Node "<<node<<endl;
201
202 m_mapNodes[node.get_StructId()][m_lastNode][noeud.get_ContentId()] = node;
203
204// cout<<"insertion du noeid "<<node<<" "<<node.get_StructId()<<" "<<m_lastNode<<" "<<noeud.get_ContentId()<<endl;
205}
206
208{
209 return m_lastNode;
210}
211
213{
214 m_lastNode = n;
215}
216
218 std::istream& file,
219 BoWDocument& document,
221 bool useIterator)
222{
223 LIMA_UNUSED(useIterator);
224 BoWBlocType blocType = static_cast<BoWBlocType>(Misc::readOneByteInt(file));
225#ifdef DEBUG_LP
227 LDEBUG << "MultimediaBinaryReaderStructureReader::readBoWDocumentBlock: read blocType"
228 << (int)blocType;
229#endif
230 // new format
231 switch ( blocType )
232 {
233 case HIERARCHY_BLOC:
234 {
235#ifdef DEBUG_LP
236 LDEBUG << "HIERARCHY_BLOC";
237#endif
238
239 std::string elementName;
240 Misc::readStringField(file, elementName);
241// cout<<"ici ----1--- "<<handler->StructureHandler::get_LastStructureId()<<endl;
242 handler->StructureHandler::set_LastStructureId(handler->StructureHandler::get_LastStructureId()+1);
243// cout<<"ici ----1--- "<<handler->StructureHandler::get_LastStructureId()<<endl;
244 handler->lastNode(0);
245
246 Node node("MULTIMEDIA_DOCUMENT_READER0", // docName
247 handler->StructureHandler::get_LastStructureId(), // structId
248 0, // nodeId
249 0, // contentId
250 0, // indexid
251 "", // uri
252 "xml", // nodeMedia
253 "", // nodeType
254 "hierarchy", // descrId
255 0, // nodeStart
256 0, // nodeEnd
257 0, // nodeLength
258 0 // nodeParent
259 );
260 handler->openSNode(&document, elementName);
261 node.set_NodeId(handler->lastNode());
262 handler->addContentNode(node);
263
264// m_content_id++;
265 break;
266 }
267 case INDEXING_BLOC:
268 {
269#ifdef DEBUG_LP
270 LDEBUG << "INDEXING_BLOC";
271#endif
272 std::string elementName;
273 Misc::readStringField(file, elementName);
274 handler->openSIndexingNode(&document, elementName);
275 break;
276 }
277 case BIN_NUM_BLOC:
278 {
279#ifdef DEBUG_LP
280 LDEBUG << "BIN_NUM_BLOC";
281#endif
282 document.clear();
283 uint64_t cid;
284 string name,path;
285 std::vector<float> vect;
286
287 readBinNum(file, cid, path, name,vect);
288 document.setStringValue("type", "numerical_feature");
289 document.setIntValue("ContentId",
290 handler->StructureHandler::get_LastContentId());
291 document.setStringValue("PathImage", path);
292 document.setStringValue("name", name);
293 handler->processSContent(&document);
294 //TODO: trouver un contentId
295 handler->processSBinNum(vect, name,
296 handler->StructureHandler::get_LastContentId());
297
298 Node node("MULTIMEDIA_DOCUMENT_READER2", // docName
299 handler->StructureHandler::get_LastStructureId(), // structId
300 0, // nodeId
301 handler->StructureHandler::get_LastContentId(), // contentId
302 2, // indexid
303 path, // uri
304 "ima", // nodeMedia
305 "", // nodeType
306 name, // descrId
307 0, // nodeStart
308 0, // nodeEnd
309 0, // nodeLength
310 0 // nodeParent
311 );
312 handler->addContentNode(node);
313 handler->StructureHandler::set_LastContentId(handler->StructureHandler::get_LastContentId()+1);
314 break;
315 }
316 case BOW_TEXT_BLOC:
317 {
318#ifdef DEBUG_LP
319 LDEBUG << "BOW_TEXT_BLOC";
320#endif
321 document.clear();
322 readBoWText(file, document);
323 document.setStringValue("type", "tokens");
324 document.setIntValue("ContentId",
325 handler->StructureHandler::get_LastContentId());
326 handler->processSContent(&document);
327 //TODO: donner un nouveau contentId
328 handler->processBoWText(&document,
329 handler->StructureHandler::get_LastContentId());
330
331 Node node("MULTIMEDIA_DOCUMENT_READER3", // docName
332 handler->StructureHandler::get_LastStructureId(), // structId
333 0, // nodeId
334 handler->StructureHandler::get_LastContentId(), // contentId
335 1, // indexid
336 "", // uri
337 "text", // nodeMedia
338 "", // nodeType
339 "bow", // descrId
340 0, // nodeStart
341 0, // nodeEnd
342 0, // nodeLength
343 0 // nodeParent
344 );
345 handler->addContentNode(node);
346 handler->StructureHandler::set_LastContentId(handler->StructureHandler::get_LastContentId()+1);
347 break;
348 }
350 {
351#ifdef DEBUG_LP
352 LDEBUG << "NODE_PROPERTIES_BLOC";
353#endif
354 document.Misc::GenericDocumentProperties::read(file);
355 handler->closePropertiesNode(&document);//TODO:ici important
356 handler->ContentHandler <Common::Misc::GenericDocumentProperties >::addContent(handler->lastNode(),
357 document);
358 break;
359 }
360 case END_BLOC:
361 {
362#ifdef DEBUG_LP
363 LDEBUG << "END_BLOC";
364#endif
365 handler->closeSNode();
366 break;
367 }
369 { // do nothing ?
370#ifdef DEBUG_LP
371 LDEBUG << "DOCUMENT_PROPERTIES_BLOC";
372#endif
373 break;
374 }
375 case ST_BLOC:
376 { // do nothing ?
377#ifdef DEBUG_LP
378 LDEBUG << "ST_BLOC";
379#endif
380 break;
381 }
382 default:;
383 }
384}
385
391
395
396
398processBoWText(const BoWText* text,int cId)
399{
400// cout<<"ajout d'un contenu linguistique "<<cId<<" "<<*text<<endl;
402}
403
405 const std::vector< float >& vect,
406 const string& SID,
407 int CID)
408{
409 LIMA_UNUSED(SID);
410// m_dmult->writeMultimediaFeature(vect);
412}
413
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#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
This class represents a list of elements, that are pointers on polymmorphic tokens that can be simple...
Definition bowText.h:48
std::pair< DatePropertiesIterator, DatePropertiesIterator > getDateProperties() const
std::pair< IntPropertiesIterator, IntPropertiesIterator > getIntProperties() const
void setStringValue(const std::string &propName, const std::string &val)
std::pair< StringPropertiesIterator, StringPropertiesIterator > getStringProperties() const
void setIntValue(const std::string &propName, const uint64_t &val)
manipulateur de contenu
void resetHandler()
Effacer les contenus du handler.
void addContent(CONTENT_ID ContentId, Content datas)
Ajouter un contenu au handler.
void readMultimediaDocumentBlock(std::istream &file, Common::BagOfWords::BoWDocument &document, MultimediaXMLStructureReader *handler, bool useIterator)
void readBinNum(std::istream &file, uint64_t &cid, std::string &path, std::string &name, std::vector< float > &vect)
void openSIndexingNode(const Common::Misc::GenericDocumentProperties *, const std::string &elementName)
void processSBinNum(const std::vector< float > &vect, const std::string &SID, int CID)
void processSContent(const Common::Misc::GenericDocumentProperties *properties)
void openSNode(const Common::Misc::GenericDocumentProperties *, const std::string &elementName)
void closePropertiesNode(const Common::Misc::GenericDocumentProperties *prop)
void processBoWText(const Common::BagOfWords::BoWText *text, int cId)
noeud de contenu au sein d'une structure.
Definition DataTypes.h:62
STRUCT_ID get_StructId() const
const std::string & descrId() const
CONTENT_ID get_ContentId() const
int32_t indexId() const
const std::string & uri() const
void set_NodeId(NODE_ID NodeId)
void add_Node(const Node &node)
void set_LastContentId(CONTENT_ID)
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.