搜索包含特定单词的短语的有效方法

时间:2016-03-06 10:53:44

标签: c# string search collections

我需要以最有效的方式在C#中完成它。

假设:

  1. Collection1:{"I am good", He is best", They are poor", "Mostly they are average", "All are very nice"}
  2. Collection2:{"good", "best" ,"nice"}
  3. 我想搜索Collection2中的所有Collection1项,并将匹配的结果存储在Collection3中,因此Collection3将是这样的:

    Collection3:{"I am good", "I am best", "All are very nice"}

3 个答案:

答案 0 :(得分:0)

IList<String> Collection3;

for(int i = 0 ; i < Collectio2.Count ; i++)
{
   foreach(String str in Collection1)
   {
      if(str.Contains(Collection2[i]))
      {
         Collection3.Add(str);
      }
   }
}

答案 1 :(得分:0)

最好的方法。

string[] Collection1 = {"I am good", "He is best", "They are poor", "Mostly they are average", "All are very nice"};
string[] Collection2 = { "good", "best", "nice" };

var Collection3 = Collection1.Select(x => x.ToLower())
                   .Where(x => Collection2.Any(y => x.Contains(y))).ToArray();

答案 2 :(得分:0)

假设你的Collection2项是那个单词通常含义的单词[没有双关语意图],你可以使用LINQ ToLookup - 这将为你提供一个正确的MultiValueDictionary模拟,并用你可以尝试类似的东西:

var phrases = new[] { "I am good", "He is best", "They are poor", "Mostly they are average", "All are very nice", "Not so\tgood \t", };

var lookup = phrases
    .Select((phrase, index) =>
        new
        {
            phrase,
            index,
            words = phrase.Split((Char[])null, StringSplitOptions.RemoveEmptyEntries)
        })
    .SelectMany(item =>
        item
            .words
            .Select(word =>
                new
                {
                    word,
                    item.index,
                    item.phrase
                }))
    .ToLookup(
        keySelector: item => item.word,
        elementSelector: item => new { item.phrase, item.index });

var wordsToSearch = new[] { "good", "best", "nice" };

var searchResults = wordsToSearch
    .Select(word =>
        new
        {
            word,
            phrases = lookup[word].ToArray()
        });

foreach (var result in searchResults)
{
    Console.WriteLine(
        "Word '{0}' can be found in phrases : {1}",
        result.word,
        String.Join(
            ", ",
            result
                .phrases
                .Select(phrase => 
                    String.Format("{0}='{1}'", phrase.index, phrase.phrase))));
}      

它为您提供了索引和短语,因此您可以根据需要进行调整。

但如果你的Collection2不是由单词组成,而是由短语组成,那么你需要更强大的东西,比如lucene.net,可以正确处理全文搜索。