比较两个略有不同的字符串 [英] comparing two slightly different strings

查看:88
本文介绍了比较两个略有不同的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个字符串并计算它们之间的差异百分比.对于两个相同长度的字符串,这本来会更容易.但是如果长度不同怎么办?例如:

你好,我叫阿米特,我想帮助所有人

我的名字是阿米特,我想为每位编码员提供帮助

我想计算两者之间的差异百分比

一个简单的逻辑就是将第一句话中的每个单词与不同句子中相同位置的单词进行比较.但是在这种情况下,长度是不同的.那我该如何进行呢?

我们将不胜感激.

i want to compare two strings and calculate the percentage of difference between them. This could have been easier for two strings of same length. But what if the length is different? For eg :

hello my name is amit and i like to help everybody

hi my name is amit and i would like to help every coder

I want to calculate the percentage of difference between them

A simple logic would be to compare each word in first sentence with same position word of different sentence. But in this case the length is different. So how do i proceed?

Any help would be appreciated.

推荐答案

基本上,您需要确定一个字符串量度,然后为您的字符串计算差值.有许多可能的指标,因此取决于您要如何定义不同"程度.

请参见此处开始 [ ^ ].如前所述,Levenshtein指标可能是最常见的.
Basically you need to decide on a string metric and then calculate the difference for your strings. There are many possible metrics, so it depends on how you want to define degrees of "different."

See here for start[^]. As mentioned, the Levenshtein metric is probably the most common.


请参见此处 [ ^ ].

如果可以找回数组(包含作者提供的所有差异),则可以通过计算差异与原始字符串长度的关系来计算百分比.
See here[^].

If you can get an array back (containing all the differences as is provided by the author here), you can calculate the percentage by calculating the differences versus the original string lengths.


这篇关于比较两个略有不同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