超碰人人人人人,亚洲AV午夜福利精品一区二区,亚洲欧美综合区丁香五月1区,日韩欧美亚洲系列

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 中如何判斷字符串的相似度

admin
2023年3月22日 17:12 本文熱度 1339

基于 F23.StringSimilarity.dll  組件,Github 上可以搜索到該組件。

核心方法:

var l = new Levenshtein();

double tempValue = l.Distance("我是中國人", "我是中國人。"); // 將返回 1?

下面是我擴展的方法,從一個集合中找到與目標字符串最相似的一個或多個字符串。

比如:["我是中國人", "我是美國人", "我的中國心", "我是中國通"]  ,現(xiàn)在要找到 和 “我是中國” 最接近的字符串(可能有多個)。

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

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>

    /// 相似度結(jié)果信息

    /// </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;

        }

    }

}?

謝謝瀏覽!


該文章在 2025/2/24 14:46:40 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務費用、相關(guān)報表等業(yè)務管理,結(jié)合碼頭的業(yè)務特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved