Compare similarity of two NSStrings

拜拜、爱过 提交于 2019-12-20 04:28:06

问题


I would like to compare two NSStrings. If the user types "Stonehhengge", should get a "border on answer"="almost equals", because the right answer is: "Stonhenge".

I would like a percentage of how much one string equals another. If "Stonhenge" is 9 letters, and the typed text contain 8 letters from "Stonhenge", but it's not equal to "Stonhenge", for example: "Stonehange" is not equal to "Stonhenge", but it's near - I'd like to know how near a match the strings are.

I only know isEqual:. If you type "Stonehenge", you match and will get the right symbol. If text field's text is not equal, you do not get a match and get the wrong symbol.


回答1:


EDIT What you want is to compute Levenshtein Distance.

You may want to check out this question Calculate number of differences between two NSStrings




回答2:


I like this library, StringScore https://github.com/thetron/StringScore

It gives you these useful methods:

- (CGFloat) scoreAgainst:(NSString *)otherString;

- (CGFloat) scoreAgainst:(NSString *)otherString fuzziness:(NSNumber *)fuzziness;

- (CGFloat) scoreAgainst:(NSString *)otherString fuzziness:(NSNumber *)fuzziness options:(NSStringScoreOption)options;


来源:https://stackoverflow.com/questions/14445476/compare-similarity-of-two-nsstrings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!