• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Go 实现字符串相似度计算函数 Levenshtein 和 SimilarText

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

【转】http://www.syyong.com/Go/Go-implements-the-string-similarity-calculation-function-Levenshtein-and-SimilarText.html

levenshtein() 和 similar_text() 是 PHP 内置的两个字符串相似度计算函数。Levenshtein 计算两个字符串之间的编辑距离,SimilarText 计算两个字符串的相似度。下面使用Go分别实现二者。

Levenshtein

// levenshtein()
// costIns: Defines the cost of insertion.
// costRep: Defines the cost of replacement.
// costDel: Defines the cost of deletion.
func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int {
    var maxLen = 255
    l1 := len(str1)
    l2 := len(str2)
    if l1 == 0 {
        return l2 * costIns
    }
    if l2 == 0 {
        return l1 * costDel
    }
    if l1 > maxLen || l2 > maxLen {
        return -1
    }

    tmp := make([]int, l2+1)
    p1 := make([]int, l2+1)
    p2 := make([]int, l2+1)
    var c0, c1, c2 int
    var i1, i2 int
    for i2 := 0; i2 <= l2; i2++ {
        p1[i2] = i2 * costIns
    }
    for i1 = 0; i1 < l1; i1++ {
        p2[0] = p1[0] + costDel
        for i2 = 0; i2 < l2; i2++ {
            if str1[i1] == str2[i2] {
                c0 = p1[i2]
            } else {
                c0 = p1[i2] + costRep
            }
            c1 = p1[i2+1] + costDel
            if c1 < c0 {
                c0 = c1
            }
            c2 = p2[i2] + costIns
            if c2 < c0 {
                c0 = c2
            }
            p2[i2+1] = c0
        }
        tmp = p1
        p1 = p2
        p2 = tmp
    }
    c0 = p1[l2]

    return c0
}

 

SimilarText

// similar_text()
func SimilarText(first, second string, percent *float64) int {
    var similarText func(string, string, int, int) int
    similarText = func(str1, str2 string, len1, len2 int) int {
        var sum, max int
        pos1, pos2 := 0, 0

        // Find the longest segment of the same section in two strings
        for i := 0; i < len1; i++ {
            for j := 0; j < len2; j++ {
                for l := 0; (i+l < len1) && (j+l < len2) && (str1[i+l] == str2[j+l]); l++ {
                    if l+1 > max {
                        max = l + 1
                        pos1 = i
                        pos2 = j
                    }
                }
            }
        }

        if sum = max; sum > 0 {
            if pos1 > 0 && pos2 > 0 {
                sum += similarText(str1, str2, pos1, pos2)
            }
            if (pos1+max < len1) && (pos2+max < len2) {
                s1 := []byte(str1)
                s2 := []byte(str2)
                sum += similarText(string(s1[pos1+max:]), string(s2[pos2+max:]), len1-pos1-max, len2-pos2-max)
            }
        }

        return sum
    }

    l1, l2 := len(first), len(second)
    if l1+l2 == 0 {
        return 0
    }
    sim := similarText(first, second, l1, l2)
    if percent != nil {
        *percent = float64(sim*200) / float64(l1+l2)
    }
    return sim
}

 

 Github地址

https://github.com/syyongx/php2go


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Errorresponsefromdaemon:ociruntimeerror:container_linux.go:235:startingcontainer ...发布时间:2022-07-10
下一篇:
Go版本管理--处理不兼容发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap