KItinerary

genericpriceextractorhelper.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "genericpriceextractorhelper_p.h"
7
8#include "text/pricefinder_p.h"
9
10#include <KItinerary/ExtractorDocumentNode>
11#include <KItinerary/ExtractorResult>
12#include <KItinerary/JsonLdDocument>
13#include <KItinerary/PriceUtil>
14#include <KItinerary/Reservation>
15#include <KItinerary/Ticket>
16
17using namespace KItinerary;
18
19static bool isApplicableForPrice(const QVariant &res)
20{
22 return false;
23 }
24
25 // marker for extractor scripts to skip generic extraction
26 const auto currency = JsonLdDocument::readProperty(res, "priceCurrency").toString();
27 if (currency.isEmpty() && !currency.isNull()) {
28 return false;
29 }
30
32 const auto r = JsonLd::convert<Reservation>(res);
33 if (r.reservationStatus() != Reservation::ReservationConfirmed) { // ignore cancellations
34 return false;
35 }
36 }
37
38 if (JsonLd::isA<FlightReservation>(res) && !res.value<FlightReservation>().reservedTicket().value<Ticket>().ticketToken().isEmpty()) { // ignore flight boarding passes
39 return false;
40 }
41
42 if (JsonLd::isA<RentalCarReservation>(res)) { // car rental reservations are full of expensive insurance scam
43 return false;
44 }
45
46 return true;
47}
48
49void GenericPriceExtractorHelper::postExtract(const QString &text, ExtractorDocumentNode &node)
50{
51 if (node.result().isEmpty()) {
52 return;
53 }
54
55 PriceFinder priceFinder;
56 std::vector<PriceFinder::Result> prices;
57 priceFinder.findAll(text, prices);
58 if (prices.empty() || !priceFinder.isSingleCurrency(prices)) {
59 return;
60 }
61
62 auto results = node.result().result();
63 if (std::none_of(results.begin(), results.end(), isApplicableForPrice)) {
64 return;
65 }
66
67 // ambigious: could be one price for each reservation or the same total for all
68 if (results.size() > 1 && prices.size() > 1) {
69 return;
70 }
71
72 const auto price = priceFinder.highest(prices);
73 if (!price.hasResult()) {
74 return;
75 }
76
77 for (auto &r : results) {
78 if (!isApplicableForPrice(r)) {
79 continue;
80 }
81 PriceUtil::setPrice(r, price.value, price.currency);
82 }
83 node.setResult(std::move(results));
84}
A node in the extracted document object tree.
QJsonArray result
Result access for QJSEngine.
void setResult(ExtractorResult &&result)
Replace the existing results by result.
A flight reservation.
Definition reservation.h:90
static QVariant readProperty(const QVariant &obj, const char *name)
Read property name on object obj.
static Q_INVOKABLE bool canHavePrice(const QVariant &item)
Returns true if item can have price/currency information.
Definition priceutil.cpp:33
static Q_INVOKABLE void setPrice(QVariant &item, double price, const QString &currency)
Sets price and currency on item.
Definition priceutil.cpp:66
static Q_INVOKABLE bool hasPrice(const QVariant &item)
Returns true if item has valid price and currency information.
Definition priceutil.cpp:16
A booked ticket.
Definition ticket.h:41
QString ticketToken
The raw ticket token string.
Definition ticket.h:50
bool isA(const QVariant &value)
Returns true if value is of type T.
Definition datatypes.h:24
bool canConvert(const QVariant &value)
Checks if the given value can be up-cast to T.
Definition datatypes.h:31
T convert(const QVariant &value)
Up-cast value to T.
Definition datatypes.h:47
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
bool isEmpty() const const
bool isEmpty() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.