37#define BOW_VERSION "0.9"
48 std::string m_version;
52 std::map<MediaticData::EntityGroupId,MediaticData::EntityGroupId> m_entityGroupIdMapping;
53 std::map<MediaticData::EntityType,MediaticData::EntityType> m_entityTypeMapping;
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);
69BoWBinaryReaderPrivate::BoWBinaryReaderPrivate():
72 m_entityGroupIdMapping(),
77BoWBinaryReaderPrivate::~BoWBinaryReaderPrivate()
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";
115 LIMA_EXCEPTION(
"BoWBinaryReader::readHeader - incompatible version : file is in version "
116 << m_d->m_version.c_str() <<
"; current version is "
123 readEntityTypes(file,m_d->m_entityGroupIdMapping,m_d->m_entityTypeMapping);
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();
132 oss << (*it).first <<
" -> " << (*it).second << std::endl;
135 LDEBUG <<
"BoWBinaryReader::readHeader end file at: " << file.tellg();
146 LDEBUG <<
"BoWBinaryReader::readBoWText file at: " << file.tellg();
147 LDEBUG <<
"BoWBinaryReader::readBoWText got size=" << size;
151 LDEBUG <<
"BoWBinaryReader::readBoWText got lang=" << bowText.
lang;
152 LDEBUG <<
"BoWBinaryReader::readBoWText file at: " << file.tellg();
156 for (uint64_t i(0); i<size; i++)
159 LDEBUG <<
"BoWBinaryReader::readBoWText reading token " << i;
161 boost::shared_ptr< AbstractBoWElement > token =
readBoWToken(file);
166 bowText.push_back(token);
171 LWARN <<
"BoWBinaryReader::readBoWText Warning: read null token in file";
176 LDEBUG <<
"BoWBinaryReader::readBoWText DONE";
184 bool useIndexIterator)
189 LDEBUG <<
"BoWBinaryReader::readBoWDocumentBlock: read blocType" << blocType;
197 LDEBUG <<
"HIERARCHY_BLOC";
199 std::string elementName;
207 LDEBUG <<
"INDEXING_BLOC";
209 std::string elementName;
217 LDEBUG <<
"BOW_TEXT_BLOC";
227 LDEBUG <<
"NODE_PROPERTIES_BLOC";
229 document.Misc::GenericDocumentProperties::read(file);
244 LDEBUG <<
"DOCUMENT_PROPERTIES_BLOC";
263 LDEBUG <<
"BoWBinaryReader::readBoWDocumentBlock EOF reached but last blockType was" << blocType <<
getBlocTypeString(blocType);
267 LERROR <<
"BoWBinaryReader::readBoWDocumentBlock EOF reached but last blockType was not END_BLOC :" << blocType <<
getBlocTypeString(blocType);
275 return m_d->readBoWToken(file);
278boost::shared_ptr< AbstractBoWElement > BoWBinaryReaderPrivate::readBoWToken( std::istream& file )
285 LDEBUG <<
"BoWBinaryReader::readBoWToken token type is " << type;
287 boost::shared_ptr< AbstractBoWElement > token;
290 token=boost::shared_ptr< BoWToken >(
new BoWToken);
291 readSimpleToken(file, boost::dynamic_pointer_cast<BoWToken>(token));
295 token=boost::shared_ptr< BoWTerm >(
new BoWTerm);
297 readSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
298 readComplexTokenParts(file,boost::dynamic_pointer_cast<BoWComplexToken>(token));
302 token=boost::shared_ptr< BoWNamedEntity >(
new BoWNamedEntity);
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));
310 token=boost::shared_ptr< BoWPredicate >(
new BoWPredicate);
311 readPredicate(file,boost::dynamic_pointer_cast<BoWPredicate>(token));
316 LERROR <<
"Read error: unknown token type.";
320 LDEBUG <<
"BoWToken: token=" << &*token;
327 boost::shared_ptr< BoWToken > token)
329 m_d->readSimpleToken(file, token);
332void BoWBinaryReaderPrivate::readSimpleToken(std::istream& file,
333 boost::shared_ptr< BoWToken > token)
340 LDEBUG <<
"BoWBinaryReader::readSimpleToken file at: " << file.tellg();
341 LDEBUG <<
"BoWBinaryReader::readSimpleToken read lemma: " << lemma;
345 LDEBUG <<
"BoWBinaryReader::readSimpleToken read infl: " << inflectedForm;
348 uint64_t position,length;
352 token->setLemma(lemma);
353 token->setInflectedForm(inflectedForm);
354 token->setCategory(category);
355 token->setPosition(position);
356 token->setLength(length);
360 boost::shared_ptr< BoWPredicate > bowPred){
361 m_d->readPredicate(file, bowPred);
364void BoWBinaryReaderPrivate::readPredicate(std::istream& file,
365 boost::shared_ptr< BoWPredicate > bowPred){
368 LDEBUG <<
"BoWBinaryReaderPrivate::readPredicate";
379 LDEBUG <<
"BoWBinaryReader::readPredicate Read "<< roleNb<<
"roles associated to the predicate";
381 bowPred->setPredicateType(predType);
382 for (uint64_t i = 0; i < roleNb; i++)
388 boost::shared_ptr< AbstractBoWElement > role = readBoWToken(file);
389 bowPred->roles().insert(roleType,role);
393void BoWBinaryReaderPrivate::readComplexTokenParts(std::istream& file,
394 boost::shared_ptr< Lima::Common::BagOfWords::BoWComplexToken > token)
398 LDEBUG <<
"BoWBinaryReader::readComplexTokenParts";
402 token->setHead(head);
404 for (uint64_t i(0); i<nbParts; i++) {
405 boost::shared_ptr< BoWRelation> rel=readBoWRelation(file);
407 file.read((
char*) &(isInList),
sizeof(
bool));
408 boost::shared_ptr< BoWToken > tok = boost::dynamic_pointer_cast<BoWToken>(readBoWToken(file));
410 token->addPart(rel,tok);
414void BoWBinaryReaderPrivate::readNamedEntityProperties(std::istream& file, boost::shared_ptr< Lima::Common::BagOfWords::BoWNamedEntity > ne)
424 LDEBUG <<
"BoWBinaryReader::readNamedEntityProperties read type " << oldType <<
"-->" << seType;
426 ne->setNamedEntityType(seType);
428 for (uint64_t i(0); i<nbFeatures; i++) {
429 std::string attribute;
433 ne->setFeature(attribute,value);
437boost::shared_ptr< BoWRelation > BoWBinaryReaderPrivate::readBoWRelation(std::istream& file)
442 boost::shared_ptr< BoWRelation > rel=boost::shared_ptr< BoWRelation >();
445 LDEBUG <<
"BoWBinaryReaderPrivate::readBoWRelation: read hasRelation" << hasRelation;
448 rel = boost::shared_ptr< BoWRelation >(
new BoWRelation());
456 return m_d->m_fileType;
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;
483 std::shared_ptr<const ShiftFrom> m_shiftFrom;
486BoWBinaryWriterPrivate::BoWBinaryWriterPrivate(std::shared_ptr<const ShiftFrom> shiftFrom) :
487 m_shiftFrom(shiftFrom)
491 LDEBUG <<
"BoWBinaryWriterPrivate::BoWBinaryWriterPrivate";
492 LDEBUG <<
"BoWBinaryWriterPrivate::BoWBinaryWriterPrivate";
496BoWBinaryWriterPrivate::~BoWBinaryWriterPrivate()
505 LDEBUG <<
"BoWBinaryWriter::BoWBinaryWriter";
518 LDEBUG <<
"BoWBinaryWriter::writeHeader version=" <<
BOW_VERSION <<
" ; type=" << type;
525 LDEBUG <<
"BoWBinaryWriter::writeHeader end on file " << &file <<
"at: " << file.tellp();
533 LDEBUG <<
"BoWBinaryWriter::writeBoWText";
539 LDEBUG <<
"BoWBinaryWriter::writeBoWText wrote lang" << bowText.
lang
540 <<
"on file"<<&file<<
" at: " << file.tellp();
542 uint64_t tokenCounter(0);
544 for (BoWText::const_iterator it=bowText.begin(),
545 it_end=bowText.end(); it!=it_end; it++) {
555 LERROR <<
"BoWBinaryWriter: writeBoWDocument non implemented";
556 LERROR <<
"Can not write "<< doc <<
" into stream"<< &file;
560 const boost::shared_ptr< AbstractBoWElement > token)
const
562 m_d->writeBoWToken(file, token);
565void BoWBinaryWriterPrivate::writeBoWToken(
567 const boost::shared_ptr< Lima::Common::BagOfWords::AbstractBoWElement > token )
const
571 LDEBUG <<
"BoWBinaryWriter::writeBoWToken token type is " << token->getType() << &file;
574 switch (token->getType()) {
576 writeSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
580 writeSimpleToken(file,boost::dynamic_pointer_cast<BoWToken>(token));
581 writeComplexTokenParts(file,boost::dynamic_pointer_cast<BoWComplexToken>(token));
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);
592 writePredicate(file,boost::dynamic_pointer_cast<BoWPredicate>(token));
597 LIMA_EXCEPTION(
"BoWBinaryWriter: cannot handle BoWType " << token->getType() );
603 const boost::shared_ptr< BoWToken > token)
const
605 m_d->writeSimpleToken(file, token);
608void BoWBinaryWriterPrivate::writeSimpleToken(std::ostream& file,
609 const boost::shared_ptr< BoWToken > token)
const
613 LDEBUG <<
"BoWBinaryWriter::writeSimpleToken write lemma: " << &file << token->getLemma();
617 LDEBUG <<
"BoWBinaryWriter::writeSimpleToken write infl: " << token->getInflectedForm();
622 auto beg = token->getPosition();
623 auto end = token->getLength() + beg;
626 LDEBUG <<
"BoWBinaryWriter::writeSimpleToken shiftFrom is:";
628 if (m_shiftFrom.use_count() > 0)
631 LTRACE <<
"m_shiftFrom.use_count:" << m_shiftFrom.use_count() ;
633 beg = m_shiftFrom->correct_offset(0, beg);
634 end = m_shiftFrom->correct_offset(0, end);
638 LDEBUG <<
"BoWBinaryWriter::writeSimpleToken writen begin :" << beg-1;
639 LDEBUG <<
"BoWBinaryWriter::writeSimpleToken writen length:" << end-beg;
646 const boost::shared_ptr< BoWPredicate > predicate)
const{
647 m_d->writePredicate(file, predicate);
650void BoWBinaryWriterPrivate::writePredicate(std::ostream& file,
651 const boost::shared_ptr< BoWPredicate > predicate)
const{
657 LDEBUG <<
"BoWBinaryWriter::writePredicate type" << type;
663 const QMultiMap<Common::MediaticData::EntityType, boost::shared_ptr< AbstractBoWElement > >& pRoles = predicate->roles();
665 LDEBUG <<
"BoWBinaryWriter::writePredicate nb roles" << pRoles.size();
668 for (
auto it = pRoles.begin(); it != pRoles.end(); it++)
672 writeBoWToken(file,it.value());
677void BoWBinaryWriterPrivate::writeComplexTokenParts(std::ostream& file,
678 const boost::shared_ptr< BoWComplexToken > token)
const
685 const std::deque<BoWComplexToken::Part>& parts=token->getParts();
687 LDEBUG <<
"BoWBinaryWriter::writeComplexTokenParts nb parts is " << parts.size();
691 for (uint64_t i(0); i<parts.size(); i++) {
692 writeBoWRelation(file,parts[i].get<0>());
695 LDEBUG <<
"BoWBinaryWriter::writeComplexTokenParts writing";
697 file.write((
char*) &(isInList),
sizeof(
bool));
698 writeBoWToken(file,parts[i].getBoWToken());
702void BoWBinaryWriterPrivate::writeNamedEntityProperties(std::ostream& file,
703 const boost::shared_ptr< BoWNamedEntity > ne)
const
712 const std::map<std::string, LimaString>& features=ne->getFeatures();
714 for (std::map<std::string, LimaString>::const_iterator
715 it=features.begin(), it_end=features.end(); it!=it_end; it++) {
721void BoWBinaryWriterPrivate::writeBoWRelation(std::ostream& file,
722 boost::shared_ptr< BoWRelation > rel)
const
724 uint64_t hasRelation=(rel!=0);
727 LDEBUG <<
"BoWBinaryWriter::writeBoWRelation: write hasRelation" << hasRelation;
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
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 closeSBoWNode()=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)
void readHeader(std::istream &file)
BoWFileType getFileType() const
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)
virtual ~BoWBinaryReader()
std::string getFileTypeString() const
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 >())
virtual ~BoWBinaryWriter()
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.
virtual void clear() override
This class represents a list of elements, that are pointers on polymmorphic tokens that can be simple...
This class contains the representation of an element of the bag of words.
static std::ostream & encodeToBinary(std::ostream &os, const LinguisticCode &value)
static LinguisticCode decodeFromBinary(std::istream &is)
static const MediaticData & single()
const singleton accessor
static MediaticData & changeable()
singleton accessor
std::string getBlocTypeString(BoWBlocType blocType)
LIMA_BOW_EXPORT uint8_t toInt(const BoWType &bt)
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
@ DOCUMENT_PROPERTIES_BLOC
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