基于 F23.StringSimilarity.dll  组件。Github 上可以搜索到该组件。

核心方法:

var l = new Levenshtein();
double tempValue = l.Distance("我是中国人", "我是中国人。"); // 将返回 1

下面是我扩展的方法,从一个集合中找到与目标字符串最相似的一个或多个字符串。

比如:["我是中国人", "我是美国人", "我的中国心", "我是中国通"]  ,现在要找到 和 “我是中国” 最接近的字符串(可能有多个)。

如果用我下面的扩展方法。返回值

SimilarityValue = 1,  SimilarityTargetList = ["我是中国人", "我是中国通"]

有需要的请拿走,不谢。

using F23.StringSimilarity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo
{
/// <summary>
/// 相似度结果信息
/// </summary>
/// <typeparam name="TSource">源集合的类型</typeparam>
public class SimilarityResultInfo<TSource>
{
/// <summary>
/// 相似度值。值越小,表示差异越小。等于 1 表示只有一个字符差异。等于 0 表示完全相等。
/// </summary>
public double SimilarityValue { get; set; } /// <summary>
/// 相似度等于 1 表示只有一个字符差异,则最接近的可能有一个或多个字符串
/// </summary>
public IEnumerable<TSource> SimilarityTargetList { get; set; }
} /// <summary>
/// IEnumerable的扩展类,扩展了一个名为 Similarity 的方法
/// </summary>
public static class EnumerableMethodSimilarityExtension
{
/// <summary>
/// 获取集合中和目标字符串最相似的集合(备注:比如:相似度等于 1 表示只有一个字符差异,则最接近的可能有一个或多个字符串)
/// </summary>
/// <param name="source">源集合</param>
/// <param name="targetText">目标字符串</param>
/// <returns>如果 source 没有元素,则返回 NULL。否则,返回值不为 NULL</returns>
public static SimilarityResultInfo<string> Similarity(this IEnumerable<string> source, string targetText)
{
return Similarity<string>(source, c => c, targetText);
} /// <summary>
/// 获取集合中和目标字符串最相似的集合(备注:比如:相似度等于 1 表示只有一个字符差异,则最接近的可能有一个或多个字符串)
/// </summary>
/// <typeparam name="TSource">源集合的类型</typeparam>
/// <param name="source">源集合</param>
/// <param name="textSelector">源集合要比较的属性</param>
/// <param name="targetText">目标字符串</param>
/// <returns>如果 source 没有元素,则返回 NULL。否则,返回值不为 NULL</returns>
public static SimilarityResultInfo<TSource> Similarity<TSource>(this IEnumerable<TSource> source, Func<TSource, string> textSelector, string targetText)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (textSelector == null)
{
throw new ArgumentNullException("textSelector");
}
var l = new Levenshtein(); // 检查 2 个字符串的相似度。
double? minStringSimilarityValue = null;
List<TSource> similarityTargetList = null;
foreach (var item in source)
{
string elementTextValue = textSelector(item);
if (string.IsNullOrEmpty(elementTextValue))
{
continue;
}
double tempValue = l.Distance(elementTextValue, targetText);
if (!minStringSimilarityValue.HasValue)
{
//说明是第一次比较。http://music.cnblogs.com
minStringSimilarityValue = tempValue;
similarityTargetList = new List<TSource>() { item };
continue;
}
if (tempValue < minStringSimilarityValue.Value)
{
minStringSimilarityValue = tempValue;
similarityTargetList.Clear();
similarityTargetList.Add(item);
continue;
}
if (tempValue == minStringSimilarityValue.Value)
{
similarityTargetList.Add(item);
continue;
}
}
if (!minStringSimilarityValue.HasValue)
{
//说明集合是空的
return null;
}
SimilarityResultInfo<TSource> result = new SimilarityResultInfo<TSource>();
result.SimilarityValue = minStringSimilarityValue.Value;
result.SimilarityTargetList = similarityTargetList;
return result;
}
} }

谢谢浏览!

