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