Skip to content

Commit

Permalink
Merge branch 'release/1.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Apr 22, 2015
2 parents 62b6043 + fd881c7 commit 48756e7
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BRFullTextSearch.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "BRFullTextSearch"
s.version = "1.0.5"
s.version = "1.0.6"
s.summary = "iOS Objective-C full text search engine."
s.description = <<-DESC
This project provides a way to integrate full-text search
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
C9049BBB17CAD3860099F480 /* StickyNoteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9049BB917CAD3860099F480 /* StickyNoteViewController.xib */; };
C9049BC217CAE7700099F480 /* stop-words.txt in Resources */ = {isa = PBXBuildFile; fileRef = C9049BC017CAE7700099F480 /* stop-words.txt */; };
C9049BC517CB10800099F480 /* Notifications.m in Sources */ = {isa = PBXBuildFile; fileRef = C9049BC417CB10800099F480 /* Notifications.m */; };
C9B506F71AE72F1B00108562 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B506F51AE72F1B00108562 /* SettingsViewController.m */; };
C9B506F81AE72F1B00108562 /* SettingsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B506F61AE72F1B00108562 /* SettingsViewController.xib */; };
EED3B88A827168F2772DB75F /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A06DBFE5733C082A1D5847E4 /* libPods.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -91,6 +93,9 @@
C9049BC117CAE7700099F480 /* en */ = {isa = PBXFileReference; lastKnownFileType = text; name = en; path = "stop-words.txt"; sourceTree = "<group>"; };
C9049BC317CB10800099F480 /* Notifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Notifications.h; sourceTree = "<group>"; };
C9049BC417CB10800099F480 /* Notifications.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Notifications.m; sourceTree = "<group>"; };
C9B506F41AE72F1B00108562 /* SettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsViewController.h; sourceTree = "<group>"; };
C9B506F51AE72F1B00108562 /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = "<group>"; };
C9B506F61AE72F1B00108562 /* SettingsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SettingsViewController.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -164,6 +169,9 @@
C9049BA917CAC6BA0099F480 /* CoreDataManager.m */,
C9049BC317CB10800099F480 /* Notifications.h */,
C9049BC417CB10800099F480 /* Notifications.m */,
C9B506F41AE72F1B00108562 /* SettingsViewController.h */,
C9B506F51AE72F1B00108562 /* SettingsViewController.m */,
C9B506F61AE72F1B00108562 /* SettingsViewController.xib */,
C9049BB217CACD870099F480 /* StickyNoteListViewController.h */,
C9049BB317CACD870099F480 /* StickyNoteListViewController.m */,
C9049BB417CACD870099F480 /* StickyNoteListViewController.xib */,
Expand Down Expand Up @@ -298,6 +306,7 @@
C9049B2117CAC48E0099F480 /* InfoPlist.strings in Resources */,
C9049B2917CAC48F0099F480 /* Default.png in Resources */,
C9049B2B17CAC48F0099F480 /* [email protected] in Resources */,
C9B506F81AE72F1B00108562 /* SettingsViewController.xib in Resources */,
C9049B2D17CAC48F0099F480 /* [email protected] in Resources */,
C9049BB617CACD870099F480 /* StickyNoteListViewController.xib in Resources */,
C9049BBB17CAD3860099F480 /* StickyNoteViewController.xib in Resources */,
Expand Down Expand Up @@ -348,6 +357,7 @@
C9049B2317CAC48E0099F480 /* main.m in Sources */,
C9049B2717CAC48E0099F480 /* AppDelegate.m in Sources */,
C9049BAA17CAC6BA0099F480 /* CoreDataManager.m in Sources */,
C9B506F71AE72F1B00108562 /* SettingsViewController.m in Sources */,
C9049BAD17CAC9CD0099F480 /* Model.xcdatamodeld in Sources */,
C9049BB017CACA5C0099F480 /* StickyNote.m in Sources */,
C9049BB517CACD870099F480 /* StickyNoteListViewController.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <BRFullTextSearch/BRFullTextSearch.h>
#import <BRFullTextSearch/CLuceneSearchService.h>
#import "CoreDataManager.h"
#import "SettingsViewController.h"
#import "StickyNoteListViewController.h"

@implementation AppDelegate {
Expand All @@ -31,7 +32,27 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.viewController.searchService = searchService;
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window makeKeyAndVisible];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
return YES;
}

- (void)settingsChanged:(NSNotification *)notification {
NSUserDefaults *defaults = notification.object;
const BOOL stemmingDisabled = [defaults boolForKey:kStemmingDisabledKey];
const BOOL stemmingPrefixEnabled = [defaults boolForKey:kStemmingPrefixSupportEnabledKey];
BOOL reindex = NO;
if ( stemmingDisabled != searchService.stemmingDisabled ) {
searchService.stemmingDisabled = stemmingDisabled;
reindex = YES;
}
if ( stemmingPrefixEnabled != searchService.supportStemmedPrefixSearches ) {
searchService.supportStemmedPrefixSearches = stemmingPrefixEnabled;
reindex = YES;
}
if ( reindex ) {
[coreDataManager reindex];
}
}

@end
2 changes: 2 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/CoreDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

@property (nonatomic, strong) id<BRSearchService> searchService;

- (void)reindex;

@end
14 changes: 14 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/CoreDataManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <BRFullTextSearch/BRFullTextSearch.h>
#import <MagicalRecord/CoreData+MagicalRecord.h>
#import "Notifications.h"
#import "StickyNote.h"

@implementation CoreDataManager

Expand Down Expand Up @@ -77,4 +78,17 @@ - (void)maintainSearchIndexFromManagedObjectDidSave:(NSNotification *)notificati
}];
}

- (void)reindex {
[self.searchService bulkUpdateIndex:^(id<BRIndexUpdateContext> updateContext) {
NSLog(@"Reindexing...");
[NSManagedObjectContext MR_resetContextForCurrentThread]; // make sure we pull in latest data
for ( StickyNote *note in [StickyNote MR_findAll] ) {
[self.searchService addObjectToIndex:note context:updateContext];
}
} queue:dispatch_get_main_queue() finished:^(int updateCount, NSError *error) {
NSLog(@"Reindexing complete.");
[[NSNotificationCenter defaultCenter] postNotificationName:SearchIndexDidChange object:nil];
}];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SettingsViewController.h
// SampleCoreDataProject
//
// Created by Matt on 4/22/15.
// Copyright (c) 2015 Blue Rocket, Inc. All rights reserved.
//

#import <UIKit/UIKit.h>

extern NSString * const kStemmingDisabledKey;
extern NSString * const kStemmingPrefixSupportEnabledKey;

/**
Provide a UI for changing system settings. Add settings are stored in @c NSUserDefaults.
*/
@interface SettingsViewController : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// SettingsViewController.m
// SampleCoreDataProject
//
// Created by Matt on 4/22/15.
// Copyright (c) 2015 Blue Rocket, Inc. All rights reserved.
//