C# 中如何判断字符串的相似度的更多相关文章

  1. 在shell中如何判断字符串是否为有效的IP地址【转】

    转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...

  2. Oracle中如何判断字符串是否全为数字

    Oracle中如何判断字符串是否全为数字 学习了:http://www.cnblogs.com/zrcoffee/archive/2012/12/11/2812744.html 本文介绍了判断字符串是 ...

  3. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  4. asp.net 中如何判断字符串中有几个逗号 (asp也通用)

    如: 字符串 a="1,2,3"; 怎样判断a 中的逗号 有几个 len(a)-len(replace(a,",",""))

  5. JS 中如何判断字符串类型的数字

    function isNumberStr(str){ var n = Number(str); return !isNaN(n); } console.log(isNumberStr('37')); ...

  6. python利用difflib判断两个字符串的相似度

    我们再工作中可能会遇到需要判断两个字符串有多少相似度的情况(比如抓取页面内容存入数据库,如果相似度大于70%则判定为同一片文章,则不录入数据库) 那这个时候,我们应该怎么判断呢? 不要着急,pytho ...

  7. 用递归法判断字符串A中包含多少个字符串B

    string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次. 为此想了一个算法. public static void CountIndex ...

  8. PHP判断字符串中是否包含指定字符串,支持中文哦

    RT,随手写的 /** * 判断字符串中是否包含指定字符串 * @var source 源字符串 * @var target 要判断的是否包含的字符串 * @return bool */ functi ...

  9. [转]Java中怎样判断一个字符串能否转成数字

    原文地址:http://blog.sina.com.cn/s/blog_7bac470701014mjf.html 判断字符串是否为数字 //1.正则表达式  public static boolea ...

随机推荐

  1. kettle 备注

    1. 基本组成 1.1 spoon: 一个可视化的工具,用于编辑kettle ETL的任务脚本 1.2 span: 用以命令行方式执行spoon的转换 1.3 kitchen: 用以命令行方式执行sp ...

  2. linux shell 备注(一)

    1.特殊字符 #!/bin/bash # $表示当前PID ID echo $$ # $n是shell脚本的参数,当0是第一个参数,即文件名 # $#是shell当前脚本的参数个数 # 例如:sh03 ...

  3. ReactNative之从“拉皮条”来看RN中的Spring动画

    上篇博客我们聊了RN中关于Timing的动画,详情请参见于<ReactNative之结合具体示例来看RN中的的Timing动画>本篇博客我们将从一个“拉皮条”的一个动画说起,然后来看一下R ...

  4. Python爬虫11-XML与XPath概述及lxml库的应用

    GitHub代码练习地址:用lxml解析HTML,文件读取,etree和XPath的配合使用:https://github.com/Neo-ML/PythonPractice/blob/master/ ...

  5. WelcomeActivity【欢迎界面】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下欢迎界面的布局以及倒计时和跳过功能. 效果图 代码分析 1.修改APP整个主题为无标题栏样式:styles.xml文件 & ...

  6. django插入数据库错误:mysql的1267错误

    错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...

  7. [翻译] EF Core in Action 关于这本书

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  8. vue-router导航守卫(router.beforeEach())的使用

    好久没写一些东西了,总是感觉有啥缺少的.~~~~恰好碰到最近在写一个移动端项目,遇到了如何使同一个链接在不同条件下跳转到不同路由组件问题,譬如大家经常看到手机中没登录跳转登录页,登陆后跳转个人信息页等 ...

  9. 2.3Options建立配置和实体的映射「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. Startup.cs中创建MVC中间件 关键代码:services.AddMvc();app.UseMvcWith ...

  10. .NET移动开发,关于发布IOS的方法(本人亲身经历折腾很久终于成功)

    前情提要:这位.NET程序员兄弟使用Smobiler开发了一个APP,尽管Smobiler云平台已经最大限度的简化了iOS应用的打包操作,但仍绕不开苹果公司强制要求的p12文件,p12文件需要开发者自 ...