LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
bowBinaryReaderWriter.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 *
8 * @file bowBinaryReaderWriter.cpp
9 * @author Romaric Besancon (romaric.besancon@cea.fr)
10 * @date Tue Mar 20 2007
11 * copyright Copyright (C) 2007 by CEA LIST
12 *
13 ***********************************************************************/
14
17#include "bowText.h"
18#include "bowDocument.h"
19#include "bowToken.h"
20#include "bowTerm.h"
21#include "bowNamedEntity.h"
22#include "BoWRelation.h"
23#include "BoWPredicate.h"
24
25#include "common/LimaCommon.h"
30
31using namespace Lima::Common::MediaticData;
32
33namespace Lima {
34namespace Common {
35namespace BagOfWords {
36
37#define BOW_VERSION "0.9"
38
39
41{
42 friend class BoWBinaryReader;
43
46
47
48 std::string m_version;
49 BoWFileType m_fileType;
50
51 // entity types mapping
52 std::map<MediaticData::EntityGroupId,MediaticData::EntityGroupId> m_entityGroupIdMapping;
53 std::map<MediaticData::EntityType,MediaticData::EntityType> m_entityTypeMapping;
54
55 // private member functions
56 void readComplexTokenParts(std::istream& file,
57 boost::shared_ptr< BoWComplexToken > token);
58 void readNamedEntityProperties(std::istream& file,
59 boost::shared_ptr< BoWNamedEntity > ne);
60 boost::shared_ptr< BoWRelation > readBoWRelation(std::istream& file);
61 boost::shared_ptr< AbstractBoWElement > readBoWToken(std::istream& file);
62 void readSimpleToken(std::istream& file,
63 boost::shared_ptr< BoWToken > token);
64 void readPredicate(std::istream& file,
65 boost::shared_ptr< BoWPredicate > bowPred);
66
67};
68
69BoWBinaryReaderPrivate::BoWBinaryReaderPrivate():
70 m_version(""),
71 m_fileType(BOWFILE_NOTYPE),
72 m_entityGroupIdMapping(),
73 m_entityTypeMapping()
74{
75}
76
77BoWBinaryReaderPrivate::~BoWBinaryReaderPrivate()
78{
79}
80
81//***********************************************************************
82// reader
86
88{
89 delete m_d;
90}
91
93{
94 switch (m_d->m_fileType) {
96 return "BOWFILE_NOTYPE";
98 return "BOWFILE_TEXT";
100 return "BOWFILE_DOCUMENT";
102 return "BOWFILE_DOCUMENTST";
104 return "BOWFILE_SDOCUMENT";
105 }
106 return "";
107}
108
109
110void BoWBinaryReader::readHeader(std::istream& file)
111{
112 Misc::readString(file,m_d->m_version);
113 if (m_d->m_version != BOW_VERSION) {
115 LIMA_EXCEPTION("BoWBinaryReader::readHeader - incompatible version : file is in version "
116 << m_d->m_version.c_str() << "; current version is "
117 << BOW_VERSION );
118 }
119 m_d->m_fileType=static_cast<BoWFileType>(Misc::readOneByteInt(file));
120
121 // read entity types
123 readEntityTypes(file,m_d->m_entityGroupIdMapping,m_d->m_entityTypeMapping);
124
125#ifdef DEBUG_LP
127 LDEBUG << "BoWBinaryReader::readHeader type mapping is (shown if logger = TRACE)";
128 std::ostringstream oss;
129 for (std::map<MediaticData::EntityType,MediaticData::EntityType>::const_iterator
130 it=m_d->m_entityTypeMapping.begin(),it_end=m_d->m_entityTypeMapping.end();
131 it!=it_end; it++) {
132 oss << (*it).first << " -> " << (*it).second << std::endl;
133 }
134 LTRACE << oss.str();
135 LDEBUG << "BoWBinaryReader::readHeader end file at: " << file.tellg();
136#endif
137
138}
139
140void BoWBinaryReader::readBoWText(std::istream& file,
141 BoWText& bowText)
142{
143 uint64_t size=Misc::readCodedInt(file);
144#ifdef DEBUG_LP
146 LDEBUG << "BoWBinaryReader::readBoWText file at: " << file.tellg();
147 LDEBUG << "BoWBinaryReader::readBoWText got size=" << size;
148#endif
149 Misc::readString(file,bowText.lang);
150#ifdef DEBUG_LP
151 LDEBUG << "BoWBinaryReader::readBoWText got lang=" << bowText.lang;
152 LDEBUG << "BoWBinaryReader::readBoWText file at: " << file.tellg();
153#endif
154 // need to store the tokens to handle the references in the part
155 // list of the complex tokens
156 for (uint64_t i(0); i<size; i++)
157 {
158#ifdef DEBUG_LP
159 LDEBUG << "BoWBinaryReader::readBoWText reading token " << i;
160#endif
161 boost::shared_ptr< AbstractBoWElement > token = readBoWToken(file);
162 if (token)
163 {
164// BOWLOGINIT;
165// LERROR << __FILE__ << __LINE__ << "BoWBinaryReader::readBoWText do not push token. It should be deleted right now";
166 bowText.push_back(token);
167 }
168 else
169 {
170#ifdef DEBUG_LP
171 LWARN << "BoWBinaryReader::readBoWText Warning: read null token in file";
172#endif
173 }
174 }
175#ifdef DEBUG_LP
176 LDEBUG << "BoWBinaryReader::readBoWText DONE";
177#endif
178}
179
181 BoWDocument& document,
183 bool useIterator,
184 bool useIndexIterator)
185{
186 BoWBlocType blocType = static_cast<BoWBlocType>( Misc::readOneByteInt(file) );
187#ifdef DEBUG_LP
189 LDEBUG << "BoWBinaryReader::readBoWDocumentBlock: read blocType" << blocType;
190#endif
191 // new format
192 switch ( blocType )
193 {
195 {
196#ifdef DEBUG_LP
197 LDEBUG << "HIERARCHY_BLOC";
198#endif
199 std::string elementName;
200 Misc::readStringField(file,elementName);
201 handler.openSBoWNode(&document, elementName);
202 break;
203 }
205 {
206#ifdef DEBUG_LP
207 LDEBUG << "INDEXING_BLOC";
208#endif
209 std::string elementName;
210 Misc::readStringField(file,elementName);
211 handler.openSBoWIndexingNode(&document, elementName);
212 break;
213 }
215 {
216#ifdef DEBUG_LP
217 LDEBUG << "BOW_TEXT_BLOC";
218#endif
219 document.clear();
220 readBoWText(file,document);
221 handler.processSBoWText(&document, useIterator, useIndexIterator);
222 break;
223 }
225 {
226#ifdef DEBUG_LP
227 LDEBUG << "NODE_PROPERTIES_BLOC";
228#endif
229 document.Misc::GenericDocumentProperties::read(file);
230 handler.processProperties(&document, useIterator, useIndexIterator);
231 break;
232 }
234 {
235#ifdef DEBUG_LP
236 LDEBUG << "END_BLOC";
237#endif
238 handler.closeSBoWNode();
239 break;
240 }
242 { // do nothing ?
243#ifdef DEBUG_LP
244 LDEBUG << "DOCUMENT_PROPERTIES_BLOC";
245#endif
246 break;
247 }
249 { // do nothing ?
250#ifdef DEBUG_LP
251 LDEBUG << "ST_BLOC";
252#endif
253 break;
254 }
255 default:;
256 }
257
258 file.peek(); // Try to read next byte to force update end-of-file flag on windows
259 if (file.eof())
260 {
261#ifdef DEBUG_LP
263 LDEBUG << "BoWBinaryReader::readBoWDocumentBlock EOF reached but last blockType was" << blocType << getBlocTypeString(blocType);
264#endif
265 if(blocType!= BoWBlocType::END_BLOC){
267 LERROR << "BoWBinaryReader::readBoWDocumentBlock EOF reached but last blockType was not END_BLOC :" << blocType << getBlocTypeString(blocType);
268 }
269 }
270 return blocType;
271}
272
273boost::shared_ptr< AbstractBoWElement > BoWBinaryReader::readBoWToken(std::istream& file)
274{
275 return m_d->readBoWToken(file);
276}
277
278boost::shared_ptr< AbstractBoWElement > BoWBinaryReaderPrivate::readBoWToken( std::istream& file )
279{
280#ifdef DEBUG_LP
282#endif
283 BoWType type=static_cast<BoWType>(Misc::readOneByteInt(file));
284#ifdef DEBUG_LP
285 LDEBUG << "BoWBinaryReader::readBoWToken token type is " << type;
286#endif
287 boost::shared_ptr< AbstractBoWElement > token;
288 switch (type) {
289 case BoWType::BOW_TOKEN: {
290 token=boost::shared_ptr< BoWToken >( new BoWToken);
291 readSimpleToken(file, boost::dynamic_pointer_cast<BoWToken>(token));
292 break;
293 }
294 case BoWType::BOW_TERM: {
295 token=boost::shared_ptr< BoWTerm >(new BoWTerm);
296 // LDEBUG << "BoWToken: calling read(file) on term";
297 readSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
298 readComplexTokenParts(file,boost::dynamic_pointer_cast<BoWComplexToken>(token));
299 break;
300 }
302 token=boost::shared_ptr< BoWNamedEntity >(new BoWNamedEntity);
303// LDEBUG << "BoWToken: calling read(file) on NE";
304 readSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
305 readComplexTokenParts(file,boost::dynamic_pointer_cast<BoWComplexToken>(token));
306 readNamedEntityProperties(file,boost::dynamic_pointer_cast<BoWNamedEntity>(token));
307 break;
308 }
310 token=boost::shared_ptr< BoWPredicate >(new BoWPredicate);
311 readPredicate(file,boost::dynamic_pointer_cast<BoWPredicate>(token));
312 break;
313 }
314 default:
316 LERROR << "Read error: unknown token type.";
317 }
318#ifdef DEBUG_LP
319 if (token != 0) {
320 LDEBUG << "BoWToken: token=" << &*token;
321 }
322#endif
323 return token;
324}
325
326void BoWBinaryReader::readSimpleToken(std::istream& file,
327 boost::shared_ptr< BoWToken > token)
328{
329 m_d->readSimpleToken(file, token);
330}
331
332void BoWBinaryReaderPrivate::readSimpleToken(std::istream& file,
333 boost::shared_ptr< BoWToken > token)
334{
335
336 LimaString lemma,inflectedForm;
337 Misc::readUTF8StringField(file,lemma);
338#ifdef DEBUG_LP
340 LDEBUG << "BoWBinaryReader::readSimpleToken file at: " << file.tellg();
341 LDEBUG << "BoWBinaryReader::readSimpleToken read lemma: " << lemma;
342#endif
343 Misc::readUTF8StringField(file,inflectedForm);
344#ifdef DEBUG_LP
345 LDEBUG << "BoWBinaryReader::readSimpleToken read infl: " << inflectedForm;
346#endif
347 LinguisticCode category;
348 uint64_t position,length;
349 category = LinguisticCode::decodeFromBinary(file);
350 position=Misc::readCodedInt(file);
351 length=Misc::readCodedInt(file);
352 token->setLemma(lemma);
353 token->setInflectedForm(inflectedForm);
354 token->setCategory(category);
355 token->setPosition(position);
356 token->setLength(length);
357}
358
359void BoWBinaryReader::readPredicate(std::istream& file,
360 boost::shared_ptr< BoWPredicate > bowPred){
361 m_d->readPredicate(file, bowPred);
362}
363
364void BoWBinaryReaderPrivate::readPredicate(std::istream& file,
365 boost::shared_ptr< BoWPredicate > bowPred){
366#ifdef DEBUG_LP
368 LDEBUG << "BoWBinaryReaderPrivate::readPredicate";
369#endif
370 EntityGroupId groupId = static_cast<EntityGroupId>( Misc::readCodedInt(file));
371 EntityTypeId typeId = static_cast<EntityTypeId>(Misc::readCodedInt(file));
373 Common::MediaticData::EntityType predType=m_entityTypeMapping[oldType];
374
375 bowPred->setPosition(Misc::readCodedInt(file));
376 bowPred->setLength(Misc::readCodedInt(file));
377 uint64_t roleNb = Misc::readCodedInt(file);
378#ifdef DEBUG_LP
379 LDEBUG << "BoWBinaryReader::readPredicate Read "<< roleNb<< "roles associated to the predicate";
380#endif
381 bowPred->setPredicateType(predType);
382 for (uint64_t i = 0; i < roleNb; i++)
383 {
384 EntityGroupId roleGrId = static_cast<EntityGroupId>(Misc::readCodedInt(file));
385 EntityTypeId roleTypId = static_cast<EntityTypeId>(Misc::readCodedInt(file));
387 Common::MediaticData::EntityType roleType=m_entityTypeMapping[oldRoleType];
388 boost::shared_ptr< AbstractBoWElement > role = readBoWToken(file);
389 bowPred->roles().insert(roleType,role);
390 }
391}
392
393void BoWBinaryReaderPrivate::readComplexTokenParts(std::istream& file,
394 boost::shared_ptr< Lima::Common::BagOfWords::BoWComplexToken > token)
395{
396#ifdef DEBUG_LP
398 LDEBUG << "BoWBinaryReader::readComplexTokenParts";
399#endif
400 // read parts
401 uint64_t head=Misc::readCodedInt(file);
402 token->setHead(head);
403 uint64_t nbParts=Misc::readCodedInt(file);
404 for (uint64_t i(0); i<nbParts; i++) {
405 boost::shared_ptr< BoWRelation> rel=readBoWRelation(file);
406 bool isInList;
407 file.read((char*) &(isInList), sizeof(bool));
408 boost::shared_ptr< BoWToken > tok = boost::dynamic_pointer_cast<BoWToken>(readBoWToken(file));
409 if (tok)
410 token->addPart(rel,tok);
411 }
412}
413
414void BoWBinaryReaderPrivate::readNamedEntityProperties(std::istream& file, boost::shared_ptr< Lima::Common::BagOfWords::BoWNamedEntity > ne)
415{
416#ifdef DEBUG_LP
418#endif
419 EntityGroupId groupId=static_cast<EntityGroupId>(Misc::readCodedInt(file));
420 EntityTypeId typeId=static_cast<EntityTypeId>(Misc::readCodedInt(file));
422 Common::MediaticData::EntityType seType=m_entityTypeMapping[oldType];
423#ifdef DEBUG_LP
424 LDEBUG << "BoWBinaryReader::readNamedEntityProperties read type " << oldType << "-->" << seType;
425#endif
426 ne->setNamedEntityType(seType);
427 uint64_t nbFeatures=Misc::readCodedInt(file);
428 for (uint64_t i(0); i<nbFeatures; i++) {
429 std::string attribute;
430 LimaString value;
431 Misc::readStringField(file,attribute);
432 Misc::readUTF8StringField(file,value);
433 ne->setFeature(attribute,value);
434 }
435}
436
437boost::shared_ptr< BoWRelation > BoWBinaryReaderPrivate::readBoWRelation(std::istream& file)
438{
439#ifdef DEBUG_LP
441#endif
442 boost::shared_ptr< BoWRelation > rel=boost::shared_ptr< BoWRelation >();
443 uint64_t hasRelation=Misc::readCodedInt(file);
444#ifdef DEBUG_LP
445 LDEBUG << "BoWBinaryReaderPrivate::readBoWRelation: read hasRelation" << hasRelation;
446#endif
447 if (hasRelation) {
448 rel = boost::shared_ptr< BoWRelation >(new BoWRelation());
449 rel->read(file);
450 }
451 return rel;
452}
453
455{
456 return m_d->m_fileType;
457}
458
459
460//***********************************************************************
461// writer
463{
464 friend class BoWBinaryWriter;
465
466 BoWBinaryWriterPrivate(std::shared_ptr<const ShiftFrom> shiftFrom);
468
469 // private member functions
470 void writeComplexTokenParts(std::ostream& file,
471 const boost::shared_ptr< BoWComplexToken > token) const;
472 void writeNamedEntityProperties(std::ostream& file,
473 const boost::shared_ptr< BoWNamedEntity > ne) const;
474 void writeBoWRelation(std::ostream& file,
475 boost::shared_ptr< BoWRelation > rel) const;
476 void writeBoWToken(std::ostream& file,
477 const boost::shared_ptr< AbstractBoWElement > bowToken) const;
478 void writeSimpleToken(std::ostream& file,
479 const boost::shared_ptr< BoWToken > token) const;
480 void writePredicate(std::ostream& file,
481 const boost::shared_ptr< BoWPredicate > predicate) const;
482
483 std::shared_ptr<const ShiftFrom> m_shiftFrom;
484};
485
486BoWBinaryWriterPrivate::BoWBinaryWriterPrivate(std::shared_ptr<const ShiftFrom> shiftFrom) :
487 m_shiftFrom(shiftFrom)
488{
489#ifdef DEBUG_LP
491 LDEBUG << "BoWBinaryWriterPrivate::BoWBinaryWriterPrivate";
492 LDEBUG << "BoWBinaryWriterPrivate::BoWBinaryWriterPrivate";
493#endif
494}
495
496BoWBinaryWriterPrivate::~BoWBinaryWriterPrivate()
497{}
498
499
500BoWBinaryWriter::BoWBinaryWriter(std::shared_ptr<const ShiftFrom> shiftFrom) :
501 m_d(new BoWBinaryWriterPrivate(shiftFrom))
502{
503#ifdef DEBUG_LP
505 LDEBUG << "BoWBinaryWriter::BoWBinaryWriter";
506#endif
507}
508
510{
511 delete m_d;
512}
513
514void BoWBinaryWriter::writeHeader(std::ostream& file, BoWFileType type) const
515{
516#ifdef DEBUG_LP
518 LDEBUG << "BoWBinaryWriter::writeHeader version=" << BOW_VERSION << " ; type=" << type;
519#endif
521 Misc::writeOneByteInt(file,static_cast<unsigned char>(type));
522 // write entity types
524#ifdef DEBUG_LP
525 LDEBUG << "BoWBinaryWriter::writeHeader end on file " << &file << "at: " << file.tellp();
526#endif
527}
528
529void BoWBinaryWriter::writeBoWText(std::ostream& file, const BoWText& bowText) const
530{
531#ifdef DEBUG_LP
533 LDEBUG << "BoWBinaryWriter::writeBoWText";
534// Q_ASSERT(!bowText.lang.empty());
535#endif
536 Misc::writeCodedInt(file,bowText.size());
537 Misc::writeString(file,bowText.lang);
538#ifdef DEBUG_LP
539 LDEBUG << "BoWBinaryWriter::writeBoWText wrote lang" << bowText.lang
540 << "on file"<<&file<<" at: " << file.tellp();
541#endif
542 uint64_t tokenCounter(0);
543 // build reverse map to store in file numbers instead of pointers
544 for (BoWText::const_iterator it=bowText.begin(),
545 it_end=bowText.end(); it!=it_end; it++) {
546 writeBoWToken(file,*it);
547 tokenCounter++;
548 }
549}
550
551void BoWBinaryWriter::writeBoWDocument(std::ostream& file,
552 const BoWDocument& doc) const
553{
555 LERROR << "BoWBinaryWriter: writeBoWDocument non implemented";
556 LERROR << "Can not write "<< doc << " into stream"<< &file;
557}
558
559void BoWBinaryWriter::writeBoWToken(std::ostream& file,
560 const boost::shared_ptr< AbstractBoWElement > token) const
561{
562 m_d->writeBoWToken(file, token);
563}
564
565void BoWBinaryWriterPrivate::writeBoWToken(
566 std::ostream& file,
567 const boost::shared_ptr< Lima::Common::BagOfWords::AbstractBoWElement > token ) const
568{
569#ifdef DEBUG_LP
571 LDEBUG << "BoWBinaryWriter::writeBoWToken token type is " << token->getType() << &file;
572#endif
573 Misc::writeOneByteInt(file,toInt(token->getType()));
574 switch (token->getType()) {
575 case BoWType::BOW_TOKEN: {
576 writeSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
577 break;
578 }
579 case BoWType::BOW_TERM: {
580 writeSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
581 writeComplexTokenParts(file,boost::dynamic_pointer_cast<BoWComplexToken>(token));
582 break;
583 }
585 boost::shared_ptr< BoWNamedEntity > ne=boost::dynamic_pointer_cast<BoWNamedEntity>(token);
586 writeSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
587 writeComplexTokenParts(file,ne);
588 writeNamedEntityProperties(file,ne);
589 break;
590 }
592 writePredicate(file,boost::dynamic_pointer_cast<BoWPredicate>(token));
593 break;
594 }
595 default: {
597 LIMA_EXCEPTION( "BoWBinaryWriter: cannot handle BoWType " << token->getType() );
598 }
599 }
600}
601
602void BoWBinaryWriter::writeSimpleToken(std::ostream& file,
603 const boost::shared_ptr< BoWToken > token) const
604{
605 m_d->writeSimpleToken(file, token);
606}
607
608void BoWBinaryWriterPrivate::writeSimpleToken(std::ostream& file,
609 const boost::shared_ptr< BoWToken > token) const
610{
611#ifdef DEBUG_LP
613 LDEBUG << "BoWBinaryWriter::writeSimpleToken write lemma: " << &file << token->getLemma();
614#endif
615 Misc::writeUTF8StringField(file,token->getLemma());
616#ifdef DEBUG_LP
617 LDEBUG << "BoWBinaryWriter::writeSimpleToken write infl: " << token->getInflectedForm();
618#endif
619 Misc::writeUTF8StringField(file,token->getInflectedForm());
620 LinguisticCode::encodeToBinary(file, token->getCategory());
621
622 auto beg = token->getPosition();
623 auto end = token->getLength() + beg;
624
625#ifdef DEBUG_LP
626 LDEBUG << "BoWBinaryWriter::writeSimpleToken shiftFrom is:";
627#endif
628 if (m_shiftFrom.use_count() > 0)
629 {
630#ifdef DEBUG_LP
631 LTRACE << "m_shiftFrom.use_count:" << m_shiftFrom.use_count() ;
632#endif
633 beg = m_shiftFrom->correct_offset(0, beg);
634 end = m_shiftFrom->correct_offset(0, end);
635 }
636
637#ifdef DEBUG_LP
638 LDEBUG << "BoWBinaryWriter::writeSimpleToken writen begin :" << beg-1;
639 LDEBUG << "BoWBinaryWriter::writeSimpleToken writen length:" << end-beg;
640#endif
641 Misc::writeCodedInt(file, beg-1);
642 Misc::writeCodedInt(file, end-beg);
643}
644
645void BoWBinaryWriter::writePredicate(std::ostream& file,
646 const boost::shared_ptr< BoWPredicate > predicate) const{
647 m_d->writePredicate(file, predicate);
648}
649
650void BoWBinaryWriterPrivate::writePredicate(std::ostream& file,
651 const boost::shared_ptr< BoWPredicate > predicate) const{
652#ifdef DEBUG_LP
654#endif
655 MediaticData::EntityType type=predicate->getPredicateType();
656#ifdef DEBUG_LP
657 LDEBUG << "BoWBinaryWriter::writePredicate type" << type;
658#endif
659 Misc::writeCodedInt(file,type.getGroupId());
660 Misc::writeCodedInt(file,type.getTypeId());
661 Misc::writeCodedInt(file,predicate->getPosition()-1);
662 Misc::writeCodedInt(file,predicate->getLength());
663 const QMultiMap<Common::MediaticData::EntityType, boost::shared_ptr< AbstractBoWElement > >& pRoles = predicate->roles();
664#ifdef DEBUG_LP
665 LDEBUG << "BoWBinaryWriter::writePredicate nb roles" << pRoles.size();
666#endif
667 Misc::writeCodedInt(file,pRoles.size());
668 for (auto it = pRoles.begin(); it != pRoles.end(); it++)
669 {
670 Misc::writeCodedInt(file,it.key().getGroupId());
671 Misc::writeCodedInt(file,it.key().getTypeId());
672 writeBoWToken(file,it.value());
673 }
674}
675
676
677void BoWBinaryWriterPrivate::writeComplexTokenParts(std::ostream& file,
678 const boost::shared_ptr< BoWComplexToken > token) const
679{
680#ifdef DEBUG_LP
682#endif
683 // write parts
684 Misc::writeCodedInt(file,token->getHead());
685 const std::deque<BoWComplexToken::Part>& parts=token->getParts();
686#ifdef DEBUG_LP
687 LDEBUG << "BoWBinaryWriter::writeComplexTokenParts nb parts is " << parts.size();
688#endif
689 Misc::writeCodedInt(file,parts.size());
690
691 for (uint64_t i(0); i<parts.size(); i++) {
692 writeBoWRelation(file,parts[i].get<0>());
693 bool isInList=false;
694#ifdef DEBUG_LP
695 LDEBUG << "BoWBinaryWriter::writeComplexTokenParts writing";
696#endif
697 file.write((char*) &(isInList), sizeof(bool));
698 writeBoWToken(file,parts[i].getBoWToken());
699 }
700}
701
702void BoWBinaryWriterPrivate::writeNamedEntityProperties(std::ostream& file,
703 const boost::shared_ptr< BoWNamedEntity > ne) const
704{
705 MediaticData::EntityType type=ne->getNamedEntityType();
706#ifdef DEBUG_LP
708 LDEBUG << "BoWBinaryWriter::writeNamedEntityProperties : write type " << type.getGroupId() << "." << type.getTypeId();
709#endif
710 Misc::writeCodedInt(file,type.getGroupId());
711 Misc::writeCodedInt(file,type.getTypeId());
712 const std::map<std::string, LimaString>& features=ne->getFeatures();
713 Misc::writeCodedInt(file,features.size());
714 for (std::map<std::string, LimaString>::const_iterator
715 it=features.begin(), it_end=features.end(); it!=it_end; it++) {
716 Misc::writeStringField(file,(*it).first);
717 Misc::writeUTF8StringField(file,(*it).second);
718 }
719}
720
721void BoWBinaryWriterPrivate::writeBoWRelation(std::ostream& file,
722 boost::shared_ptr< BoWRelation > rel) const
723{
724 uint64_t hasRelation=(rel!=0);
725#ifdef DEBUG_LP
727 LDEBUG << "BoWBinaryWriter::writeBoWRelation: write hasRelation" << hasRelation;
728#endif
729 Misc::writeCodedInt(file, hasRelation);
730 if (hasRelation) {
731 Misc::writeUTF8StringField(file,rel->getRealization());
732 Misc::writeCodedInt(file,rel->getType());
733 Misc::writeCodedInt(file,rel->getSynType());
734 }
735}
736
737
738} // end namespace BagOfWords
739} // end namespace Common
740} // end namespace Lima
#define LWARN
Definition LimaCommon.h:160
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
Definition LimaCommon.h:293
#define LTRACE
Definition LimaCommon.h:156
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define BOWLOGINIT
Definition LimaCommon.h:201
#define BOW_VERSION
virtual void openSBoWIndexingNode(const Lima::Common::Misc::GenericDocumentProperties *properties, const std::string &elementName)=0
virtual void processSBoWText(const BoWText *boWText, bool useIterators, bool useIndexIterator)=0
virtual void processProperties(const Misc::GenericDocumentProperties *properties, bool useIterators, bool useIndexIterator) override=0
virtual void openSBoWNode(const Lima::Common::Misc::GenericDocumentProperties *properties, const std::string &elementName)=0
void readSimpleToken(std::istream &file, boost::shared_ptr< BoWToken > token)
boost::shared_ptr< Lima::Common::BagOfWords::AbstractBoWElement > readBoWToken(std::istream &file)
void readBoWText(std::istream &file, BoWText &bowText)
BoWBlocType readBoWDocumentBlock(std::istream &file, BoWDocument &document, AbstractBoWDocumentHandler &handler, bool useIterator, bool useIndexIterator)
void readPredicate(std::istream &file, boost::shared_ptr< BoWPredicate > bowPred)
void writeBoWToken(std::ostream &file, const boost::shared_ptr< AbstractBoWElement > bowToken) const
void writeBoWDocument(std::ostream &file, const BoWDocument &bowText) const
void writeBoWText(std::ostream &file, const BoWText &bowText) const
void writeSimpleToken(std::ostream &file, const boost::shared_ptr< BoWToken > token) const
BoWBinaryWriter(std::shared_ptr< const ShiftFrom > shiftFrom=std::shared_ptr< const ShiftFrom >())
void writeHeader(std::ostream &file, BoWFileType type) const
void writePredicate(std::ostream &file, const boost::shared_ptr< BoWPredicate > token) const
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
This class contains the representation of an element of the bag of words.
Definition bowToken.h:46
void writeEntityTypes(std::ostream &os) const
static std::ostream & encodeToBinary(std::ostream &os, const LinguisticCode &value)
Definition StdBitset.h:173
static LinguisticCode decodeFromBinary(std::istream &is)
Definition StdBitset.h:197
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
std::string getBlocTypeString(BoWBlocType blocType)
LIMA_BOW_EXPORT uint8_t toInt(const BoWType &bt)
Definition bowToken.cpp:42
BoWType
enum to characterize the type of the AbstractBoWElement
@ BOW_TERM
the AbstractBoWElement is a multi-term
@ BOW_NAMEDENTITY
the AbstractBoWElement is a named entity
@ BOW_TOKEN
the AbstractBoWElement is a simple token
@ BOW_PREDICATE
the AbstractBoWElement is a predicate (n-ary relation, template or semantic frame
void writeUTF8StringField(std::ostream &file, const Lima::LimaString &s)
Binary write a a LimaString in a file as an utf8 string.
void readString(istream &file, string &s)
Read an input stream into a wide string, until EOF.
uint8_t readOneByteInt(std::istream &file)
read a integer coded on one byte in a file
void writeString(ostream &file, const string &s)
write a string on an output file stream
uint64_t readCodedInt(std::istream &file)
read a integer coded in variable-byte format in a file
void readStringField(istream &file, string &s)
void writeOneByteInt(std::ostream &file, const uint8_t number)
write a integer coded on one byte format in a file
void writeStringField(ostream &file, const string &s)
void readUTF8StringField(std::istream &file, Lima::LimaString &s)
Binary read an utf8 string from a file and store it in a LimaString.
void writeCodedInt(std::ostream &file, const uint64_t number)
write a integer coded in variable-byte format in a file
NAUTITIA.
QString LimaString
Definition LimaString.h:33