xcode - How can I learn if 2 NSStrings are almost same to eachother? -


i take string user,and make iterations on it.

for example user entered text @"the weather beautiful today.";

i want make same iterations when user entered "the wether beatifullll tdy" or "th weather beatttiful tooodayy" or "the weatherrr iss beautiful toda".

here code:

// (str user's text)  if ([str rangeofstring:@"hello" options:nscaseinsensitivesearch].location != nsnotfound) { // when user entered helloo want make same iteration, // in case program goes else part.             [array insertobject:@"hello" atindex:s]; } else if ([str rangeofstring:@"you 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]; } 

you can use stringscore fuzzy matching:

https://github.com/thetron/stringscore

example:

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); 

output:

result 1 = 1.000000 result 2 = 0.425000 result 3 = 0.271528 result 4 = 0.250000 result 5 = 0.425000 result 6 = 0.645833 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -