From 27db10fe504117dd8b4a166dcfc94881528c44c3 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 13 Jul 2009 15:24:16 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20levenshteinDistanceOfWord:=20to=20S?= =?UTF-8?q?PStringAdditions=20-=20will=20be=20used=20mainly=20for=20auto-m?= =?UTF-8?q?atching=20column=20names=20while=20csv=20import=20=E2=80=A2=20f?= =?UTF-8?q?ixed=20bug=20for=20in/decreasing=20the=20font=20size=20by=20usi?= =?UTF-8?q?ng=20the=20two-finger=20gesture=20on=20a=20trackpad=20if=20a=20?= =?UTF-8?q?textView=20has=20a=20selection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPStringAdditions.h | 6 ++-- Source/SPStringAdditions.m | 72 ++++++++++++++++++++++++++++++++++++++++++-- Source/SPTextViewAdditions.m | 3 +- 3 files changed, 75 insertions(+), 6 deletions(-) (limited to 'Source') diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h index f6a49105..47863370 100644 --- a/Source/SPStringAdditions.h +++ b/Source/SPStringAdditions.h @@ -59,8 +59,10 @@ static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, int encodin - (NSArray *)lineRangesForRange:(NSRange)aRange; - (NSString *)createViewSyntaxPrettifier; -- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask; -- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet; +- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask; +- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet; + +- (float)levenshteinDistanceWithWord:(NSString *)stringB; #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)set; diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index 114ecf7d..ff7efc88 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -26,6 +26,11 @@ #import "SPStringAdditions.h" #import "RegexKitLite.h" + +@interface NSString (Private) +- (int)smallestOf:(int)a andOf:(int)b andOf:(int)c; +@end + @implementation NSString (SPStringAdditions) /* @@ -272,7 +277,7 @@ #endif -- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask +- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask { NSRange range; NSMutableString* newString = [NSMutableString string]; @@ -302,9 +307,72 @@ } -- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet +- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet { return [self stringByRemovingCharactersInSet:charSet options:0]; } +// calculate the distance between two string case-insensitively +- (float)levenshteinDistanceWithWord:(NSString *)stringB +{ + // normalize strings + NSString * stringA = [NSString stringWithString: self]; + [stringA stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [stringB stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + stringA = [stringA lowercaseString]; + stringB = [stringB lowercaseString]; + + int k, i, j, cost, * d, distance; + + int n = [stringA length]; + int m = [stringB length]; + + if( n++ != 0 && m++ != 0 ) { + + d = malloc( sizeof(int) * m * n ); + + for( k = 0; k < n; k++) + d[k] = k; + + for( k = 0; k < m; k++) + d[ k * n ] = k; + + for( i = 1; i < n; i++ ) + for( j = 1; j < m; j++ ) { + + if( [stringA characterAtIndex: i-1] == [stringB characterAtIndex: j-1] ) + cost = 0; + else + cost = 1; + + d[ j * n + i ] = [self smallestOf: d [ (j - 1) * n + i ] + 1 + andOf: d[ j * n + i - 1 ] + 1 + andOf: d[ (j - 1) * n + i -1 ] + cost ]; + } + + distance = d[ n * m - 1 ]; + + free( d ); + + return distance; + } + return 0.0; +} + +// return the minimum of a, b and c +- (int)smallestOf:(int)a andOf:(int)b andOf:(int)c +{ + int min = a; + if ( b < min ) + min = b; + + if( c < min ) + min = c; + + return min; +} + + @end diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m index f9c9a1de..2dcf0614 100644 --- a/Source/SPTextViewAdditions.m +++ b/Source/SPTextViewAdditions.m @@ -416,7 +416,7 @@ #pragma mark multi-touch trackpad support /* - * Trackpad two-finger zooming gesture in/decreases the font size + * Trackpad two-finger zooming gesture for in/decreasing the font size */ - (void) magnifyWithEvent:(NSEvent *)anEvent { @@ -430,7 +430,6 @@ else if([anEvent deltaZ]<-5.0) [self makeTextSizeSmaller]; - [self insertText:@""]; } -- cgit v1.2.3