Check the similarity between two string expressions in Swift

I have a scanned text:

Mils, chiiese, wh_ite ch$col_te

      

And a list of expressions, example:

- cheese
- bread
- white chocolate
- etc.

      

I need to compare a broken expression with an expression from my list, for example. "white chocolate" with "wh_ite ch $ col_te."

You might be recommending some framework.

+3


source to share


2 answers


Line distance - Levenshtein distance

What you need to do is measure the difference between the two lines. You can use Levenshtein distance for this .

For your luck, someone has already implemented this algorithm in Swift HERE .

To get it to work in Swift 1.2 you just need to auto-fix some errors that occur, nothing special.



Then you can 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

      

Then you just set the tolerance and you're done!

+6


source


Dejan Skledar is on the right track - you want to use Levenshtein distance . The implementation he points to needs tweaking to work in Swift 1.2, and it tends to be slow. Below is the faster version with fast speed 1.2.

Just include the class Tools

in your project. Once you have done that, you can get a number representing the difference between the two strings like this:



Tools.levenshtein("cheese", bStr: "chee_e") // returns 1
Tools.levenshtein("butter", bStr: "b_tt_r") // returns 2
Tools.levenshtein("milk", bStr: "butter")   // returns 6

      

+3


source







All Articles