1

ユーザーから文字列を取得し、それに対していくつかの反復を行います。

たとえば、ユーザーが @"The weather is beautiful today." というテキストを入力したとします。

"the wether is beatifullll tdy"ユーザーがまたは"th weather is beatttiful tooodayy"またはを入力したときに同じ反復を行いたい"the weatherrr iss beautiful toda"

これが私のコードです:

// (str is the user's text)

if ([str rangeOfString:@"hello" options:NSCaseInsensitiveSearch].location != NSNotFound) {
// when user entered helloo I want make to same iteration,
// but in this case the program goes else part.        
    [array insertObject:@"hello" atIndex:s];
} else if ([str rangeOfString:@"You are so beautiful" options:NSCaseInsensitiveSearch].location != NSNotFound ) {
    [dizi insertObject:@"I know, Thanks" atIndex:s];
} else if ([str rangeOfString:@"Have Lunch?" options:NSCaseInsensitiveSearch].location != NSNotFound) {
    [array insertObject:@"Yes,I have" atIndex:s];
} else {
    [array insertObject:@"please,speak english" atIndex:s];
}
4

1 に答える 1

1

あいまい一致には StringScore を使用できます。

https://github.com/thetron/StringScore

例:

NSString *testString = @"Hello world!";

CGFloat result1 = [testString scoreAgainst:@"Hello world!"];
CGFloat result2 = [testString scoreAgainst:@"world"];
CGFloat result3 = [testString scoreAgainst:@"wXrld" fuzziness:[NSNumber numberWithFloat:0.8]];
CGFloat result4 = [testString scoreAgainst:@"world" fuzziness:nil options:NSStringScoreOptionFavorSmallerWords];
CGFloat result5 = [testString scoreAgainst:@"world" fuzziness:nil options:(NSStringScoreOptionFavorSmallerWords & NSStringScoreOptionReducedLongStringPenalty)];
CGFloat result6 = [testString scoreAgainst:@"HW"]; // abbreviation matching example

NSLog(@"Result 1 = %f", result1);
NSLog(@"Result 2 = %f", result2);
NSLog(@"Result 3 = %f", result3);
NSLog(@"Result 4 = %f", result4);
NSLog(@"Result 5 = %f", result5);
NSLog(@"Result 6 = %f", result6);

出力:

Result 1 = 1.000000
Result 2 = 0.425000
Result 3 = 0.271528
Result 4 = 0.250000
Result 5 = 0.425000
Result 6 = 0.645833
于 2013-08-21T07:01:03.523 に答える