levenshtein() - 计算两个字符串之间的编辑距离 - php 字符串函数
levenshtein()
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
计算两个字符串之间的编辑距离
说明
levenshtein(string$str,string$str): intlevenshtein(string$str,string$str,int$cost_ins,int$cost_rep,int$cost_del): int编辑距离,是指两个字串之间,通过替换、插入、删除等操作将字符串$str1转换成$str2所需要操作的最少字符数量。该算法的复杂度是O(m*n),其中n和m分别是$str1 和$str2的长度(当和算法复杂度为O(max(n,m)**3)的similar_text()相比时,此函数还是相当不错的,尽管仍然很耗时。)。
在最简单的形式中,该函数只以两个字符串作为参数,并计算通过插入、替换和删除等操作将$str1转换成$str2所需要的操作次数。
第二种变体将采用三个额外的参数来定义插入、替换和删除操作的次数。此变体比第一种更加通用和适应,但效率不高。
参数
求编辑距离中的其中一个字符串
$str2求编辑距离中的另一个字符串
$cost_ins定义插入次数
$cost_rep定义替换次数
$cost_del定义删除次数
返回值
此函数返回两个字符串参数之间的编辑距离,如果其中一个字符串参数长度大于限制的255个字符时,返回-1。
范例
levenshtein()例子:
以上例程会输出:
Input word: carrrot Did you mean: carrot?
参见
soundex()
Calculate the soundex key of a stringsimilar_text()
计算两个字符串的相似度metaphone()
Calculate the metaphone key of a string
The levenshtein function processes each byte of the input string individually. Then for multibyte encodings, such as UTF-8, it may give misleading results. Example with a french accented word : - levenshtein('notre', 'votre') = 1 - levenshtein('notre', 'nôtre') = 2 (huh ?!) You can easily find a multibyte compliant PHP implementation of the levenshtein function but it will be of course much slower than the C implementation. Another option is to convert the strings to a single-byte (lossless) encoding so that they can feed the fast core levenshtein function. Here is the conversion function I used with a search engine storing UTF-8 strings, and a quick benchmark. I hope it will help. Results (for about 6000 calls) - reference time core C function (single-byte) : 30 ms - utf8 to ext-ascii conversion + core function : 90 ms - full php implementation : 3000 ms
Please, be aware that: This is a classic implentation in which several tricks are used: 1. the strings are exploded into multi-byte characters in O(n lg n) time 2. instead of searching for the longest path in a precomputed two-dimensional array, we search for the best point which lays in the middle column. This is achieved by spliting the second string in half, and recursively calling the algorithm twice. The only thing we need from the recursive call are the values in the middle column. The trick is to return the last column from each recursive call, which is what we need for the left part, but requires one more trick for the right part - we simply mirror the strings and the array so that the last column is the first column. Then we just find the row which maximizes the sum of lenghts in each part. 3. one can prove that the time consumed by the algorithm is proportional to the area of the (imaginary) two-dimensional array, thus it is O(n*m).
Simple levenshtein function without string length limit ...
I have made a function that removes the length-limit of levenshtein function and ajust the result with similar_text:
Here is a string resynch function:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)