[英]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."
“白巧克力”与“wh_ite ch$col_te”。
Maybe you recommend some frameworks.也许你推荐一些框架。
What you need to do is measure the difference between two string.您需要做的是测量两个字符串之间的差异。 To do that, you can use the Levenshtein distance .
为此,您可以使用Levenshtein distance 。
For your luck, somebody already implemented this algorihtm in Swift HERE .幸运的是,有人已经在 Swift HERE 中实现了这个算法。
To make it work in Swift 1.2, you'll just have to autofix some errors that occour, nothing too fancy.为了让它在 Swift 1.2 中工作,你只需要自动修复一些发生的错误,没什么特别的。
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!然后,您只需设置容差即可完成!
Dejan Skledar's on the right track -- you want to make use of Levenshtein distance . Dejan Skledar 走在正确的轨道上——你想利用Levenshtein distance 。 The implementation he points to needs tweaking to work in Swift 1.2, and it tends to be slow.
他指出的实现需要调整才能在 Swift 1.2 中工作,而且速度往往很慢。 Here's a Swift 1.2-compatible, faster implementation.
这是一个兼容 Swift 1.2 的更快的实现。
Simply include the Tools
class in your project.只需在您的项目中包含
Tools
类。 Once you've done that, you can get a number representing the difference between two strings this way:完成后,您可以通过这种方式获得一个表示两个字符串之间差异的数字:
Tools.levenshtein("cheese", bStr: "chee_e") // returns 1
Tools.levenshtein("butter", bStr: "b_tt_r") // returns 2
Tools.levenshtein("milk", bStr: "butter") // returns 6
Use StringMetric and be happy使用 StringMetric 并快乐
https://github.com/autozimu/StringMetric.swift https://github.com/autozimu/StringMetric.swift
import StringMetric
...
"kitten".distance(between: "sitting") // => 0.746
"君子和而不同".distance(between: "小人同而不和") // => 0.555
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.