gpt4 book ai didi

C# 查找两个字符串之间匹配单词的百分比

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:57 24 4
gpt4 key购买 nike

我有一个字符串,我想将其与字符串列表进行比较以找到最佳匹配。

例如,

string search = "Orange Black Red One Five"

字符串列表可以包含以下内容

l[0] = "Orange Seven Three Black"
l[1] = " Nine Eight Seven Six"
l[2] = " Black Blue Purple Red Five Four Nine Ten"

l[0] contains 2 matches
l[1] contains 0 matches
l[2] contains 3 matches

所以程序会选择 l[2] 作为最佳匹配,匹配率为 60%。
我将如何比较两个这样的字符串?

最佳答案

    var s = search.Split(new string[] { " "}, StringSplitOptions.RemoveEmptyEntries);

var res1 = (from string part in l
select new
{
list = part,
count = part.Split(new char[] {' '}).Sum(p => s.Contains(p) ? 1 : 0)

}).OrderByDescending(p=> p.count).First();

Console.Write(res1.count);

关于C# 查找两个字符串之间匹配单词的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29544275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com