0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team.
|
|
4 |
** All rights reserved.
|
|
5 |
**
|
|
6 |
** Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
7 |
** All rights reserved.
|
|
8 |
**
|
|
9 |
** This file may be used under the terms of the GNU Lesser General Public
|
|
10 |
** License version 2.1 as published by the Free Software Foundation and
|
|
11 |
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
12 |
** Please review the following information to ensure the GNU Lesser General
|
|
13 |
** Public License version 2.1 requirements will be met:
|
|
14 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
15 |
**
|
|
16 |
****************************************************************************/
|
|
17 |
|
|
18 |
#include "qanalyzer_p.h"
|
|
19 |
#include "qclucene_global_p.h"
|
|
20 |
|
|
21 |
#include <CLucene.h>
|
|
22 |
#include <CLucene/analysis/AnalysisHeader.h>
|
|
23 |
|
|
24 |
QT_BEGIN_NAMESPACE
|
|
25 |
|
|
26 |
QCLuceneAnalyzerPrivate::QCLuceneAnalyzerPrivate()
|
|
27 |
: QSharedData()
|
|
28 |
{
|
|
29 |
analyzer = 0;
|
|
30 |
deleteCLuceneAnalyzer = true;
|
|
31 |
}
|
|
32 |
|
|
33 |
QCLuceneAnalyzerPrivate::QCLuceneAnalyzerPrivate(const QCLuceneAnalyzerPrivate &other)
|
|
34 |
: QSharedData()
|
|
35 |
{
|
|
36 |
analyzer = _CL_POINTER(other.analyzer);
|
|
37 |
deleteCLuceneAnalyzer = other.deleteCLuceneAnalyzer;
|
|
38 |
}
|
|
39 |
|
|
40 |
QCLuceneAnalyzerPrivate::~QCLuceneAnalyzerPrivate()
|
|
41 |
{
|
|
42 |
if (deleteCLuceneAnalyzer)
|
|
43 |
_CLDECDELETE(analyzer);
|
|
44 |
}
|
|
45 |
|
|
46 |
|
|
47 |
QCLuceneAnalyzer::QCLuceneAnalyzer()
|
|
48 |
: d(new QCLuceneAnalyzerPrivate())
|
|
49 |
{
|
|
50 |
//nothing todo, private
|
|
51 |
}
|
|
52 |
|
|
53 |
QCLuceneAnalyzer::~QCLuceneAnalyzer()
|
|
54 |
{
|
|
55 |
// nothing todo
|
|
56 |
}
|
|
57 |
|
|
58 |
qint32 QCLuceneAnalyzer::positionIncrementGap(const QString &fieldName) const
|
|
59 |
{
|
|
60 |
Q_UNUSED(fieldName);
|
|
61 |
return 0;
|
|
62 |
}
|
|
63 |
|
|
64 |
QCLuceneTokenStream QCLuceneAnalyzer::tokenStream(const QString &fieldName,
|
|
65 |
const QCLuceneReader &reader) const
|
|
66 |
{
|
|
67 |
TCHAR *fName = QStringToTChar(fieldName);
|
|
68 |
QCLuceneTokenStream tokenStream;
|
|
69 |
tokenStream.d->tokenStream = d->analyzer->tokenStream(fName, reader.d->reader);
|
|
70 |
delete [] fName;
|
|
71 |
|
|
72 |
return tokenStream;
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
QCLuceneStandardAnalyzer::QCLuceneStandardAnalyzer()
|
|
77 |
: QCLuceneAnalyzer()
|
|
78 |
{
|
|
79 |
d->analyzer = new lucene::analysis::standard::StandardAnalyzer();
|
|
80 |
}
|
|
81 |
|
|
82 |
QCLuceneStandardAnalyzer::~QCLuceneStandardAnalyzer()
|
|
83 |
{
|
|
84 |
// nothing todo
|
|
85 |
}
|
|
86 |
|
|
87 |
QCLuceneStandardAnalyzer::QCLuceneStandardAnalyzer(const QStringList &stopWords)
|
|
88 |
{
|
|
89 |
const TCHAR **tArray = new const TCHAR*[stopWords.count() +1];
|
|
90 |
|
|
91 |
for(int i = 0; i < stopWords.count(); ++i) {
|
|
92 |
TCHAR *stopWord = QStringToTChar(stopWords.at(i));
|
|
93 |
tArray[i] = STRDUP_TtoT(stopWord);
|
|
94 |
delete [] stopWord;
|
|
95 |
}
|
|
96 |
tArray[stopWords.count()] = 0;
|
|
97 |
|
|
98 |
d->analyzer = new lucene::analysis::standard::StandardAnalyzer(tArray);
|
|
99 |
}
|
|
100 |
|
|
101 |
|
|
102 |
QCLuceneWhitespaceAnalyzer::QCLuceneWhitespaceAnalyzer()
|
|
103 |
: QCLuceneAnalyzer()
|
|
104 |
{
|
|
105 |
d->analyzer = new lucene::analysis::WhitespaceAnalyzer();
|
|
106 |
}
|
|
107 |
|
|
108 |
QCLuceneWhitespaceAnalyzer::~QCLuceneWhitespaceAnalyzer()
|
|
109 |
{
|
|
110 |
// nothing todo
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
QCLuceneSimpleAnalyzer::QCLuceneSimpleAnalyzer()
|
|
115 |
: QCLuceneAnalyzer()
|
|
116 |
{
|
|
117 |
d->analyzer = new lucene::analysis::SimpleAnalyzer();
|
|
118 |
}
|
|
119 |
|
|
120 |
QCLuceneSimpleAnalyzer::~QCLuceneSimpleAnalyzer()
|
|
121 |
{
|
|
122 |
// nothing todo
|
|
123 |
}
|
|
124 |
|
|
125 |
|
|
126 |
QCLuceneStopAnalyzer::QCLuceneStopAnalyzer()
|
|
127 |
: QCLuceneAnalyzer()
|
|
128 |
{
|
|
129 |
d->analyzer = new lucene::analysis::StopAnalyzer();
|
|
130 |
}
|
|
131 |
|
|
132 |
QCLuceneStopAnalyzer::~QCLuceneStopAnalyzer()
|
|
133 |
{
|
|
134 |
// nothing todo
|
|
135 |
}
|
|
136 |
|
|
137 |
QCLuceneStopAnalyzer::QCLuceneStopAnalyzer(const QStringList &stopWords)
|
|
138 |
: QCLuceneAnalyzer()
|
|
139 |
{
|
|
140 |
const TCHAR **tArray = new const TCHAR*[stopWords.count() +1];
|
|
141 |
|
|
142 |
for(int i = 0; i < stopWords.count(); ++i) {
|
|
143 |
TCHAR *stopWord = QStringToTChar(stopWords.at(i));
|
|
144 |
tArray[i] = STRDUP_TtoT(stopWord);
|
|
145 |
delete [] stopWord;
|
|
146 |
}
|
|
147 |
tArray[stopWords.count()] = 0;
|
|
148 |
|
|
149 |
d->analyzer = new lucene::analysis::StopAnalyzer(tArray);
|
|
150 |
}
|
|
151 |
|
|
152 |
QStringList QCLuceneStopAnalyzer::englishStopWords() const
|
|
153 |
{
|
|
154 |
QStringList stopWordList;
|
|
155 |
|
|
156 |
const TCHAR** stopWords = lucene::analysis::StopAnalyzer::ENGLISH_STOP_WORDS;
|
|
157 |
for (qint32 i = 0; stopWords[i] != 0; ++i)
|
|
158 |
stopWordList.append(TCharToQString(stopWords[i]));
|
|
159 |
|
|
160 |
return stopWordList;
|
|
161 |
}
|
|
162 |
|
|
163 |
|
|
164 |
QCLuceneKeywordAnalyzer::QCLuceneKeywordAnalyzer()
|
|
165 |
: QCLuceneAnalyzer()
|
|
166 |
{
|
|
167 |
d->analyzer = new lucene::analysis::KeywordAnalyzer();
|
|
168 |
}
|
|
169 |
|
|
170 |
QCLuceneKeywordAnalyzer::~QCLuceneKeywordAnalyzer()
|
|
171 |
{
|
|
172 |
// nothing todo
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
QCLucenePerFieldAnalyzerWrapper::QCLucenePerFieldAnalyzerWrapper(
|
|
177 |
QCLuceneAnalyzer *defaultAnalyzer)
|
|
178 |
: QCLuceneAnalyzer()
|
|
179 |
{
|
|
180 |
d->analyzer = new
|
|
181 |
lucene::analysis::PerFieldAnalyzerWrapper(defaultAnalyzer->d->analyzer);
|
|
182 |
|
|
183 |
analyzers.append(defaultAnalyzer);
|
|
184 |
defaultAnalyzer->d->deleteCLuceneAnalyzer = false;
|
|
185 |
}
|
|
186 |
|
|
187 |
QCLucenePerFieldAnalyzerWrapper::~QCLucenePerFieldAnalyzerWrapper()
|
|
188 |
{
|
|
189 |
qDeleteAll(analyzers);
|
|
190 |
}
|
|
191 |
|
|
192 |
void QCLucenePerFieldAnalyzerWrapper::addAnalyzer(const QString &fieldName,
|
|
193 |
QCLuceneAnalyzer *analyzer)
|
|
194 |
{
|
|
195 |
lucene::analysis::PerFieldAnalyzerWrapper *analyzerWrapper =
|
|
196 |
static_cast<lucene::analysis::PerFieldAnalyzerWrapper*> (d->analyzer);
|
|
197 |
|
|
198 |
if (analyzerWrapper == 0)
|
|
199 |
return;
|
|
200 |
|
|
201 |
analyzers.append(analyzer);
|
|
202 |
analyzer->d->deleteCLuceneAnalyzer = false;
|
|
203 |
|
|
204 |
TCHAR *fName = QStringToTChar(fieldName);
|
|
205 |
analyzerWrapper->addAnalyzer(fName, analyzer->d->analyzer);
|
|
206 |
delete [] fName;
|
|
207 |
}
|
|
208 |
|
|
209 |
QT_END_NAMESPACE
|