如何在ios中搜索输入字符串中的所有单词?

Vik*_*nec 4 recursion search filter nspredicate ios

说,我有词汇量(大约100000个单词)和单词("inputstring").所以:

我需要生成"inputstring"中的所有单词,如"input","string","put","strinpg"等.然后我需要在我的词汇表中查看它们.你能说出任何好的算法吗?因为我只知道:

  1. 在步骤1中递归搜索所有可能的组合
  2. 使用NSPredicates在我的词汇进行过滤.

Lar*_*rme 5

我尝试使用NSRegularExpressionCoreData并且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
Run Code Online (Sandbox Code Playgroud)

我把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;
}
Run Code Online (Sandbox Code Playgroud)

注意:为避免过多循环,您可能需要剥离evaluatedObject以便不检查重复值.例如,如果evaluatedObject = @"aaa",它将为"a"看起来3次.因此,删除其中的重复值可以提高速度.这是一个解决方案.所以代码将在谓词块中:

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.
}
Run Code Online (Sandbox Code Playgroud)

WorkingTest:

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

NSLog(@"Test: %@", [testValues filteredArrayUsingPredicate:predicate]);
Run Code Online (Sandbox Code Playgroud)

输出:

> AllValues: (
    inputString,
    input,
    string,
    put,
    strinpg,
    Stringpg,
    stringNOTWANTED
)
> Test: (
    inputString,
    input,
    string,
    put,
    strinpg
)
Run Code Online (Sandbox Code Playgroud)