Working on a new algorithm that includes wild card matching and that uses alphagrams
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 22 May 2008 01:23:10 +0000 (21:23 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 22 May 2008 01:23:10 +0000 (21:23 -0400)
* Source/AppController.m:
(-[findWords:])

Source/AppController.m

index 55fe550ecd743ab993cb08a25139fd37ab926585..a6541fe7d5113c5ce49b5b8c465271d55c7b8fda 100644 (file)
 {
        [wordlist removeObjects:[wordlist arrangedObjects]];
        
-       NSString *tileString = [tilesField stringValue];
-       int tileCount = [tileString length];
+       Word *temp = [[Word alloc] initWithWord:[tilesField stringValue]];
+       NSString *tiles = [temp alphagram];
+       [temp release];
        
-       NSMutableArray *tiles = [NSMutableArray arrayWithCapacity:tileCount - 1];
-       for (int i = 0; i < tileCount; i++)
-       {
-               [tiles addObject:[tileString substringWithRange:NSMakeRange(i, 1)]];
-       }
-       
-       // create character sets from the tiles
-       NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:tileString];
-       NSCharacterSet *charsetInverted = [charset invertedSet];
+       NSRange wildRange = [tiles rangeOfString:@"?" options:NSBackwardsSearch];
+       int numWild = wildRange.length + wildRange.location;
+       int length = [tiles length];
        
-       // iterate over the dictionary to build our word list
-       for (NSString *word in dictionary)
+       for (int i = 0; i < [dictionary count]; i++)
        {
-               // word is larger than the number of tiles we have, remove
-               if ([word length] > tileCount)
-               {
-                       continue;
-               }
+               Word *word = [dictionary objectAtIndex:i];
+               int wildCounter = numWild;
                
-               // if the word contains characters we don't have, then remove it
-               if ([word rangeOfCharacterFromSet:charsetInverted].location != NSNotFound)
+               if ([word length] > length)
                {
                        continue;
                }
                
-               BOOL success = YES;
-               for (NSString *tile in tiles)
+               BOOL add = YES;
+               for (int j = 0, k = numWild; j < [word length]; j++, k++)
                {
-                       if ([word occurrenceOfChar:tile] > [tileString occurrenceOfChar:tile])
+                       if ([[word alphagram] characterAtIndex:j] != [tiles characterAtIndex:k])
                        {
-                               success = NO;
+                               wildCounter--;
+                               if (wildCounter < 0)
+                               {
+                                       add = NO;
+                                       break;
+                               }
+                               else
+                               {
+                                       continue;
+                               }
                        }
                }
                
-               if (success)
+               if (add)
                {
-                       [wordlist addObject:[[Word alloc] initWithWord:word]];
+                       [wordlist addObject:[word word]];
                }
+               add = YES;
        }
        
        // resort