c#字符相似度对比
字符串相似度算法使用 Levenshtein Distance算法(中文翻译:编辑距离算法) 这算法是由俄国科学家Levenshtein提出的.
下面使用C#实现
public class LevenshteinDistance
{
private static LevenshteinDistance _instance=null;
public static LevenshteinDistance Instance
{
get
{
if (_instance == null)
{
return new LevenshteinDistance();
}
return _instance;
}
}
/// <summary>
/// 取最小的一位数
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="third"></param>
/// <returns></returns>
public int LowerOfThree(int first, int second, int third)
{
int min = first;
if (second < min)
min = second;
if (third < min)
min = third;
return min;
}
public int Levenshtein_Distance(string str1, string str2)
{
int[,] Matrix;
int n=str1.Length;
int m=str2.Length;
int temp = 0;
char ch1;
char ch2;
int i = 0;
int j = 0;
if (n ==0)
{
return m;
}
if (m == 0)
{
return n;
}
Matrix=new int[n+1,m+1];
for (i = 0; i <= n; i++)
{
//初始化第一列
Matrix[i,0] = i;
}
for (j = 0; j <= m; j++)
{
//初始化第一行
Matrix[0, j] = j;
}
for (i = 1; i <= n; i++)
{
ch1 = str1[i-1];
for (j = 1; j <= m; j++)
{
ch2 = str2[j-1];
if (ch1.Equals(ch2))
{
temp = 0;
}
else
{
temp = 1;
}
Matrix[i,j] = LowerOfThree(Matrix[i - 1,j] + 1, Matrix[i,j - 1] + 1, Matrix[i - 1,j - 1] + temp);
}
}
for (i = 0; i <= n; i++)
{
for (j = 0; j <= m; j++)
{
Console.Write(" {0} ", Matrix[i, j]);
}
Console.WriteLine("");
}
return Matrix[n, m];
}
/// <summary>
/// 计算字符串相似度
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public decimal LevenshteinDistancePercent(string str1,string str2)
{
int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
int val = Levenshtein_Distance(str1, str2);
return 1 - (decimal)val / maxLenth;
}
}
class Program
{
static void Main(string[] args)
{
string str1 = "你好蒂蒂";
string str2="你好蒂芬";
Console.WriteLine("字符串1 {0}", str1);
Console.WriteLine("字符串2 {0}", str2);
Console.WriteLine("相似度 {0} %", LevenshteinDistance.Instance.LevenshteinDistancePercent(str1, str2)*100);
Console.ReadLine();
}
}
转自:http://blog.csdn.net/Feiin/article/details/5169649
可另外参考:http://www.cnblogs.com/stone_w/archive/2012/08/16/2642679.html
c#字符相似度对比的更多相关文章
- iOS 使用百度的人脸识别登录验证,解决认证失败不跳转界面连续认证,认证相似度对比
在使用百度人脸识别出现的问题:小米6调用摄像机是黑白的一个情况,iOS上会出现识别准确性上的问题(多次代开认证,会通过) 人脸识别(活体验证): 1.芝麻认证 : 0.4元/次,需要企业企业认证.不能 ...
- Python OpenCV 图像相识度对比
强大的openCV能做什么我就不啰嗦,你能想到的一切图像+视频处理. 这里,我们说说openCV的图像相似度对比, 嗯,说好听一点那叫图像识别,但严格讲, 图像识别是在一个图片中进行类聚处理,比如图片 ...
- OpenCV进行图像相似度对比的几种办法
转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/43853435, 来自:shiter编写程序的艺术 对计算图像相似度的方法,本文做 ...
- 题目1049:字符串去特定字符——九度OJ
题目1049:字符串去特定字符 http://ac.jobdu.com/problem.php?pid=1049 时间限制:1 秒 内存限制:32 兆 题目描述: 输入字符串s和字符c,要求去掉s中所 ...
- 【剑指Offer面试编程题】题目1283:第一个只出现一次的字符--九度OJ
题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据 每一组输入一个字符串. 输出: 输出第一个只出现一次的 ...
- sql server 相似度对比
转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx 函數一.產生 Like 比對用字串 ) ) ) as begin / ...
- JAVA 字节流和字符流度读写的区别
java处理文件的类里面,stream结尾都是采用字节流,reader和writer结尾都是采用字符流.两者的区别就是读写的时候一个是按字节读写,一个是按字符. 字符流的底层就是字节流.而字符流主要是 ...
- 用Python实现一个简单的——人脸相似度对比
近几年来,兴起了一股人工智能热潮,让人们见到了AI的能力和强大,比如图像识别,语音识别,机器翻译,无人驾驶等等.总体来说,AI的门槛还是比较高,不仅要学会使用框架实现,更重要的是,需要有一定的数学基础 ...
- 使用FaceNet 图像相识度对比
1. 模型结构:
随机推荐
- 安装Visual C ++进行跨平台移动开发
Visual Studio 2015 Visual Studio文档的新家是docs.microsoft.com上的Visual Studio 2017文档 . 有关Visual Studio 2 ...
- Eclipse 模拟http 请求插件Rest Client
eclipse update 网址 http://nextinterfaces.com/http4e/install/ 参考 http://www.nextinterfaces.com/eclips ...
- NGINX通过Stream转发ftp请求
一.NGINX 1.9之前,需要安装第三方的TCP插件: http://www.cnblogs.com/i-blog/p/6165378.html 二.1.9之后直接使用Stream配置就可以了,当然 ...
- elasticsearch pinyin 拼音分词器
安装pinyin分词 地址:https://github.com/medcl/elasticsearch-analysis-pinyin PUT py_test { "index" ...
- js操作history
js操作history pushState pushState只会在当前history中添加一条记录,并不会刷新浏览器 history.pushState({}, "my title&quo ...
- DFS leetcode
把字符串转换成整数 class Solution { public: int StrToInt(string str) { int n = str.size(), s = 1; long long r ...
- C过程思想,根据需求写方法就行
实现的方法有多种 Comprehensive orientate 2017/10/27 13:25:07 C过程思想,根据需求写方法就行
- Springboot项目打成jar包运行 和 打成war包 外部tomcat运行
Jar打包方式运行 类型为jar时 <packaging>jar</packaging> 1.使用命令mvn clean package 打包 2.使用java –jar 包 ...
- delphi OleVariant转换RecordSet
delphi OleVariant转换RecordSet uses Data.Win.ADODB; function varToRecordSet( parms : OleVariant ) : Da ...
- latex中如何引用公式
在使用latex编辑文章时,经常会需要引用公式.图表等等. 如果我们人为地对这些公式.图表进行编号1-2-3-4,然后在文章中使用Eq(1)-Eq(2)-Eq(3)-Eq(4)去引用这些公式,固然是可 ...