KItinerary

jsapi/extractorengine.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "extractorengine.h"
7#include "logging.h"
8
9#include "text/pricefinder_p.h"
10
11#include <KItinerary/ExtractorDocumentNodeFactory>
12
13#include <QJSValue>
14#include <QJSValueIterator>
15#include <QScopeGuard>
16
17using namespace KItinerary;
18
19static constexpr inline auto RecursionDepthLimit = 10;
20
21JsApi::ExtractorEngine::ExtractorEngine(QObject *parent)
22 : QObject(parent)
23{
24}
25
26JsApi::ExtractorEngine::~ExtractorEngine() = default;
27
28void JsApi::ExtractorEngine::setEngine(KItinerary::ExtractorEngine *engine)
29{
30 m_engine = engine;
31}
32
33void JsApi::ExtractorEngine::setCurrentNode(const ExtractorDocumentNode& node)
34{
35 m_currentNode = node;
36}
37
38void JsApi::ExtractorEngine::clear()
39{
40 m_currentNode = {};
41}
42
44{
45 if (m_recursionDepth > RecursionDepthLimit) {
46 qCWarning(Log) << "Recursion depth limit reached, aborting";
47 return {};
48 }
49
50 const auto preHints = m_engine->hints();
51 const auto prevNode = m_currentNode;
52
54 auto node = m_engine->documentNodeFactory()->createNode(data);
55 m_currentNode.appendChild(node);
56
57 ++m_recursionDepth;
58 m_engine->processNode(node);
59 --m_recursionDepth;
60
61 m_engine->setHints(preHints);
62 m_currentNode = prevNode;
63
64 return node;
65}
66
68{
69 if (m_recursionDepth > RecursionDepthLimit) {
70 qCWarning(Log) << "Recursion depth limit reached, aborting";
71 return {};
72 }
73
74 const auto preHints = m_engine->hints();
75 const auto prevNode = m_currentNode;
76
78 auto node = m_engine->documentNodeFactory()->createNode(content, mimeType);
79 m_currentNode.appendChild(node);
80
81 ++m_recursionDepth;
82 m_engine->processNode(node);
83 --m_recursionDepth;
84
85 m_engine->setHints(preHints);
86 m_currentNode = prevNode;
87
88 return node;
89}
90
91static void applyPrice(const PriceFinder::Result &price, QJSValue obj)
92{
93 if (!obj.isObject()) {
94 return;
95 }
96 obj.setProperty(QStringLiteral("totalPrice"), price.value);
97 obj.setProperty(QStringLiteral("priceCurrency"), price.currency);
98}
99
101{
102 PriceFinder finder;
103 const auto price = finder.findHighest(text);
104 if (!price.hasResult()) {
105 return;
106 }
107
108 if (result.isArray()) {
109 QJSValueIterator it(result);
110 while (it.hasNext()) {
111 it.next();
112 applyPrice(price, it.value());
113 }
114 } else {
115 applyPrice(price, result);
116 }
117}
118
119#include "moc_extractorengine.cpp"
A node in the extracted document object tree.
void appendChild(ExtractorDocumentNode &child)
Add another child node.
Semantic data extraction engine.
@ ExtractFullPageRasterImages
perform expensive image processing on (PDF) documents containing full page raster images
Q_INVOKABLE void extractPrice(const QString &text, QJSValue result) const
Run the generic price information extractor to text and apply the found price value and currency to r...
Q_INVOKABLE KItinerary::ExtractorDocumentNode extract(const QByteArray &data)
Run the extractor on data.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
bool isArray() const const
bool isObject() const const
void setProperty(const QString &name, const QJSValue &value)
bool hasNext() const const
QJSValue value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.