LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SegmentationResultsLoader.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 * @author Romaric Besancon <romaric.besancon@cea.fr>
8 * @date Tue Jan 18 2011
9 ***********************************************************************/
10
12#include "SegmentationData.h"
13
17
18#include <QFile>
19#include <QXmlStreamReader>
20
21using namespace std;
23
24namespace Lima {
25namespace LinguisticProcessing {
26
28
29
31{
32public:
34
36
37 bool parse(QIODevice *device);
38 void readResults();
39
40 QString errorString() const;
41
42 QXmlStreamReader m_reader;
43
44 QString m_graphName;
45 QString m_dataName;
46
49 uint64_t m_position;
50 uint64_t m_length;
51 QString m_type;
52
53};
54
55//***********************************************************************
56// constructors and destructors
61
66
68 m_graphName("AnalysisGraph"),
69 m_dataName("segmentationData")
70{
71}
72
73//***********************************************************************
75 Manager* manager)
76
77{
78 AnalysisLoader::init(unitConfiguration,manager);
79 try
80 {
81 m_d->m_graphName = QString::fromStdString(unitConfiguration.getParamsValueAtKey("graph"));
82 }
83 catch (Common::XMLConfigurationFiles::NoSuchParam& ) {} // keep default value
84
85 try
86 {
87 m_d->m_dataName = QString::fromStdString(unitConfiguration.getParamsValueAtKey("data"));
88 }
89 catch (Common::XMLConfigurationFiles::NoSuchParam& ) {} // keep default value
90}
91
93{
94 // get analysis graph
95 auto graph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData(m_d->m_graphName.toStdString()));
96 if (graph == nullptr)
97 {
98 LOGINIT("LP::AnalysisLoader");
99 LERROR << "no graph '" << m_d->m_graphName << "' available !";
100 return MISSING_DATA;
101 }
102
103 // get segmentation data or create new
104 auto data = analysis.getData(m_d->m_dataName.toStdString());
105 SegmentationData* segmData = nullptr;
106 if (data == nullptr)
107 {
108 segmData = new SegmentationData();
109 analysis.setData(m_d->m_dataName.toStdString(), segmData);
110 }
111 else
112 {
113 segmData = static_cast<SegmentationData*>(data.get());
114 if (segmData == nullptr)
115 {
116 LOGINIT("LP::AnalysisLoader");
117 LERROR << "data "<< data.get() <<" is not an object of class SegmentationData";
118 return MISSING_DATA;
119 }
120 }
121
122 try
123 {
124 auto filename = getInputFile(analysis);
125 QFile file(filename);
126 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
127 LIMA_EXCEPTION_SELECT_LOGINIT(LOGINIT("LP::AnalysisLoader"),
128 "Cannot open" << filename,
130 if (!m_d->parse(&file))
131 {
133 LOGINIT("LP::AnalysisLoader"),
134 "Cannot parse" << filename << m_d->errorString(),
136 }
137 }
138 catch (const XMLException& )
139 {
140 return UNKNOWN_ERROR;
141 }
142
143 return SUCCESS_ID;
144}
145
147{
148 LOGINIT("LP::AnalysisLoader");
149 LTRACE << "parse";
150 m_reader.setDevice(device);
151 readResults();
152 return !m_reader.error();
153}
154
156{
157 LOGINIT("LP::AnalysisLoader");
158 LTRACE << "SegmentationResultsLoaderPrivate::readResults";
159
160 bool ok;
161 while (m_reader.readNextStartElement())
162 {
163 if (m_reader.name() == QLatin1String("segment"))
164 {
165 LDEBUG << "SegmentationResultsLoader::XMLHandler add data " << m_type << "," << m_position << ","
166 << m_length << "," << m_graph;
167 Segment s(m_type.toStdString());
169 m_data->add(s);
170 }
171 else if (m_reader.name().toString() == "position")
172 {
173 m_position = m_reader.readElementText().toInt(&ok);
174 if (!ok)
175 {
176 m_reader.raiseError(QObject::tr("Cannot convert position value %1 to int.").arg(m_reader.text()));
177 }
178 }
179 else if (m_reader.name().toString() == "length")
180 {
181 m_length = m_reader.readElementText().toInt(&ok);
182 if (!ok)
183 {
184 m_reader.raiseError(QObject::tr("Cannot convert length value %1 to int.").arg(m_reader.text()));
185 }
186 }
187 else if (m_reader.name().toString() == "type")
188 {
189 m_type = m_reader.readElementText();
190 }
191 else
192 {
193 m_reader.skipCurrentElement();
194 }
195 }
196}
197
199{
201 auto errorStr = QObject::tr("%1, Line %2, column %3")
202 .arg(m_reader.errorString())
203 .arg(m_reader.lineNumber())
204 .arg(m_reader.columnNumber());
205 LERROR << errorStr;
206 return errorStr;
207}
208
209
210} // end namespace
211} // end namespace
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
Definition LimaCommon.h:332
#define LTRACE
Definition LimaCommon.h:156
#define LOGINIT(X)
Definition LimaCommon.h:187
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define SEGMENTATIONRESULTSLOADER_CLASSID
Defines a Factory to create Object of type Base.
#define LIMA_TEXTSEGMENTATION_EXPORT
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
std::shared_ptr< AnalysisData > getData(const QString &id)
return AnalysisData by id
void setData(const QString &id, std::shared_ptr< AnalysisData > data)
set an analysisData with the given id.
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
QString getInputFile(AnalysisContent &analysis) const
An AnalysisData containing a LinguisticGraph with a language and an id.
void setVerticesFromPositions(uint64_t posBegin, uint64_t length, LinguisticAnalysisStructure::AnalysisGraph *anagraph)
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
SimpleFactory< MediaProcessUnit, SegmentationResultsLoader > SegmentationResultsLoaderFactory(SEGMENTATIONRESULTSLOADER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ UNKNOWN_ERROR
Definition LimaCommon.h:238
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.