#import "SettingsViewController.h"

NSString * const kStemmingDisabledKey = @"StemmingDisabled";
NSString * const kStemmingPrefixSupportEnabledKey = @"StemmingPrefixSupportEnabled";

@interface SettingsViewController ()
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
@property (strong, nonatomic) IBOutlet UISwitch *stemmingSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *stemmingPrefixSwitch;
@end

@implementation SettingsViewController

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateUIFromModel];
}

- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.topMarginConstraint.constant = (self.topLayoutGuide.length + 20);
}

- (void)updateUIFromModel {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.stemmingSwitch.on = ([defaults boolForKey:kStemmingDisabledKey] != YES);
self.stemmingPrefixSwitch.enabled = self.stemmingSwitch.on;
self.stemmingPrefixSwitch.on = [defaults boolForKey:kStemmingPrefixSupportEnabledKey];
}

- (IBAction)switchDidChange:(UISwitch *)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ( sender == self.stemmingSwitch ) {
[defaults setBool:!sender.on forKey:kStemmingDisabledKey];
[self updateUIFromModel];
} else if ( sender == self.stemmingPrefixSwitch ) {
[defaults setBool:sender.on forKey:kStemmingPrefixSupportEnabledKey];
}
}

@end
Loading

0 comments on commit 48756e7

Please sign in to comment.