Skip to content

Commit

Permalink
Add stemmingDisabled property to CLuceneSearchService, which toggles …
Browse files Browse the repository at this point in the history
…new stemmingDisabled property in BRSnowballAnalyzer to enable toggling support for stemming on/off.
  • Loading branch information
msqr committed Apr 22, 2015
1 parent 8aa3d8f commit c116120
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
47 changes: 29 additions & 18 deletions BRFullTextSearch/BRSnowballAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ BRSnowballAnalyzer::BRSnowballAnalyzer(const TCHAR* language) {
stopSet = NULL;
}

/** Builds the named analyzer with the given stop words. */
BRSnowballAnalyzer::BRSnowballAnalyzer(const TCHAR* language, const TCHAR** stopWords, bool prefixModeEnabled) {
this->language = STRDUP_TtoT(language);

stopSet = _CLNEW CLTCSetList(true);
StopFilter::fillStopTable(stopSet,stopWords);
prefixMode = prefixModeEnabled;
}

BRSnowballAnalyzer::~BRSnowballAnalyzer(){
SavedStreams* t = reinterpret_cast<SavedStreams*>(this->getPreviousTokenStream());
if (t) _CLDELETE(t->filteredTokenStream);
Expand All @@ -55,16 +64,6 @@ BRSnowballAnalyzer::~BRSnowballAnalyzer(){
_CLDELETE(stopSet);
}

/** Builds the named analyzer with the given stop words.
*/
BRSnowballAnalyzer::BRSnowballAnalyzer(const TCHAR* language, const TCHAR** stopWords, bool prefixModeEnabled) {
this->language = STRDUP_TtoT(language);

stopSet = _CLNEW CLTCSetList(true);
StopFilter::fillStopTable(stopSet,stopWords);
prefixMode = prefixModeEnabled;
}

TokenStream* BRSnowballAnalyzer::tokenStream(const TCHAR* fieldName, CL_NS(util)::Reader* reader) {
return this->tokenStream(fieldName,reader,false);
}
Expand All @@ -85,10 +84,12 @@ TokenStream* BRSnowballAnalyzer::tokenStream(const TCHAR* fieldName, CL_NS(util)
if (stopSet != NULL) {
result = _CLNEW CL_NS(analysis)::StopFilter(result, true, stopSet);
}
if ( prefixMode ) {
result = _CLNEW bluerocket::lucene::analysis::SnowballPrefixFilter(result, true, language);
} else {
result = _CLNEW SnowballFilter(result, language, true);
if ( !stemmingDisabled ) {
if ( prefixMode ) {
result = _CLNEW bluerocket::lucene::analysis::SnowballPrefixFilter(result, true, language);
} else {
result = _CLNEW SnowballFilter(result, language, true);
}
}
return result;
}
Expand All @@ -108,10 +109,12 @@ TokenStream* BRSnowballAnalyzer::reusableTokenStream(const TCHAR* fieldName, Rea
streams->filteredTokenStream = _CLNEW StandardFilter(streams->tokenStream, true);
streams->filteredTokenStream = _CLNEW LowerCaseFilter(streams->filteredTokenStream, true);
streams->filteredTokenStream = _CLNEW StopFilter(streams->filteredTokenStream, true, stopSet);
if ( prefixMode ) {
streams->filteredTokenStream = _CLNEW bluerocket::lucene::analysis::SnowballPrefixFilter(streams->filteredTokenStream, true, language);
} else {
streams->filteredTokenStream = _CLNEW SnowballFilter(streams->filteredTokenStream, language, true);
if ( !stemmingDisabled ) {
if ( prefixMode ) {
streams->filteredTokenStream = _CLNEW bluerocket::lucene::analysis::SnowballPrefixFilter(streams->filteredTokenStream, true, language);
} else {
streams->filteredTokenStream = _CLNEW SnowballFilter(streams->filteredTokenStream, language, true);
}
}
} else {
streams->tokenStream->reset(reader);
Expand All @@ -128,4 +131,12 @@ void BRSnowballAnalyzer::setPrefixMode(bool mode) {
prefixMode = mode;
}

bool BRSnowballAnalyzer::getStemmingDisabled() {
return stemmingDisabled;
}

void BRSnowballAnalyzer::setStemmingDisabled(bool disabled) {
stemmingDisabled = disabled;
}

CL_NS_END2
6 changes: 5 additions & 1 deletion BRFullTextSearch/BRSnowballAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
CL_CLASS_DEF(util,BufferedReader)
CL_NS_DEF2(analysis,snowball)

/** Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
/**
* Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
* LowerCaseFilter}, {@link StopFilter} and {@link SnowballFilter}.
*
* Available stemmers are listed in {@link net.sf.snowball.ext}. The name of a
Expand All @@ -25,6 +26,7 @@ class CLUCENE_CONTRIBS_EXPORT BRSnowballAnalyzer : public Analyzer {
TCHAR* language;
CLTCSetList* stopSet;
bool prefixMode;
bool stemmingDisabled = false;

class SavedStreams;

Expand All @@ -41,6 +43,8 @@ class CLUCENE_CONTRIBS_EXPORT BRSnowballAnalyzer : public Analyzer {
bool getPrefixMode();
void setPrefixMode(bool mode);

bool getStemmingDisabled();
void setStemmingDisabled(bool disabled);

/** Constructs a {@link StandardTokenizer} filtered by a {@link
StandardFilter}, a {@link LowerCaseFilter} and a {@link StopFilter}. */
Expand Down
7 changes: 7 additions & 0 deletions BRFullTextSearch/CLuceneSearchService.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
*/
@property (nonatomic, strong) NSString *defaultAnalyzerLanguage;

/**
* Turn stemming for tokenized fields on/off. Defaults to @c NO.
*
* @since 1.0.6
*/
@property (nonatomic, getter=isStemmingDisabled) BOOL stemmingDisabled;

/**
* Turn support for prefix-based searches on tokenized and stemmed fields. Defaults to @c NO.
*
Expand Down
17 changes: 17 additions & 0 deletions BRFullTextSearch/CLuceneSearchService.mm
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ - (void)setSupportStemmedPrefixSearches:(BOOL)supportStemmedPrefixSearches {
}
}

- (BOOL)isStemmingDisabled {
Analyzer *analyzer = [self defaultAnalyzer];
lucene::analysis::snowball::BRSnowballAnalyzer *snowball = dynamic_cast<lucene::analysis::snowball::BRSnowballAnalyzer *>(analyzer);
if ( snowball != NULL ) {
return (snowball->getStemmingDisabled() ? YES : NO);
}
return NO;
}

- (void)setStemmingDisabled:(BOOL)stemmingDisabled {
Analyzer *analyzer = [self defaultAnalyzer];
lucene::analysis::snowball::BRSnowballAnalyzer *snowball = dynamic_cast<lucene::analysis::snowball::BRSnowballAnalyzer *>(analyzer);
if ( snowball != NULL ) {
snowball->setStemmingDisabled(stemmingDisabled ? true : false);
}
}

- (void)resetSearcher {
if ( searcher.get() != NULL ) {
searcher->close();
Expand Down

0 comments on commit c116120

Please sign in to comment.