How to search all words from input line in ios?

Say I have a dictionary (about 100,000 words) and a word ("inputstring"). So:

I need to generate all words from "inputstring", for example, "input", "string", "put", "strinpg" etc. And then I need to check them in my dictionary. Could you say what is a good algorithm for this? Because I only have an idea:

  • Recursively search for all possible combinations in step 1
  • Use NSPredicatesto filter them in my dictionary.
+4
source share
5 answers

NSPredicate. , , - fault tolerant, . || .

, NSArray. filteredArrayUsingPredicate:, .

, :

, , , CONTAINS . ?tring or s?ring or st?ing... for. ? , .

+3

NSRegularExpression, CoreData NSPredicate, , , (, Regex, ). NSCharacterSet, , .

, , :

NSString *searchedWord = @"inputString";

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) {
    for (NSUInteger index = 0; index < [evaluatedObject length]; index++)
    {
        NSString *subString = [evaluatedObject substringWithRange:NSMakeRange(index, 1)];

        NSUInteger numberOfOccurrencesInSearchWord = [self occurrencesOfSubString:subString inString:searchedWord];
        NSUInteger numberOfOccurrencesInCurrentWord = [self occurrencesOfSubString:subString inString:evaluatedObject];
        if (numberOfOccurrencesInCurrentWord > numberOfOccurrencesInSearchWord)
            return FALSE;
    }
    return TRUE;
}];

//Apply this predicate to your fetch

occurrencesOfSubString:inString: , NSString, . rangeOfString:option:range, NSRegularExpression. ( )

-(NSUInteger)occurrencesOfSubString:(NSString *)subString inString:(NSString *)string
{
    NSUInteger numberOfMatches = 0;
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:subString
                                                                           options:NSRegularExpressionCaseInsensitive error:&error];


    if (!error)
        numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];

    return numberOfMatches;
}

. , evaluatedObject, . , evaluatedObject = @"aaa", 3 "a". . . , :

NSString *evaluatedWithoutRepeat = [evaluatedObject removeDuplicatedCharacters];
for (NSUInteger index = 0; index <= [evaluatedWithoutRepeat length]; index ++)
{
    NSString *subString = [evaluatedWithoutRepeat substringWithRange:NSMakeRange:(index,1)];
    //The rest would be the same.
}

WorkingTest:

NSArray *testValues = @[@"inputString",
                        @"input",
                        @"string",
                        @"put",
                        @"strinpg",
                        @"Stringpg",
                        @"stringNOTWANTED"];
NSLog(@"AllValues: %@", testValues);

NSLog(@"Test: %@", [testValues filteredArrayUsingPredicate:predicate]);

:

> AllValues: (
    inputString,
    input,
    string,
    put,
    strinpg,
    Stringpg,
    stringNOTWANTED
)
> Test: (
    inputString,
    input,
    string,
    put,
    strinpg
)
+5

, . , , , .

, trie , , . ( trie.)

+3

, . , . :

- (NSArray *)getWordsFromString:(NSString *)input{

   NSMutableArray *result = [NSMutableArray new];
   NSUInteger *startIndex = 0;

   for (NSUInteger i = 0; i < input.length ; i++){
       NSString *substring = [input substringWithRange:NSMakeRange(*startIndex, i)];

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"word == %@", substring]; 
      NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Word"];
      fetchRequest.predicate = predicate
      [fetchRequest setIncludesPropertyValues:NO];
      [fetchRequest setIncludesSubentities:NO];
      NSArray *fetchResult = fetch result with predicate

       if (fetchResult.count > 0){
           [result addObject:substring];
           startIndex = i;
       }
   } 

   return result;
}
0
NSMutableArray *foundWords = [NSMutableArray new];
for (NSString *knownWord in vocabulary)
{
    if ([input rangeOfString:knownWord].location != NSNotFound)
    {
        [foundWords addObject:knownWord];
    }
}

, . , , .

0

Source: https://habr.com/ru/post/1598772/


All Articles