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_AnalysisHeader_ 00008 #define _lucene_analysis_AnalysisHeader_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "CLucene/util/Reader.h" 00015 00016 CL_NS_DEF(analysis) 00017 00018 00019 00033 class Token:LUCENE_BASE{ 00034 private: 00035 int32_t _startOffset; // start in source text 00036 int32_t _endOffset; // end in source text 00037 const TCHAR* _type; // lexical type 00038 int32_t positionIncrement; 00039 size_t bufferTextLen; 00040 00041 public: 00042 #ifndef LUCENE_TOKEN_WORD_LENGTH 00043 TCHAR* _termText; // the text of the term 00044 #else 00045 TCHAR _termText[LUCENE_TOKEN_WORD_LENGTH+1]; // the text of the term 00046 #endif 00047 int32_t _termTextLen; 00048 static const TCHAR* defaultType; 00049 00050 Token(); 00051 ~Token(); 00052 // Constructs a Token with the given text, start and end offsets, & type. 00053 Token(const TCHAR* text, const int32_t start, const int32_t end, const TCHAR* typ=defaultType); 00054 void set(const TCHAR* text, const int32_t start, const int32_t end, const TCHAR* typ=defaultType); 00055 00056 size_t bufferLength(){ return bufferTextLen; } 00057 void growBuffer(size_t size); 00058 00059 /* Set the position increment. This determines the position of this 00060 * token relative to the previous Token in a TokenStream, used in 00061 * phrase searching. 00062 * 00063 * The default value is 1. 00064 * 00065 * Some common uses for this are: 00066 * 00067 * - Set it to zero to put multiple terms in the same position. This is 00068 * useful if, e.g., a word has multiple stems. Searches for phrases 00069 * including either stem will match. In this case, all but the first stem's 00070 * increment should be set to zero: the increment of the first instance 00071 * should be one. Repeating a token with an increment of zero can also be 00072 * used to boost the scores of matches on that token. 00073 * 00074 * - Set it to values greater than one to inhibit exact phrase matches. 00075 * If, for example, one does not want phrases to match across removed stop 00076 * words, then one could build a stop word filter that removes stop words and 00077 * also sets the increment to the number of stop words removed before each 00078 * non-stop word. Then exact phrase queries will only match when the terms 00079 * occur with no intervening stop words. 00080 */ 00081 void setPositionIncrement(int32_t posIncr); 00082 int32_t getPositionIncrement() const; 00083 const TCHAR* termText() const; 00084 size_t termTextLength(); 00085 void resetTermTextLen(); 00086 void setText(const TCHAR* txt); 00087 00096 int32_t startOffset() const { return _startOffset; } 00097 void setStartOffset(int32_t val){ _startOffset =val; } 00098 00103 int32_t endOffset() const { return _endOffset; } 00104 void setEndOffset(int32_t val){ _endOffset =val; } 00105 00106 // Returns this Token's lexical type. Defaults to "word". 00107 const TCHAR* type() const { return _type; } 00108 void setType(const TCHAR* val) { _type = val; } 00109 00110 TCHAR* toString() const; 00111 00113 class OrderCompare:LUCENE_BASE, public CL_NS(util)::Compare::_base //<Token*> 00114 { 00115 public: 00116 bool operator()( Token* t1, Token* t2 ) const; 00117 }; 00118 }; 00119 00132 class TokenStream:LUCENE_BASE { 00133 public: 00135 virtual bool next(Token* token) = 0; 00136 00138 virtual void close() = 0; 00139 00140 virtual ~TokenStream(){ 00141 } 00142 00143 /* This is for backwards compatibility only. You should pass the token you want to fill 00144 * to next(), this will save a lot of object construction and destructions. 00145 * @deprecated. use next(token). Kept only to avoid breaking existing code. 00146 */ 00147 _CL_DEPRECATED(next(Token)) Token* next(); 00148 }; 00149 00150 00161 class Analyzer:LUCENE_BASE{ 00162 public: 00168 virtual TokenStream* tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader)=0; 00169 00170 virtual ~Analyzer(){ 00171 } 00172 00186 virtual int32_t getPositionIncrementGap(const TCHAR* fieldName); 00187 }; 00188 00189 00194 class Tokenizer:public TokenStream { 00195 protected: 00197 CL_NS(util)::Reader* input; 00198 00199 public: 00201 Tokenizer(); 00203 Tokenizer(CL_NS(util)::Reader* _input); 00204 00205 // ** By default, closes the input Reader. */ 00206 virtual void close(); 00207 virtual ~Tokenizer(); 00208 }; 00209 00214 class TokenFilter:public TokenStream { 00215 protected: 00217 TokenStream* input; 00219 bool deleteTokenStream; 00220 00226 TokenFilter(TokenStream* in, bool deleteTS=false); 00227 virtual ~TokenFilter(); 00228 public: 00230 void close(); 00231 }; 00232 00233 CL_NS_END 00234 #endif