CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_analysis_Analyzers_ 00008 #define _lucene_analysis_Analyzers_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "CLucene/util/Reader.h" 00015 #include "AnalysisHeader.h" 00016 #include "CLucene/util/Misc.h" 00017 00018 CL_NS_DEF(analysis) 00019 00020 00021 class CharTokenizer:public Tokenizer { 00022 private: 00023 int32_t offset, bufferIndex, dataLen; 00024 TCHAR buffer[LUCENE_MAX_WORD_LEN+1]; 00025 const TCHAR* ioBuffer; 00026 protected: 00027 00032 virtual bool isTokenChar(const TCHAR c) const = 0; 00033 00037 virtual TCHAR normalize(const TCHAR c) const; 00038 00039 public: 00040 CharTokenizer(CL_NS(util)::Reader* in); 00041 virtual ~CharTokenizer(){ 00042 } 00043 bool next(Token* token); 00044 }; 00045 00046 00053 class LetterTokenizer:public CharTokenizer { 00054 public: 00055 // Construct a new LetterTokenizer. 00056 LetterTokenizer(CL_NS(util)::Reader* in): 00057 CharTokenizer(in) {} 00058 00059 ~LetterTokenizer(){} 00060 protected: 00062 bool isTokenChar(const TCHAR c) const; 00063 }; 00064 00065 00066 00077 class LowerCaseTokenizer:public LetterTokenizer { 00078 public: 00080 LowerCaseTokenizer(CL_NS(util)::Reader* in): 00081 LetterTokenizer(in) {} 00082 00083 ~LowerCaseTokenizer(){} 00084 protected: 00086 TCHAR normalize(const TCHAR chr) const; 00087 }; 00088 00089 00092 class WhitespaceTokenizer: public CharTokenizer { 00093 public: 00095 WhitespaceTokenizer(CL_NS(util)::Reader* in):CharTokenizer(in) {} 00096 ~WhitespaceTokenizer(){} 00097 protected: 00100 bool isTokenChar(const TCHAR c) const; 00101 }; 00102 00103 00105 class WhitespaceAnalyzer: public Analyzer { 00106 public: 00107 TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader); 00108 ~WhitespaceAnalyzer(){} 00109 }; 00110 00112 class SimpleAnalyzer: public Analyzer { 00113 public: 00114 TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader); 00115 ~SimpleAnalyzer(){} 00116 }; 00117 00118 00119 00123 class LowerCaseFilter: public TokenFilter { 00124 public: 00125 LowerCaseFilter(TokenStream* in, bool deleteTokenStream):TokenFilter(in,deleteTokenStream) {} 00126 ~LowerCaseFilter(){} 00127 bool next(Token* token); 00128 }; 00129 00130 00134 class StopFilter: public TokenFilter { 00135 private: 00136 //bvk: i found this to work faster with a non-hash table. the number of items 00137 //in the stop table is not like to make it worth having hashing. 00138 CL_NS(util)::CLSetList<const TCHAR*>* table; 00139 public: 00140 // Constructs a filter which removes words from the input 00141 // TokenStream that are named in the array of words. 00142 StopFilter(TokenStream* in, bool deleteTokenStream, const TCHAR** stopWords); 00143 00144 ~StopFilter(){} 00145 00149 StopFilter(TokenStream* in, bool deleteTokenStream, CL_NS(util)::CLSetList<const TCHAR*>* stopTable): 00150 TokenFilter(in, deleteTokenStream), 00151 table(stopTable) 00152 {} 00153 00154 00161 static void fillStopTable(CL_NS(util)::CLSetList<const TCHAR*>* stopTable, 00162 const TCHAR** stopWords); 00163 00167 bool next(Token* token); 00168 }; 00169 00170 00171 00172 00174 class StopAnalyzer: public Analyzer { 00175 CL_NS(util)::CLSetList<const TCHAR*> stopTable; 00176 00177 public: 00179 StopAnalyzer(); 00180 ~StopAnalyzer(); 00181 00183 StopAnalyzer( const TCHAR** stopWords ); 00185 TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader); 00186 00189 static const TCHAR* ENGLISH_STOP_WORDS[]; 00190 }; 00191 00192 00193 00214 class PerFieldAnalyzerWrapper : public Analyzer { 00215 private: 00216 Analyzer* defaultAnalyzer; 00217 CL_NS(util)::CLHashMap<const TCHAR*, Analyzer*, CL_NS(util)::Compare::TChar, 00218 CL_NS(util)::Equals::TChar, CL_NS(util)::Deletor::tcArray,CL_NS(util)::Deletor::Void<Analyzer> > analyzerMap; 00219 public: 00226 PerFieldAnalyzerWrapper(Analyzer* defaultAnalyzer); 00227 ~PerFieldAnalyzerWrapper(); 00228 00235 void addAnalyzer(const TCHAR* fieldName, Analyzer* analyzer); 00236 TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader); 00237 }; 00238 00239 00247 class ISOLatin1AccentFilter: public TokenFilter { 00248 public: 00249 ISOLatin1AccentFilter(TokenStream* input, bool deleteTs): 00250 TokenFilter(input,deleteTs) 00251 { 00252 } 00253 00257 bool next(Token* token); 00258 }; 00259 00260 00264 class KeywordTokenizer: public Tokenizer { 00265 private: 00266 LUCENE_STATIC_CONSTANT(int, DEFAULT_BUFFER_SIZE = 256); 00267 bool done; 00268 int bufferSize; 00269 public: 00270 KeywordTokenizer(CL_NS(util)::Reader* input, int bufferSize=-1); 00271 virtual ~KeywordTokenizer(); 00272 bool next(Token* token); 00273 }; 00274 00279 class KeywordAnalyzer: public Analyzer { 00280 public: 00281 TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader); 00282 virtual ~KeywordAnalyzer(){} 00283 }; 00284 00285 00290 class LengthFilter: public TokenFilter { 00291 private: 00292 int _min; 00293 int _max; 00294 public: 00299 LengthFilter(TokenStream* in, int _min, int _max); 00300 00304 bool next(Token* token); 00305 }; 00306 00307 00308 CL_NS_END 00309 #endif