Akonadi Search

xapiantermgenerator.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8#include "xapiantermgenerator.h"
9
10#include "akonadi_search_xapian_debug.h"
11#include <QTextBoundaryFinder>
12
13using namespace Akonadi::Search;
14
15XapianTermGenerator::XapianTermGenerator(Xapian::Document *doc)
16 : m_doc(doc)
17{
18 if (doc) {
19 m_termGen.set_document(*doc);
20 }
21}
22
23void XapianTermGenerator::indexText(const QString &text)
24{
25 indexText(text, QString());
26}
27
28void XapianTermGenerator::setDocument(Xapian::Document *doc)
29{
30 m_doc = doc;
31}
32
33QStringList XapianTermGenerator::termList(const QString &text)
34{
35 int start = 0;
36 int end = 0;
37
40 for (; bf.position() != -1; bf.toNextBoundary()) {
41 if (bf.boundaryReasons() & QTextBoundaryFinder::StartOfItem) {
42 start = bf.position();
43 continue;
44 } else if (bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) {
45 end = bf.position();
46
47 QString str = text.mid(start, end - start);
48
49 // Get the string ready for saving
50 str = str.toLower();
51
52 // Remove all accents
53 const QString denormalized = str.normalized(QString::NormalizationForm_KD);
54
55 QString cleanString;
56 cleanString.reserve(denormalized.size());
57 for (const QChar &ch : denormalized) {
58 const auto cat = ch.category();
60 cleanString.append(ch);
61 }
62 }
63
66 }
67 }
68
69 return list;
70}
71
72void XapianTermGenerator::indexText(const QString &text, const QString &prefix, int wdfInc)
73{
74 const QByteArray par = prefix.toUtf8();
75 const QByteArray ta = text.toUtf8();
76 m_termGen.index_text(ta.constData(), wdfInc, par.constData());
77
78 const QStringList terms = termList(text);
79 for (const QString &term : terms) {
80 const QByteArray arr = term.toUtf8();
81
82 const QByteArray finalArr = par + arr;
83 const std::string stdString(finalArr.constData(), finalArr.size());
84 m_doc->add_posting(stdString, m_position, wdfInc);
85
86 m_position++;
87 }
88}
89
90int XapianTermGenerator::position() const
91{
92 return m_position;
93}
94
95void XapianTermGenerator::setPosition(int position)
96{
97 m_position = position;
98}
Q_SCRIPTABLE Q_NOREPLY void start()
Akonadi search infrastructure.
Definition core/query.h:21
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & end()
const char * constData() const const
qsizetype size() const const
Mark_NonSpacing
NormalizationForm_KD
QString & append(QChar ch)
QString mid(qsizetype position, qsizetype n) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
void reserve(qsizetype size)
qsizetype size() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
QByteArray toUtf8() const const
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.