Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check similarity between two string expressions in Swift

I have scanned text:

Mils, chiiese, wh_ite ch$col_te

And expression list, example:

- cheese
- bread
- white chocolate
- etc.

I need compare broken expression with expression from my list, ex. "white chocolate" with "wh_ite ch$col_te."

Maybe you recommend some frameworks.

like image 367
Artem Krachulov Avatar asked Nov 30 '22 17:11

Artem Krachulov


1 Answers

String distance - Levenshtein distance

What you need to do is measure the difference between two string. To do that, you can use the Levenshtein distance.

For your luck, somebody already implemented this algorihtm in Swift HERE.

To make it work in Swift 1.2, you'll just have to autofix some errors that occour, nothing too fancy.

You can then use it like this:

println(levenshtein("wh_ite ch$col_te", bStr: "white chocolate")) // prints 3, because you have to change 3 letters to get from aStr to bStr

println(levenshtein("wh_ite ch$col_te", bStr: "whsdfdsite chosdfsdfcolate")) // prints 13, because you have to change 13 letters to get from aStr to bStr

You then just set the tolerance and you are done!

like image 58
Dejan Skledar Avatar answered Dec 05 '22 17:12

Dejan Skledar