LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
shiftFrom.cpp
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 * @author Gaël de Chalendar <gael.de-chalendar@cea.fr>
8 * @date Wed Dec 15 2021
9 ***********************************************************************/
10
11#include "shiftFrom.h"
12#include "qtSgmlEntities.h"
13
14#include <QDebug>
15#include <QFile>
16#include <QRegularExpression>
17#include <tuple>
18
19namespace Lima {
20
22{
23public:
24 ShiftFromPrivate(const QString& xml);
25 ~ShiftFromPrivate() = default;
26
27 void build_mapping();
28
29 QMap<int, std::tuple<int, QString, QString> > m_shiftFrom;
30 QString m_xml;
31 QString m_xml_noent;
32 QMap<int, QString> m_mapping;
33
34
35};
36
37
38const QString& ShiftFrom::xml()
39{
40 return m_d->m_xml;
41}
42
43const QString& ShiftFrom::xml_noent()
44{
45 return m_d->m_xml_noent;
46}
47
54QString ShiftFrom::rebuild_text(const QString& input, int offset) const
55{
56 QString txt = input;
57// qDebug() << "Handling '" << txt << "' at offset" << offset;
58// qDebug() << "Mapping from" << offset << "to" << offset+txt.size();
59 for (auto it = m_d->m_mapping.lowerBound(offset),
60 it_end = m_d->m_mapping.upperBound(offset+txt.size());
61 it != it_end; it++)
62 {
63// qDebug() << "it:" << it.key();
64 txt = txt.replace(it.key()-offset, it.value().size(), it.value());
65 }
66 return txt;
67}
68
74int ShiftFrom::correct_offset(int offset, int indexOfToken) const
75{
76// qDebug() << "ShiftFrom::correct_offset("<<offset<<","<<indexOfToken<<")";
77// qDebug() << "ShiftFrom::correct_offset" << m_d;
78// qDebug() << "ShiftFrom::correct_offset" << m_d->m_shiftFrom.keys();
79 auto correction = (
80 m_d->m_shiftFrom.lowerBound(offset+indexOfToken-1)==m_d->m_shiftFrom.begin()
81 ?0
82 :(std::get<0>((m_d->m_shiftFrom.lowerBound(offset+indexOfToken-1)-1).value()))
83 );
84// qDebug() << "correction:" << correction;
85 auto correctedOffset = offset + correction;
86// qDebug() << "corrected offset:" << correctedOffset;
87 auto correctedPosition = correctedOffset+indexOfToken;
88// qDebug() << "corrected position:" << correctedPosition;
89// qDebug() << "";
90 return correctedPosition;
91}
92
93ShiftFrom::ShiftFrom(const QString& xml):
94 m_d(new ShiftFromPrivate(xml))
95{
96// qDebug() << "ShiftFrom::ShiftFrom" << xml;
97}
98
99
101{
102 delete m_d;
103}
104
106 m_xml(xml)
107// QMap<int, std::tuple<int, QString, QString> > buildShiftFrom(const QString& xml)
108{
109// qDebug() << "ShiftFromPrivate::ShiftFromPrivate" << m_xml;
110 QRegularExpression rx("(&([^;]*);)");
111 int shift = 0;
112
113 qsizetype indexofent = 0;
114 QRegularExpressionMatch match;
115 while ((indexofent = m_xml.indexOf(rx, indexofent, &match)) != -1)
116 {
117// qDebug() << indexofent;
118// qDebug() << rx.cap(1);
119 int indexInResolved = indexofent-shift;
120 QString entity = match.captured(1);
121 QString entityString = match.captured(2);
122// qDebug() << entity;
123 QString parsedEntity = parseEntity(entityString);
124 shift += entity.size()-parsedEntity.size();
125// qDebug() << parsedEntity;
126 m_shiftFrom.insert(indexInResolved, {shift, entity, parsedEntity});
127// qDebug() << "ShiftFromPrivate::ShiftFromPrivate indexofent:" << indexofent << "; indexInResolved:"
128// << indexInResolved << "; shift:" << shift;
129
130 indexofent += match.capturedLength();
131 }
132// qDebug() << "ShiftFromPrivate::ShiftFromPrivate shiftFrom is:" << m_shiftFrom.keys();
133
135}
136
138{
139// qDebug() << m_xml;
140 QRegularExpression rx("(&([^;]*);)");
142
143 qsizetype indexofent = 0;
144 QRegularExpressionMatch match;
145 while ((indexofent = m_xml_noent.indexOf(rx, indexofent, &match)) != -1)
146 {
147 auto entity = match.captured(1);
148 auto entityString = match.captured(2);
149 auto parsedEntity = parseEntity(entityString);
150 m_xml_noent.replace(indexofent, entity.size(),
151 QString(parsedEntity.size(),'_'));
152 m_mapping.insert(indexofent, parsedEntity);
153 indexofent += parsedEntity.size();
154 }
155// qDebug() << "xml_noent:" << m_xml_noent;
156// qDebug() << "mapping:" << m_mapping;
157}
158
159
160QDebug& operator<<(QDebug& os, const ShiftFrom& sf)
161{
162// QMap<int, std::tuple<int, QString, QString> > m_shiftFrom;
163 os << "ShiftFrom xml:" << sf.m_d->m_xml << QTENDL;
164 os << "ShiftFrom xml_noent:" << sf.m_d->m_xml_noent << QTENDL;
165 os << "ShiftFrom xml rebuilt:" << sf.rebuild_text(sf.m_d->m_xml_noent, 0) << QTENDL;
166 os << "ShiftFrom shiftFrom: {" ;
167 for (const auto& k: sf.m_d->m_shiftFrom.keys())
168 {
169 os << "{" << k << ":" << "{" << std::get<0>(sf.m_d->m_shiftFrom[k])
170 << "," << std::get<1>(sf.m_d->m_shiftFrom[k])
171 << "," << std::get<2>(sf.m_d->m_shiftFrom[k])
172 << "}" << "}";
173 }
174 os << "}" ;
175 return os;
176}
177
178
179} // end namespace
#define QTENDL
Definition LimaCommon.h:32
QMap< int, QString > m_mapping
Definition shiftFrom.cpp:32
ShiftFromPrivate(const QString &xml)
QMap< int, std::tuple< int, QString, QString > > m_shiftFrom
Definition shiftFrom.cpp:29
QString rebuild_text(const QString &input, int offset) const
Replace in input the sequences in mapping (shifted by offset)
Definition shiftFrom.cpp:54
int correct_offset(int offset, int indexOfToken) const
Return the correct absolute position of a token in its origin text when found at position indexOfToke...
Definition shiftFrom.cpp:74
const QString & xml()
Definition shiftFrom.cpp:38
const QString & xml_noent()
Definition shiftFrom.cpp:43
virtual ~ShiftFrom()
ShiftFrom(const QString &xml)
Definition shiftFrom.cpp:93
NAUTITIA.
std::ostream & operator<<(ostream &os, const Node &node)
QString parseEntity(QStringView entity)