C# 从字符串列表中查找几乎匹配的短语
我想从一个字符串列表中搜索,我的查找短语只有70%的匹配,但我仍然想将其视为已找到。在下面的代码中,我的查找短语是“XXX在中间样本XXX”。如果我使用Contains或Any,则不会产生任何结果。从查找单词中,我希望搜索包含单词“中间”(不区分大小写)的匹配项。我更喜欢3个或更多匹配的连续单词,例如“中间”。请帮忙 C# 预期产出C# 从字符串列表中查找几乎匹配的短语,c#,string,console-application,C#,String,Console Application,我想从一个字符串列表中搜索,我的查找短语只有70%的匹配,但我仍然想将其视为已找到。在下面的代码中,我的查找短语是“XXX在中间样本XXX”。如果我使用Contains或Any,则不会产生任何结果。从查找单词中,我希望搜索包含单词“中间”(不区分大小写)的匹配项。我更喜欢3个或更多匹配的连续单词,例如“中间”。请帮忙 C# 预期产出 Found match: False Found match: False Found match: False Found match: False Found
Found match: False
Found match: False
Found match: False
Found match: False
Found match: True
Found match: True
Found match: True
Found match: False
您可以通过创建一个具有“中间”值的子字符串,然后在字符串列表中找到该子字符串,即字符串列表是否包含该子字符串来实现这一点 试着这样做:
var lookupWord = "in the middle";
var exist = word.ToLower().Contains(lookupWord.ToLower());
每当您比较字符串时,都希望转换为小写或大写,这样您就不会运行区分大小写的问题。我想我已经为您找到了一个解决方案,但是,代码没有优化。但是,你可以优化它。这段代码完全符合您的要求。这是我的代码=>
static void Main(string[] args)
{
bool exist = false;
var wordListToLookUp = new List<string> { "In the middle", "This is a sample text in the middle", "There is secret in the middle of the forest", "None of your business" };
var lookupWord = "xxx in the middle sample xxx";
List<string> checkerarrary = lookupWord.ToLower().Split(' ').ToList();
foreach (var word in wordListToLookUp)
{
exist = false;
List<string> currentStringarrary = word.ToLower().Split(' ').ToList();
if (checkerarrary.Count >= 3 && currentStringarrary.Count>=3)
{
for(int i=0; i<= checkerarrary.Count-3;i++)
{
for (int c = 0; c <= currentStringarrary.Count - 3; c++)
{
if(checkerarrary[i]== currentStringarrary[c]
&& checkerarrary[i+1] == currentStringarrary[c+1]
&& checkerarrary[i + 2] == currentStringarrary[c+2])
{
exist = true;
}
}
}
}
Console.WriteLine("Found match: {0}", exist);
}
}
static void Main(字符串[]args)
{
bool exist=false;
ValdListLoToOkUp=新列表{“中间”,“这是中间的一个示例文本”,“在林中有秘密”,“没有你的事”};
VaR查找词=“XXX在中间样本XXX”;
List checkerarrary=lookupford.ToLower().Split(“”).ToList();
foreach(wordListToLookUp中的var单词)
{
存在=错误;
List CurrentStringArray=word.ToLower().Split(“”).ToList();
如果(CheckerArray.Count>=3&&CurrentStringArray.Count>=3)
{
对于(int i=0;i您如何定义“70%匹配”?有多种,最广为人知的可能是,但您需要决定要使用哪种度量。@如果有三个或更多匹配的词,我会更喜欢某个人。例如“中间”这是三个词短语。@Rollen Garcia我想我回答了您的要求。请检查并让我知道。:)天才!非常感谢。现在我也可以定义连续单词的匹配数了。
static void Main(string[] args)
{
bool exist = false;
var wordListToLookUp = new List<string> { "In the middle", "This is a sample text in the middle", "There is secret in the middle of the forest", "None of your business" };
var lookupWord = "xxx in the middle sample xxx";
List<string> checkerarrary = lookupWord.ToLower().Split(' ').ToList();
foreach (var word in wordListToLookUp)
{
exist = false;
List<string> currentStringarrary = word.ToLower().Split(' ').ToList();
if (checkerarrary.Count >= 3 && currentStringarrary.Count>=3)
{
for(int i=0; i<= checkerarrary.Count-3;i++)
{
for (int c = 0; c <= currentStringarrary.Count - 3; c++)
{
if(checkerarrary[i]== currentStringarrary[c]
&& checkerarrary[i+1] == currentStringarrary[c+1]
&& checkerarrary[i + 2] == currentStringarrary[c+2])
{
exist = true;
}
}
}
}
Console.WriteLine("Found match: {0}", exist);
}
}