22#include <QXmlStreamReader>
45 std::ostream& output);
48 bool parse(QIODevice *device);
70 boost::shared_ptr< BoWComplexToken >
token;
87 std::map<uint64_t, boost::shared_ptr< BoWToken > >
m_refMap;
90 uint64_t
getIntAttribute(
const QXmlStreamReader& attributes,
const char* name)
const;
91 float getFloatAttribute(
const QXmlStreamReader& attributes,
const char* name)
const;
92 std::string
getStringAttribute(
const QXmlStreamReader& attributes,
const char* name)
const;
94 QDate
getDateAttribute(
const QXmlStreamReader& attributes,
const char* name)
const;
96 bool addProperty(
const QXmlStreamReader& attributes);
109 std::ostream& output):
123 std::ostream& output):
125 m_outputStream(output),
126 m_currentBoWDocument(),
128 m_currentComplexToken(),
131 QFile file(filename.c_str());
132 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
135 QString(QLatin1String(
"Error opening %1")).arg(filename.c_str()).toUtf8().constData(),
140 throw XMLException(QString(QLatin1String(
"Error parsing %1: %2"))
141 .arg(filename.c_str())
149 LDEBUG <<
"BoWXmlReaderPrivate::parse";
151 if (
m_reader.readNextStartElement()) {
152 if (
m_reader.name() == QLatin1String(
"bowDocument"))
156 else if (
m_reader.name() == QLatin1String(
"bowText"))
162 m_reader.raiseError(QObject::tr(
"The file is not a LIMA BoW XML file."));
171 LTRACE <<
"BoWXmlReaderPrivate::readBowDocument" <<
m_reader.name();
172 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowDocument"));
176 while (
m_reader.readNextStartElement()) {
177 if (
m_reader.name() == QLatin1String(
"bowText"))
179 else if (
m_reader.name() == QLatin1String(
"hierarchy"))
182 m_reader.raiseError(QObject::tr(
"Expected a bowText or a hierarchy but got a %1.").arg(
m_reader.name()));
191 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowText"));
194 while (
m_reader.readNextStartElement()) {
195 if (
m_reader.name() == QLatin1String(
"tokens"))
198 m_reader.raiseError(QObject::tr(
"Expected a tokens but got a %1.").arg(
m_reader.name()));
205 LTRACE <<
"BoWXmlReaderPrivate::readHierarchy" <<
m_reader.name();
206 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"hierarchy"));
209 bool isIndexingNode(
false);
212 if (indexingNode ==
"yes")
214 isIndexingNode =
true;
230 std::string elementName(
"");
240 while (
m_reader.readNextStartElement())
242 if (
m_reader.name() == QLatin1String(
"bowText"))
244 else if (
m_reader.name() == QLatin1String(
"hierarchy"))
247 m_reader.raiseError(QObject::tr(
"Expected a bowText or a hierarchy but got a %1.").arg(
m_reader.name()));
263 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"property"));
271 LTRACE <<
"BoWXmlReaderPrivate::readProperties" <<
m_reader.name();
272 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"properties"));
275 while (
m_reader.readNextStartElement()) {
276 if (
m_reader.name() == QLatin1String(
"property"))
279 m_reader.raiseError(QObject::tr(
"Expected a property but got a %1.").arg(
m_reader.name()));
289 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"tokens"));
297 LERROR <<
"<tokens> found whereas no BoWText initialized";
299 while (
m_reader.readNextStartElement()) {
300 if (
m_reader.name() == QLatin1String(
"bowToken"))
302 else if (
m_reader.name() == QLatin1String(
"bowTerm"))
304 else if (
m_reader.name() == QLatin1String(
"bowNamedEntity"))
307 m_reader.raiseError(QObject::tr(
"Expected a bowToken, bowTerm or a bowNamedEntity but got a %1.")
319 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowToken"));
349 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowTerm"));
356 while (
m_reader.readNextStartElement())
358 if (
m_reader.name() == QLatin1String(
"parts"))
361 m_reader.raiseError(QObject::tr(
"Expected a parts but got a %1.").arg(
m_reader.name()));
387 LTRACE <<
"BoWXmlReaderPrivate::readBowNamedEntity" <<
m_reader.name();
388 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowNamedEntity"));
402 qts << __FILE__ <<
", line" << __LINE__
403 <<
"Unknown entity type" << typeName << this->
errorString();
407 auto ne = boost::shared_ptr< BoWNamedEntity >(
414 while (
m_reader.readNextStartElement()) {
415 if (
m_reader.name() == QLatin1String(
"parts"))
417 else if (
m_reader.name() == QLatin1String(
"feature"))
420 m_reader.raiseError(QObject::tr(
"Expected a parts or feature but got a %1.").arg(
m_reader.name()));
448 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"parts"));
453 while (
m_reader.readNextStartElement()) {
454 if (
m_reader.name() == QLatin1String(
"bowToken"))
456 else if (
m_reader.name() == QLatin1String(
"bowTerm"))
458 else if (
m_reader.name() == QLatin1String(
"bowNamedEntity"))
461 m_reader.raiseError(QObject::tr(
"Expected a bowToken, bowTerm or a bowNamedEntity but got a %1.")
469 LTRACE <<
"BoWXmlReaderPrivate::readBowTokenRef" <<
m_reader.name();
470 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"bowTokenRef"));
481 Q_ASSERT(
m_reader.isStartElement() &&
m_reader.name() == QLatin1String(
"feature"));
486 setFeature(name,value);
499 if (typName ==
"int")
503 else if (typName ==
"string")
507 else if (typName ==
"date")
511 else if (typName ==
"dateRange")
517 else if (typName ==
"multString")
521 else if (typName ==
"multWeightedId")
528 catch (std::exception& e)
531 LERROR <<
"Error reading doc infos: " << e.what();
556 auto attr = attributes.attributes().value(name).toULongLong(&ok);
560 std::ostringstream oss;
561 oss <<
"Cannot convert attribute \"" << name <<
"\"=\""
562 << attributes.attributes().value(name).toString().toStdString()
563 <<
"\" to uint64_t. " <<
errorString().toStdString();
574 auto attr = attributes.attributes().value(name).toFloat(&ok);
578 std::ostringstream oss;
579 oss <<
"Cannot convert attribute \"" << name <<
"\"=\""
580 << attributes.attributes().value(name).toString().toStdString()
596 auto strDate = attributes.attributes().value(name).toString();
598 if (strDate ==
"NotADate")
600 LERROR <<
"BoWXMLHandler::getDateAttribute read strDate \""<<strDate<<
"\"";
604 LDEBUG <<
"BoWXMLHandler::getDateAttribute read strDate \""<<strDate<<
"\" with format \"yyyyMMdd\"";
605 auto date = QDate::fromString(strDate,
"yyyyMMdd");
609 LERROR <<
"BoWXMLHandler::getDateAttribute Error trying to read date: " << strDate;
617 return attributes.attributes().value(name).toString();
623 auto errorStr = QObject::tr(
"%1, Line %2, column %3")
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
#define PROPERTYCODELOGINIT
void writeBoWText(std::ostream &file, const BoWText &bowText) const
represent a document as a Bag Of Word, with associated document properties.
virtual void clear() override
This is a complex token used to represent a named entity token.
This is a complex token used to represent a multiword term.
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.
CurrentComplexToken(boost::shared_ptr< BoWComplexToken > tok)
boost::shared_ptr< BoWComplexToken > token
QString errorString() const
Common::Misc::GenericDocumentProperties m_currentProperties
QString getQStringAttribute(const QXmlStreamReader &attributes, const char *name) const
void readBowNamedEntity()
std::string getStringAttribute(const QXmlStreamReader &attributes, const char *name) const
std::vector< CurrentComplexToken > m_currentComplexToken
uint64_t getTokenAttributes(const QXmlStreamReader &attributes, LimaString &lemma, LinguisticCode &category, uint64_t &position, uint64_t &length, uint64_t &id) const
boost::shared_ptr< BoWText > m_currentBoWText
~BoWXmlReaderPrivate()=default
BoWDocument m_currentBoWDocument
uint64_t getIntAttribute(const QXmlStreamReader &attributes, const char *name) const
float getFloatAttribute(const QXmlStreamReader &attributes, const char *name) const
QXmlStreamReader m_reader
QDate getDateAttribute(const QXmlStreamReader &attributes, const char *name) const
std::map< uint64_t, boost::shared_ptr< BoWToken > > m_refMap
std::ostream & m_outputStream
bool addProperty(const QXmlStreamReader &attributes)
bool parse(QIODevice *device)
BoWXmlReaderPrivate(const std::string &filename, std::ostream &output)
XML input/output for BoW elements.
BoWXmlReader(const std::string &filename, std::ostream &output)
NoAttributeException(const std::string &mess)
void addWeightedPropValue(const std::string &propName, const std::pair< std::string, float > &str)
void setStringValue(const std::string &propName, const std::string &val)
void write(std::ostream &file) const
void addStringValue(const std::string &propName, const std::string &str)
void setDateIntervalValue(const std::string &propName, const std::pair< QDate, QDate > &val)
void setDateValue(const std::string &propName, const QDate &val)
void setIntValue(const std::string &propName, const uint64_t &val)
The main LIMA exception class.
static LinguisticCode fromUInt(uint64_t v)
static const MediaticData & single()
const singleton accessor
Throw this exception when encountering problems with XML files opening or parsing.
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)