Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法 博客分类: java 搜索引擎,爬虫

主题:Levenshtein Distance(LD);

相关介绍:Levenshtein distance是由俄国科学家Vladimir Levenshtein在1965年设计并以他的名字命名的。如果不能拼写或发Levenshtein音,通常可以称它edit distance(编辑距离);

用途:该算法用于判断两个字符串的距离,或者叫模糊度。个人理解就是差异程度。而差异的标准就是1)加一个字母(Insert),2)删一个字母(Delete),3改变一个字母(Substitute)。

算法描述

Step

Description

1

Set n to be the length of s.Set m to be the length of t.
If n = 0, return m and exit.If m = 0, return n and exit.
Construct a matrix containing 0..m rows and 0..n columns.

2

Initialize the first row to 0..n.
Initialize the first column to 0..m.

3

Examine each character of s (I from 1 to n).

4

Examine each character of t (j from 1 to m).

5

If s[i] equals t[j], the cost is 0.
If s[i] doesn’t equal t[j], the cost is 1.

6

Set cell d[I,j] of the matrix equal to the minimum of:
a. The cell immediately above plus 1: d[i-1,j] + 1.
b. The cell immediately to the left plus 1: d[I,j-1] + 1.
c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.

7

After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].

1、  得到源串s长度n与目标串t的长度m,如果一方为的长度0,则返回另一方的长度。

2、  初始化(n+1)*(m+1)的矩阵d,第一行第一列的值为0增至对应的长度。

3、  遍历数组中的每一个字符(i,j从1开始)。如果s[i]与t[j]的值相等,cost值为0,否则为1。D[i][j]的值为d[i-1,j] + 1(左边的值加1)、d[I,j-1] + 1.(上边的值加1)、d[i-1,j-1] + cost (斜上角的值加cost) 中的最小者。

4、  等第三步遍历完后,右下角d[n,m]的值就为两个字符串的距离。

应用演示:source:word与target:world比较过程。

应用举例:据《开发自己的搜索引擎——Lucene 2.0+Heriterx

》记载P134页记载,lucene中FuzzyQuery(模糊匹配)就是应用该算法的;也可用于Spell checking(拼写检查),Speech recognition(语句识别),DNA analysis(DNA分析) ,Plagiarism detection(抄袭检测)。

参考资料

http://www.merriampark.com/ld.htm

http://my.oschina.net/MrMichael/blog/339217

转载于:https://my.oschina.net/xiaominmin/blog/1597443

Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法的更多相关文章

  1. 扒一扒编辑距离(Levenshtein Distance)算法

    最近由于工作需要,接触了编辑距离(Levenshtein Distance)算法.赶脚很有意思.最初百度了一些文章,但讲的都不是很好,读起来感觉似懂非懂.最后还是用google找到了一些资料才慢慢理解 ...

  2. Levenshtein distance 编辑距离算法

    这几天再看 virtrual-dom,关于两个列表的对比,讲到了 Levenshtein distance 距离,周末抽空做一下总结. Levenshtein Distance 介绍 在信息理论和计算 ...

  3. Levenshtein Distance (编辑距离) 算法详解

    编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...

  4. Levenshtein Distance + LCS 算法计算两个字符串的相似度

    //LD最短编辑路径算法 public static int LevenshteinDistance(string source, string target) { int cell = source ...

  5. C#实现Levenshtein distance最小编辑距离算法

    Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...

  6. 利用Levenshtein Distance (编辑距离)实现文档相似度计算

    1.首先将word文档解压缩为zip /** * 修改后缀名 */ public static String reName(String path){ File file=new File(path) ...

  7. Levenshtein Distance算法(编辑距离算法)

    编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符, ...

  8. 最喜欢的算法(们) - Levenshtein distance

    String Matching: Levenshtein distance Purpose: to use as little effort to convert one string into th ...

  9. Magic Number(Levenshtein distance算法)

    Magic Number Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. MariaDB使用数据库查询《三》

                                                                 MariaDB使用数据库查询 案例5:使用数据库查询 5.1 问题 本例要求配 ...

  2. 搭建环境-git常见使用总结

    Descripton:git 一.Git安装和本地用户全局配置 官网下载并且安装 查看是否安装成功win + R输入git,出现git命令指南,则安装成功 全局配置本地用户,在git Bash中进行下 ...

  3. python3(十一)generator

    # 只要把一个列表生成式的[]改成() L = [x * x for x in range(10)] print(L) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] g ...

  4. 【python实现卷积神经网络】padding2D层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  5. Python队列的三种队列方法

    今天讲一下队列,用到一个python自带的库,queue 队列的三种方法有: 1.FIFO先入先出队列(Queue) 2.LIFO后入先出队列(LifoQueue) 3.优先级队列(PriorityQ ...

  6. IE各版本CSS Hack(兼容性处理)语法速查表

    为了兼容IE各个版本,需要在CSS中添加额外的代码,比如以前常用的_width.之所以工作,是因为浏览器会忽略不能解析的样式规则,因此举个例子来说,把_width写在width下面,对于非IE浏览器会 ...

  7. sql 系统表协助集合

    一.判断字段是否存在: select * from syscolumns where id=object_id('表') and name='字段'

  8. Delphi Unicode转中文

    function UniCode2GB(S : String):String;Var I: Integer;beginI := Length(S);while I >=4 do begintry ...

  9. 如何将SqlServer配置为django的数据源(2.2以后版本)

    django-pyodbc-azure 是一个官方推荐的 第三方django数据库支持backend. 根据官网的介绍django-pyodbc-azure 只能支持到 django 2.1.如果涉及 ...

  10. Python爬取养眼图片

    1.准备 各位绅士们,你可能会觉得疫情在家无聊,那么现在我们的Python语言可以满足你们的需求.项目需要的工具(1)Python3(2)requests库requests库可以通过代码pip ins ...