This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
AEStringAdditions.mm
486 lines (409 loc) · 13.5 KB
/
AEStringAdditions.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//
// AEStringAdditions.m
// AssistantExtensions
//
// Created by Kexik on 11/30/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "AEStringAdditions.h"
@implementation NSString (AEAdditions)
-(NSMutableArray*) _letterPairs:(NSString*) string;
{
int numPairs=[string length]-1;
NSMutableArray* pairs=[NSMutableArray arrayWithCapacity:numPairs];
int i=0;
for (;i<numPairs; i++)
[pairs addObject:[string substringWithRange:NSMakeRange(i,2)]];
return pairs;
}
-(NSMutableArray*) _wordLetterPairs:(NSString*) string;
{
NSMutableArray* allPairs=[NSMutableArray array];
NSArray* words=[string componentsSeparatedByString:@" "];
unsigned w=0;
for (; w<[words count]; w++)
[allPairs addObjectsFromArray:[self _letterPairs:[words objectAtIndex:w]]];
return allPairs;
}
// K3A Note: not my code, but I don't know the author as well... google will help
-(float) similarityWithString:(NSString*) otherString;
{
NSMutableArray* pairs1=[self _wordLetterPairs:[self uppercaseString]];
NSMutableArray* pairs2=[otherString _wordLetterPairs:[otherString uppercaseString]];
int intersection=0;
int u=([pairs1 count]+[pairs2 count]);
unsigned i=0;
unsigned j;
for (; i<[pairs1 count]; i++)
{
j=0;
for (; j<[pairs2 count]; j++)
{
if ([[pairs1 objectAtIndex:i] compare:[pairs2 objectAtIndex:j]] == NSOrderedSame)
{
intersection++;
[pairs2 removeObjectAtIndex:j];
}
}
}
return (2.0*intersection)/u;
}
-(NSString*)stringWithFirstUppercase
{
if ([self length] == 0)
return [[self copy] autorelease];
NSString* firstCh = [[self substringToIndex:1] uppercaseString];
return [firstCh stringByAppendingString:[self substringFromIndex:1]];
}
-(NSString*)urlEncodedString
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self, NULL, CFSTR(":/?#[]@!$&'()*+,;="), kCFStringEncodingUTF8);
return [result autorelease];
}
static NSArray* soundexCharSets = nil;
- (void) initSoundex
{
if( soundexCharSets == nil )
{
NSMutableArray* cs = [NSMutableArray array];
NSCharacterSet* charSet;
charSet = [NSCharacterSet characterSetWithCharactersInString:@"aeiouhw"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"bfpv"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"cgjkqsxz"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"dt"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"l"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"mn"];
[cs addObject:charSet];
charSet = [NSCharacterSet characterSetWithCharactersInString:@"r"];
[cs addObject:charSet];
soundexCharSets = [cs retain];
}
}
- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask
{
NSRange range;
NSMutableString* newString = [NSMutableString string];
unsigned len = [self length];
mask &= ~NSBackwardsSearch;
range = NSMakeRange (0, len);
while (range.length)
{
NSRange substringRange;
unsigned pos = range.location;
range = [self rangeOfCharacterFromSet:charSet options:mask range:range];
if (range.location == NSNotFound)
range = NSMakeRange (len, 0);
substringRange = NSMakeRange (pos, range.location - pos);
[newString appendString:[self substringWithRange:substringRange]];
range.location += range.length;
range.length = len - range.location;
}
return newString;
}
- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet
{
return [self stringByRemovingCharactersInSet:charSet options:0];
}
- (unsigned) soundexValueForCharacter:(unichar) aCharacter
{
// returns the soundex mapping for the first character in the string. If the value returned is 0, the character should be discarded.
unsigned indx;
NSCharacterSet* cs;
for( indx = 0; indx < [soundexCharSets count]; ++indx )
{
cs = [soundexCharSets objectAtIndex:indx];
if([cs characterIsMember:aCharacter])
return indx;
}
return 0;
}
- (NSString*) soundexString
{
// returns the Soundex representation of the string.
/*
Replace consonants with digits as follows (but do not change the first letter):
b, f, p, v => 1
c, g, j, k, q, s, x, z => 2
d, t => 3
l => 4
m, n => 5
r => 6
Collapse adjacent identical digits into a single digit of that value.
Remove all non-digits after the first letter.
Return the starting letter and the first three remaining digits. If needed, append zeroes to make it a letter and three digits.
*/
[self initSoundex];
if([self length] > 0)
{
NSMutableString* soundexStr = [NSMutableString string];
// strip whitespace and convert to lower case
NSString* workingString = [[self lowercaseString] stringByRemovingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
unsigned indx, soundValue, previousSoundValue = 0;
// include first character
[soundexStr appendString:[workingString substringToIndex:1]];
// convert up to 3 more significant characters
for( indx = 1; indx < [workingString length]; ++indx )
{
soundValue = [self soundexValueForCharacter:[workingString characterAtIndex:indx]];
if( soundValue > 0 && soundValue != previousSoundValue )
[soundexStr appendString:[NSString stringWithFormat:@"%d", soundValue]];
previousSoundValue = soundValue;
// if we've got four characters, don't need to scan any more
if([soundexStr length] >= 4)
break;
}
// if < 4 characters, need to pad the string with zeroes
while([soundexStr length] < 4)
[soundexStr appendString:@"0"];
//NSLog(@"soundex for '%@' = %@", self, soundexStr );
return soundexStr;
}
else
return @"";
}
- (BOOL) soundsLikeString:(NSString*) aString
{
if (![self length] || ![aString length]) return NO;
return [[self soundexString] isEqualToString:[aString soundexString]];
}
//-------------------------------------- METAPHONE --------------------------------------------
#pragma mark - METAPHONE
- (NSString *)metaphone {
NSString* aWord = self;
NSString *code = @"";
int term_length = [aWord length];
if (term_length == 0) {
return code;
}
//NSArray *vowels = [[NSArray alloc] initWithObjects:@"a",@"e",@"i",@"o",@"u",nil];
aWord = [aWord lowercaseString];
aWord = [aWord stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
aWord = [aWord stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([aWord length] == 0) {
return code;
}
//NSString *firstChar = [aWord substringToIndex:1];
NSString *aWord2 = [aWord substringToIndex:1];
for (unsigned idx = 0; idx < [aWord length]; ++idx) {
NSString *ch = [aWord substringWithRange:NSMakeRange(idx, 1)];
if (![ch isEqualToString:[aWord2 substringWithRange:NSMakeRange([aWord2 length]-1, 1)]]) {
aWord2 = [aWord2 stringByAppendingString:ch];
}
}
/* firstChar = [aWord2 substringToIndex:1];
NSString *aWord3 = [aWord2 substringToIndex:1];
for (int idx = 1; idx < [aWord2 length]; ++idx) {
NSString *ch = [aWord2 substringWithRange:NSMakeRange(idx, 1)];
if (![vowels containsObject:ch]) {
aWord3 = [aWord3 stringByAppendingString:ch];
}
}*/
aWord = aWord2;
term_length = [aWord length];
if (term_length == 0) {
return code;
}
if (term_length > 1) {
NSString *firstChars = [aWord substringToIndex:2];
NSDictionary *translations = [NSDictionary dictionaryWithObjectsAndKeys:
@"e",@"ae",
@"n",@"gn",
@"n",@"kn",
@"n",@"pn",
@"n",@"wr",
@"w",@"wh",nil];
if ([translations objectForKey:firstChars] != nil) {
aWord = [aWord substringFromIndex:2];
code = [translations objectForKey:firstChars];
term_length = [aWord length];
}
} else if ([aWord characterAtIndex:0] == 'x') {
aWord = @"";
code = @"s";
term_length = 0;
}
NSDictionary *standardTranslations = [NSDictionary dictionaryWithObjectsAndKeys:
@"b",@"b",
@"k",@"c",
@"t",@"d",
@"k",@"g",
@"h",@"h",
@"k",@"k",
@"p",@"p",
@"k",@"q",
@"s",@"s",
@"t",@"t",
@"f",@"v",
@"w",@"w",
@"ks",@"x",
@"y",@"y",
@"s",@"z",
nil];
int i = 0;
while (i < term_length) {
NSString *addChar=@"", *part_n_2=@"", *part_n_3=@"", *part_n_4=@"", *part_c_2=@"", *part_c_3=@"";
if (i < (term_length - 1)) {
part_n_2 = [aWord substringWithRange:NSMakeRange(i, 2)];
if (i > 0) {
part_c_2 = [aWord substringWithRange:NSMakeRange(i-1, 2)];
part_c_3 = [aWord substringWithRange:NSMakeRange(i-1, 3)];
}
}
if (i < (term_length - 2)) {
part_n_3 = [aWord substringWithRange:NSMakeRange(i, 3)];
}
if (i < (term_length - 3)) {
part_n_4 = [aWord substringWithRange:NSMakeRange(i, 4)];
}
switch ([aWord characterAtIndex:i]) {
case 'b':
addChar = [standardTranslations objectForKey:@"b"];
if (i == (term_length - 1)) {
if (i > 0) {
if ([aWord characterAtIndex:i-1] == 'm') {
addChar = @"";
}
}
}
break;
case 'c':
addChar = [standardTranslations objectForKey:@"c"];
if ([part_c_2 isEqualToString:@"ch"]) {
addChar = @"x";
} else if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"c[iey]"] evaluateWithObject:part_n_2] == YES) {
addChar = @"s";
}
if ([part_n_3 isEqualToString:@"cia"]) {
addChar = @"x";
}
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"sc[iey]"] evaluateWithObject:part_c_3] == YES) {
addChar = @"";
}
break;
case 'd':
addChar = [standardTranslations objectForKey:@"d"];
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"dg[iey]"] evaluateWithObject:part_n_3] == YES) {
addChar = @"j";
}
break;
case 'g':
addChar = [standardTranslations objectForKey:@"g"];
if ([part_n_2 isEqualToString:@"gh"]) {
if (i == (term_length - 2)) {
addChar = @"";
}
} else if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"gh[aeiouy]"] evaluateWithObject:part_n_3] == YES) {
addChar = @"";
} else if ([part_n_2 isEqualToString:@"gn"]) {
addChar = @"";
} else if ([part_n_4 isEqualToString:@"gned"]) {
addChar = @"";
} else if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"dg[iey]"] evaluateWithObject:part_c_3] == YES) {
addChar = @"";
} else if ([part_n_2 isEqualToString:@"gi"]) {
if (![part_c_3 isEqualToString:@"ggi"]) {
addChar = @"j";
}
} else if ([part_n_2 isEqualToString:@"ge"]) {
if (![part_c_3 isEqualToString:@"gge"]) {
addChar = @"j";
}
} else if ([part_n_2 isEqualToString:@"gy"]) {
if (![part_c_3 isEqualToString:@"ggy"]) {
addChar = @"j";
}
} else if ([part_n_2 isEqualToString:@"gg"]) {
addChar = @"";
}
break;
case 'h':
addChar = [standardTranslations objectForKey:@"h"];
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"[aeiouy]h[^aeiouy]"] evaluateWithObject:part_c_3] == YES) {
addChar = @"";
} else if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"[csptg]h"] evaluateWithObject:part_c_2] == YES) {
addChar = @"";
}
break;
case 'k':
addChar = [standardTranslations objectForKey:@"k"];
if ([part_c_2 isEqualToString:@"ck"]) {
addChar = @"";
}
break;
case 'p':
addChar = [standardTranslations objectForKey:@"p"];
if ([part_n_2 isEqualToString:@"ph"]) {
addChar = @"f";
}
break;
case 'q':
addChar = [standardTranslations objectForKey:@"q"];
break;
case 's':
addChar = [standardTranslations objectForKey:@"s"];
if ([part_n_2 isEqualToString:@"sh"]) {
addChar = @"x";
}
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"si[ao]"] evaluateWithObject:part_n_3] == YES) {
addChar = @"x";
}
break;
case 't':
addChar = [standardTranslations objectForKey:@"t"];
if ([part_n_2 isEqualToString:@"th"]) {
addChar = @"0";
}
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"ti[ao]"] evaluateWithObject:part_n_3] == YES) {
addChar = @"x";
}
break;
case 'v':
addChar = [standardTranslations objectForKey:@"v"];
break;
case 'w':
addChar = [standardTranslations objectForKey:@"w"];
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"w[^aeiouy]"] evaluateWithObject:part_n_2] == YES) {
addChar = @"";
}
break;
case 'x':
addChar = [standardTranslations objectForKey:@"x"];
break;
case 'y':
addChar = [standardTranslations objectForKey:@"y"];
break;
case 'z':
addChar = [standardTranslations objectForKey:@"z"];
break;
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
if (i == 0) {
addChar = [aWord substringWithRange:NSMakeRange(i,1)];
} else {
addChar = @"";
}
break;
default:
addChar = [aWord substringWithRange:NSMakeRange(i, 1)];
break;
}
code = [code stringByAppendingString:addChar];
i += 1;
}
return code;
}
//---------------------------------
-(BOOL)isFuzzyEqualTo:(NSString*)aStr
{
return [self soundsLikeString:aStr];
}
@end