50 std::ostream& m_outputStream;
52 uint64_t m_currentTokId;
56 Lima::MediaId m_language;
61 void setSpaces(
const std::string& s);
66 void writeBoWTokenList(
const BoWText* text,
67 const bool useIterator,
68 const bool useIndexIterator);
71 template<
typename PropertyType>
72 void writeProperty(
const std::string& name,
73 const std::string& type,
74 const PropertyType& value);
78 std::string xmlString(
const std::string& str)
const;
81 void incIndent() { m_spaces+=
" "; }
82 void decIndent() { m_spaces.erase(m_spaces.size()-2); }
84 void setLanguage(
const Lima::MediaId lang);
85 void setLanguage(
const std::string& lang);
91BoWXMLWriterPrivate::BoWXMLWriterPrivate(std::ostream& os, int32_t offset):
100BoWXMLWriterPrivate::~BoWXMLWriterPrivate()
124 m_d->m_outputStream <<
"<?xml-stylesheet type=\"text/xsl\" href=\"bow.xslt\"?>" << endl;
125 m_d->m_outputStream <<
"<bowDocuments>" << endl;
129 m_d->m_outputStream <<
"</bowDocuments>" << endl;
134 const std::string& elementName ) {
135 m_d->m_outputStream << m_d->m_spaces <<
"<hierarchy elementName=\"" << elementName <<
"\">" << std::endl;
141 const std::string& elementName ) {
142 m_d->m_currentTokId=0;
143 m_d->m_outputStream << m_d->m_spaces <<
"<hierarchy elementName=\"" << elementName <<
"\" indexingNode=\"yes\">" << std::endl;
148 m_d->m_outputStream <<m_d->m_spaces <<
"</hierarchy>" << std::endl;
153 bool useIndexIterator) {
156 LDEBUG <<
"BoWXMLWriter::processSBoWText";
160 m_d->writeBoWTokenList(boWText,useIterator,useIndexIterator);
166 m_d->writeGenericDocumentProperties(properties);
171 const bool useIterator,
172 const bool useIndexIterator) {
176 m_d->m_outputStream <<
"<bowText>" << std::endl;
178 m_d->writeBoWTokenList(text,useIterator,useIndexIterator);
180 m_d->m_outputStream <<
"</bowText>" << std::endl;
184 m_d->writeBoWToken(token);
188template<
typename PropertyType>
189void BoWXMLWriterPrivate::writeProperty(
const std::string& name,
190 const std::string& type,
191 const PropertyType& value) {
192 m_outputStream <<m_spaces <<
"<property name=\"" << name
193 <<
"\" type=\""<< type
194 <<
"\" value=\"" << value
195 <<
"\"/>" << std::endl;
198void BoWXMLWriterPrivate::writeGenericDocumentProperties(
201 m_outputStream <<m_spaces <<
"<properties>" << std::endl;
208 for( IntIter it = ret.first ; it != ret.second ; it++ ) {
209 std::pair<std::string,uint64_t> elmt = *it;
210 m_outputStream <<m_spaces <<
"<property name=\"" << elmt.first <<
"\""
212 <<
" value=\"" << elmt.second <<
"\"/>"
219 for( StringIter it = ret.first ; it != ret.second ; it++ ) {
220 std::pair<std::string,std::string> elmt = *it;
221 m_outputStream <<m_spaces <<
"<property name=\"" << elmt.first <<
"\""
222 <<
" type=\"string\""
223 <<
" value=\"" << xmlString(elmt.second) <<
"\"/>"
230 for( DateIter it = ret.first ; it != ret.second ; it++ ) {
231 std::pair<std::string,QDate> elmt = *it;
232 m_outputStream <<m_spaces <<
"<property name=\"" << elmt.first <<
"\""
234 <<
" value=\"" << elmt.second.toString(
"yyyyMMdd").toUtf8().data() <<
"\"/>"
241 for( DateIntervIter it = ret.first ; it != ret.second ; it++ ) {
242 std::pair<std::string,std::pair<QDate,QDate> > elmt = *it;
243 m_outputStream <<m_spaces <<
"<property name=\"" << elmt.first <<
"\""
244 <<
" type=\"dateRange\""
245 <<
" valueBegin=\"" << (elmt.second).first.toString(
"yyyyMMdd").toUtf8().data() <<
"\""
246 <<
" valueEnd=\"" << (elmt.second).second.toString(
"yyyyMMdd").toUtf8().data()
247 <<
"\"/>" << std::endl;
250 std::pair<Misc::MultiValuedPropertyIterator<std::string>,
251 Misc::MultiValuedPropertyIterator<std::string> > svnit =
255 for( ; svnit.first != svnit.second; (svnit.first)++ ) {
256 std::string propName = *(svnit.first);
258 for( ; svit.first != svit.second ; (svit.first)++ ) {
259 m_outputStream <<m_spaces <<
"<property name=\"" << propName <<
"\""
260 <<
" type=\"multString\""
261 <<
" value=\"" << xmlString(*(svit.first)) <<
"\"/>"
265 std::pair<Misc::MultiValuedPropertyIterator<std::pair<std::string,float> >,
266 Misc::MultiValuedPropertyIterator<std::pair<std::string,float> > > swnit =
270 for( ; swnit.first != swnit.second; (swnit.first)++ ) {
271 std::string propName = *(swnit.first);
273 for( ; svit.first != svit.second ; (svit.first)++ ) {
274 m_outputStream <<m_spaces <<
"<property name=\"" << propName <<
"\""
275 <<
" type=\"multWeightedId\""
276 <<
"\" value=\"" << xmlString((*(svit.first)).first)
277 <<
"\" weight=\"" << (*(svit.first)).second <<
"\"/>"
283 m_outputStream <<m_spaces <<
"</properties>" << std::endl;
286void BoWXMLWriterPrivate::writePredicateRoles(
const BoWPredicate* term)
288 m_outputStream <<m_spaces <<
"<roles>" << std::endl;
290 for (
auto it = term->roles().begin(); it != term->roles().end(); it++)
292 m_outputStream <<m_spaces <<
"<role type=\""
294 <<
"\" >" << std::endl;
296 writeBoWToken(&*it.value());
298 m_outputStream <<m_spaces <<
"</role>" << std::endl;
301 m_outputStream <<m_spaces <<
"</roles>" << std::endl;
305void BoWXMLWriterPrivate::writeBoWTokenList(
307 const bool useIterator,
308 const bool useIndexIterator) {
310 m_outputStream <<m_spaces <<
"<tokens>" << std::endl;
314 BoWTokenIterator it(*text);
315 while (! it.isAtEnd())
317 writeBoWToken( &*it.getElement());
321 else if (useIndexIterator)
323 IndexElementIterator it(*text);
324 while (! it.isAtEnd())
326 writeIndexElement(it.getElement());
332 for (
const auto& tok: *text) {
333 writeBoWToken( &*tok);
337 m_outputStream <<m_spaces <<
"</tokens>" << std::endl;
340void BoWXMLWriterPrivate::writeIndexElement(
341 const IndexElement& element) {
342 m_outputStream << m_spaces <<
"<term "
343 <<
"id=\"" << element.getId() <<
"\"";
344 if (element.empty()) {
345 m_outputStream <<
"/>" << endl;
348 if (element.isSimpleTerm()) {
352 cat =
static_cast<const MediaticData::LanguageData&
>(
MediaticData::MediaticData::single().
mediaData(m_language)).getPropertyCodeManager().getPropertyManager(
"MACRO").getPropertySymbolicValue(element.getCategory());
357 LERROR <<
"BoWXMLWriterPrivate::writeIndexElement Catched MediaNotInitialized:" << e.
what();
360 <<
"\" category=\"" << cat
361 <<
"\" position=\"" << element.getPosition() + m_offset
362 <<
"\" length=\"" << element.getLength() <<
"\"";
363 if (element.isNamedEntity()) {
370 m_outputStream <<
"/>" << endl;
375 if (element.isNamedEntity()) {
382 m_outputStream <<
">" << endl
383 << m_spaces <<
" <structure>" << endl;
385 for (uint64_t i(0),size=element.getStructure().size(); i<size; i++) {
386 m_outputStream << m_spaces <<
" <termRef id=\""
387 << element.getStructure()[i]
388 <<
"\" rel=\"" << element.getRelations()[i]
392 m_outputStream << m_spaces <<
" </structure>" << endl
393 << m_spaces <<
"</term>" << endl;
396void BoWXMLWriterPrivate::writeBoWRelation(
const BoWRelation* relation) {
397 m_outputStream <<m_spaces <<
"<bowRelation "
400 <<
"\" type=\"" << relation->getType() <<
"\"/>"
404void BoWXMLWriterPrivate::writeBoWToken(
405 const BagOfWords::AbstractBoWElement* token ) {
407 const BoWToken* tok = 0;
408 switch(token->getType()) {
410 tok =
static_cast<const BoWToken*
>(token);
411 std::string cat =
static_cast<const MediaticData::LanguageData&
>(
MediaticData::MediaticData::single().
mediaData(m_language)).getPropertyCodeManager().getPropertyManager(
"MACRO").getPropertySymbolicValue(tok->getCategory());
413 m_outputStream <<m_spaces <<
"<bowToken "
414 <<
"id=\"" << m_currentTokId
416 <<
"\" category=\"" << cat
417 <<
"\" position=\"" << tok->getPosition() + m_offset
418 <<
"\" length=\"" << tok->getLength() <<
"\""
419 <<
"/>" << std::endl;
423 const BoWPredicate* term=
static_cast<const BoWPredicate*
>(token);
424 m_outputStream <<m_spaces <<
"<bowPredicate "
425 <<
"id=\"" << m_currentTokId
427 <<
"\" position=\"" << term->getPosition() + m_offset
428 <<
"\" length=\"" << term->getLength() <<
"\""
431 writePredicateRoles(term);
433 m_outputStream <<m_spaces <<
"</bowPredicate>" << std::endl;
437 const BoWTerm* term=
static_cast<const BoWTerm*
>(token);
438 std::string cat =
static_cast<const MediaticData::LanguageData&
>(
MediaticData::MediaticData::single().
mediaData(m_language)).getPropertyCodeManager().getPropertyManager(
"MACRO").getPropertySymbolicValue(term->getCategory());
440 m_outputStream <<m_spaces <<
"<bowTerm "
441 <<
"id=\"" << m_currentTokId
443 <<
"\" category=\"" << cat
444 <<
"\" position=\"" << term->getPosition() + m_offset
445 <<
"\" length=\"" << term->getLength() <<
"\""
448 writeComplexTokenParts( term);
450 m_outputStream <<m_spaces <<
"</bowTerm>" << std::endl;
454 const BoWNamedEntity* ne=
static_cast<const BoWNamedEntity*
>(token);
455 std::string cat =
static_cast<const MediaticData::LanguageData&
>(
MediaticData::MediaticData::single().
mediaData(m_language)).getPropertyCodeManager().getPropertyManager(
"MACRO").getPropertySymbolicValue(ne->getCategory());
457 m_outputStream <<m_spaces <<
"<bowNamedEntity "
458 <<
"id=\"" << m_currentTokId
460 <<
"\" category=\"" << cat
461 <<
"\" position=\"" << ne->getPosition() + m_offset
462 <<
"\" length=\"" << ne->getLength()
468 writeComplexTokenParts( ne);
469 for (std::map<std::string, LimaString>::const_iterator
470 feature=ne->getFeatures().begin();
471 feature!=ne->getFeatures().end(); feature++) {
472 m_outputStream <<m_spaces <<
"<feature name=\"" << (*feature).first
475 <<
"\"/>" <<std::endl;
478 m_outputStream <<m_spaces <<
"</bowNamedEntity>" << std::endl;
483 LERROR <<
"Invalid BoW token: no type token";
487void BoWXMLWriterPrivate::writeComplexTokenParts(
const BoWComplexToken* token) {
488 if (token->getParts().empty()) {
491 m_outputStream <<m_spaces <<
"<parts head=\""<< token->getHead() <<
"\">" << std::endl;
493 for (std::deque<BoWComplexToken::Part>::const_iterator
494 part=token->getParts().begin();
495 part!=token->getParts().end(); part++)
499 if ((*part).getBoWRelation()!=0)
501 writeBoWRelation( &*(*part).getBoWRelation());
503 writeBoWToken( &*(*part).getBoWToken());
509 m_outputStream <<m_spaces <<
"</parts>" << std::endl;
513std::string BoWXMLWriterPrivate::xmlString(
const std::string& s)
const
515 static char charsToEncode[4]={
'&',
'"',
'<',
'>'};
516 static string xmlEntities[4]={
"&",
""",
"<",
">"};
520 for (uint64_t i=0; i<4; i++) {
525 string::size_type prev=0;
526 string::size_type k=str.find(charsToEncode[i],prev);
527 while (k!=string::npos) {
528 str.replace(k,1,xmlEntities[i]);
530 k=str.find(charsToEncode[i],prev);
536void BoWXMLWriterPrivate::setSpaces(
const std::string& s)
548 m_d->setLanguage(lang);
551void BoWXMLWriterPrivate::setLanguage(
const Lima::MediaId lang)
558 m_d->setLanguage(lang);
561void BoWXMLWriterPrivate::setLanguage(
const std::string& lang)
570 m_d->writeIndexElement(element);
This class is the abstract base class of all elements that can be stored in a BoWText.
This is a complex token for an index.
This is a BoW element used to represent a predicate (n-ary relation, template or semantic frame).
This class is used to type the relation between a part and its enclosing complex token.
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.
void writeBoWDocumentsFooter()
void writeBoWText(const BoWText *document, const bool useIterator, const bool useIndexIterator)
void setSpaces(const std::string &s)
void processSBoWText(const BoWText *boWText, bool useIterators, bool useIndexIterator) override
void openSBoWIndexingNode(const Lima::Common::Misc::GenericDocumentProperties *properties, const std::string &elementName) override
void openSBoWNode(const Lima::Common::Misc::GenericDocumentProperties *properties, const std::string &elementName) override
void writeBoWDocumentsHeader()
void setLanguage(const Lima::MediaId lang)
void closeSBoWNode() override
void writeIndexElement(const IndexElement &element)
void processProperties(const Misc::GenericDocumentProperties *properties, bool useIterators, bool useIndexIterator) override
BoWXMLWriter(std::ostream &os, int32_t offset=0)
void writeBoWToken(const BoWToken *token)
Represent an element of an index If it is a predicate, its simple term is "PredicateElement" and its ...
std::pair< DateIntervalPropertiesIterator, DateIntervalPropertiesIterator > getDateIntervalProperties() const
std::pair< StringPropMultIter, StringPropMultIter > getMultipleStringPropValue(std::string propName) const
Read access functions for "multiple-valued" property propName Return a pair of iterator (begin,...
std::pair< DatePropertiesIterator, DatePropertiesIterator > getDateProperties() const
std::pair< MultiValuedPropertyIterator< std::pair< std::string, float > >, MultiValuedPropertyIterator< std::pair< std::string, float > > > getWeightedPropPropertyNames() const
std::pair< MultiValuedPropertyIterator< std::string >, MultiValuedPropertyIterator< std::string > > getStringPropertyNames() const
Read access functions for all "multiple valued" properties return an iterator of names Types may be a...
std::pair< IntPropertiesIterator, IntPropertiesIterator > getIntProperties() const
std::map< std::string, std::string >::const_iterator StringPropertiesIterator
std::map< std::string, std::pair< QDate, QDate > >::const_iterator DateIntervalPropertiesIterator
std::map< std::string, QDate >::const_iterator DatePropertiesIterator
std::pair< StringPropertiesIterator, StringPropertiesIterator > getStringProperties() const
std::pair< WeightedPropMultIter, WeightedPropMultIter > getMultipleWeightedPropValue(std::string propName) const
std::map< std::string, uint64_t >::const_iterator IntPropertiesIterator
Read access functions for all "only once valued" properties return an iterator Types may be any of (u...
virtual const char * what() const override
static const MediaticData & single()
const singleton accessor
@ 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
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.