gpt4 book ai didi

c# - 基于相似性比较字符串

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:04 25 4
gpt4 key购买 nike

我有两个列表,它们看起来像这样

<List> ads
[0]
Headline = "Sony Ericsson Arc silver"
[1]
Headline = "Sony Ericsson Play R800I"


<List> feedItems
[0]
Headline = "Sony Ericsson Xperia Arc Silver"
[1]
Headline = "Sony Ericsson Xperia Play R800i Black"

创建新的第三个列表的最简单方法是什么,其中的元素与至少两个 词相互匹配?您能否以 LINQ 方式完成此操作?

第三个列表看起来像这样

[0]
AdHeadline = "Sony Ericsson Arc silver"
MatchingFeed = "Sony Ericsson Xperia Arc Silver"
// etc

我已经尝试遍历第一个列表并使用 Regex.Match 类,如果我找到一个匹配项,我将填充第三个列表 - 我想知道你更喜欢这样做的方式是什么是,以及如何检查最小值。表达式中有 2 个以上的词。

最佳答案

我不确定正则表达式能为这里的聚会带来什么。下面的怎么样?

// Define a helper function to split a string into its words.
Func<string, HashSet<string>> GetWords = s =>
new HashSet<string>(
s.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)
);

// Pair up each string with its words. Materialize the second one as
// we'll be querying it multiple times.
var aPairs = ads.Select(a => new { Full = a, Words = GetWords(a) });
var fPairs = feedItems
.Select(f => new { Full = f, Words = GetWords(f) })
.ToArray();

// For each ad, select all the feeds that match more than one word.
// Then just select the original ad and feed strings.
var result = aPairs.SelectMany(
a => fPairs
.Where(f => a.Words.Intersect(f.Words).Skip(1).Any())
.Select(f => new { AdHeadline = a.Full, MatchingFeed = f.Full })
);

关于c# - 基于相似性比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134789/

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