Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 901994f4 authored by Volker Krause's avatar Volker Krause

Try harder to separate postal codes and city names

This matters specifically as we use the city names to name trip groups.
parent c33a0423
......@@ -38,6 +38,7 @@ private Q_SLOTS:
QTest::addColumn<QString>("terminalName");
QTest::newRow("empty") << QString() << QString() << QString();
QTest::newRow("no terminal") << s("Paris Charles de Gaulle") << s("Paris Charles de Gaulle") << s("");
QTest::newRow("CDG 1") << s("PARIS, FR (CHARLES DE GAULLE), TERMINAL 2E") << s("PARIS, FR (CHARLES DE GAULLE)") << s("2E");
QTest::newRow("CDG 2") << s("Paris Charles de Gaulle (Terminal 2D)") << s("Paris Charles de Gaulle") << s("2D");
QTest::newRow("LHR") << s("London/Heathrow-Terminal 2") << s("London/Heathrow") << s("2");
......@@ -71,6 +72,36 @@ private Q_SLOTS:
const auto out2 = ExtractorUtil::extractTerminals(out);
QCOMPARE(out, out2);
}
void testPostalCodeExtraction_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("city");
QTest::addColumn<QString>("postalCode");
QTest::newRow("empty") << QString() << QString() << QString();
QTest::newRow("no code") << s("PARIS") << s("PARIS") << s("");
QTest::newRow("prefix 1") << s("1060 Brussels") << s("Brussels") << s("1060");
QTest::newRow("prefix 2") << s("171060 Brussels") << s("Brussels") << s("171060");
QTest::newRow("prefix 3") << s("75012 Paris Some Suffix") << s("Paris Some Suffix") << s("75012");
}
void testPostalCodeExtraction()
{
QFETCH(QString, input);
QFETCH(QString, city);
QFETCH(QString, postalCode);
PostalAddress a;
a.setAddressLocality(input);
const auto out = ExtractorUtil::extractPostalCode(a);
QCOMPARE(out.addressLocality(), city);
QCOMPARE(out.postalCode(), postalCode);
const auto out2 = ExtractorUtil::extractPostalCode(out);
QCOMPARE(out, out2);
}
};
QTEST_GUILESS_MAIN(ExtractorUtilTest)
......
......@@ -561,6 +561,7 @@ template<typename T> T ExtractorPostprocessorPrivate::processPlace(T place) cons
addr.setAddressLocality(addr.addressLocality().simplified());
addr.setAddressRegion(addr.addressRegion().simplified());
addr = ExtractorUtil::extractPostalCode(addr);
place.setAddress(addr);
return place;
}
......
......@@ -18,6 +18,7 @@
#include "extractorutil.h"
#include <KItinerary/Flight>
#include <KItinerary/Place>
#include <QDebug>
#include <QRegularExpression>
......@@ -58,17 +59,15 @@ static std::tuple<QString, QString> splitAirportName(const QString &name)
return std::make_tuple(name, QString());
}
Flight ExtractorUtil::extractTerminals(const Flight &flight)
Flight ExtractorUtil::extractTerminals(Flight flight)
{
Flight result(flight);
if (flight.departureTerminal().isEmpty()) {
auto a = flight.departureAirport();
QString name, terminal;
std::tie(name, terminal) = splitAirportName(a.name());
a.setName(name);
result.setDepartureAirport(a);
result.setDepartureTerminal(terminal);
flight.setDepartureAirport(a);
flight.setDepartureTerminal(terminal);
}
if (flight.arrivalTerminal().isEmpty()) {
......@@ -76,9 +75,34 @@ Flight ExtractorUtil::extractTerminals(const Flight &flight)
QString name, terminal;
std::tie(name, terminal) = splitAirportName(a.name());
a.setName(name);
result.setArrivalAirport(a);
result.setArrivalTerminal(terminal);
flight.setArrivalAirport(a);
flight.setArrivalTerminal(terminal);
}
return flight;
}
PostalAddress ExtractorUtil::extractPostalCode(PostalAddress addr)
{
if (!addr.postalCode().isEmpty() || addr.addressLocality().isEmpty()) {
return addr;
}
// ### this so far only covers the typical European numerical prefix case, we probably want
// something for alpha-numeric and suffix cases too, if necessary we can also make this
// conditional on addr.addressCountry()
static QRegularExpression patterns[] = {
QRegularExpression(QStringLiteral("^(\\d{4,8}) (.*)$"), QRegularExpression::CaseInsensitiveOption),
};
for (const auto &re : patterns) {
const auto match = re.match(addr.addressLocality());
if (match.hasMatch()) {
addr.setAddressLocality(match.captured(2));
addr.setPostalCode(match.captured(1));
break;
}
}
return result;
return addr;
}
......@@ -23,6 +23,7 @@
namespace KItinerary {
class Flight;
class PostalAddress;
/** Data extraction utility functions. */
namespace ExtractorUtil
......@@ -30,7 +31,12 @@ namespace ExtractorUtil
/** Move terminal indications from airport names to the correct fields.
* @internal Only exported for unit tests.
*/
KITINERARY_EXPORT Flight extractTerminals(const Flight &flight);
KITINERARY_EXPORT Flight extractTerminals(Flight flight);
/** Try to extract postal codes included in the city name field.
* @internal Only exported for unit tests.
*/
KITINERARY_EXPORT PostalAddress extractPostalCode(PostalAddress addr);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment