给定字符串集合,计算每个单词出现在List <t> </t>中的次数

时间:2013-04-23 05:06:50

标签: c# .net regex linq

输入1:List<string>,例如:

  

&#34; hello&#34;,&#34; world&#34;,&#34; stack&#34;,&#34; overflow&#34;。

输入2:List<Foo>(两个属性,字符串a,字符串b),例如:

  

Foo 1:     a:&#34;你好!&#34;     b:string.Empty

     

Foo 2:     a:&#34;我喜欢Stack Overflow&#34;     b:&#34;它是有史以来最好的网站!&#34;

所以我想以Dictionary<string,int>结束。 List<Foo> a字段中的单词及其在b中显示的次数。

当前的头版代码的第一遍/顶部,这太慢了:

var occurences = new Dictionary<string, int>();
foreach (var word in uniqueWords /* input1 */)
{
    var aOccurances = foos.Count(x => !string.IsNullOrEmpty(x.a) && x.a.Contains(word));
    var bOccurances = foos.Count(x => !string.IsNullOrEmpty(x.b) && x.b.Contains(word));
    occurences.Add(word, aOccurances + bOccurances);
}

2 个答案:

答案 0 :(得分:1)

大致是:

  1. 从第一个输入构建字典(occurrences),可选择使用不区分大小写的比较器。
  2. 对于第二个输入中的每个Foo,使用RegExab分成单词。
  3. 对于每个单词,检查occurrences中是否存在密钥。如果存在,则增加并更新字典中的值。

答案 1 :(得分:0)

您可以尝试将两个字符串a + b连接起来。然后做一个正则表达式将所有单词拉出到一个集合中。然后最终使用查询分组索引。

例如

void Main()
{
    var a = "Hello there!";
    var b =  "It's the best site ever!";

    var ab = a + " " + b;

    var matches = Regex.Matches(ab, "[A-Za-z]+");
    var occurences = from x in matches.OfType<System.Text.RegularExpressions.Match>()
                    let word = x.Value.ToLowerInvariant()
                    group word by word into g
                    select new { Word = g.Key, Count = g.Count() };
    var result = occurences.ToDictionary(x => x.Word, x => x.Count);
    Console.WriteLine(result);
}

建议进行一些更改的示例... 编辑。只是重新阅读要求....有点奇怪,但嘿......

void Main()
{
    var counts = GetCount(new [] {
        "Hello there!",
        "It's the best site ever!"
    });
    Console.WriteLine(counts);
}


public IDictionary<string, int> GetCount(IEnumerable<Foo> inputs)
{
    var allWords =      from input in inputs
                        let matchesA = Regex.Matches(input.A, "[A-Za-z']+").OfType<System.Text.RegularExpressions.Match>()
                        let matchesB = Regex.Matches(input.B, "[A-Za-z']+").OfType<System.Text.RegularExpressions.Match>()
                        from x in matchesA.Concat(matchesB)
                        select x.Value;
    var occurences = allWords.GroupBy(x => x, (x, y) => new{Key = x, Count = y.Count()}, StringComparer.OrdinalIgnoreCase);

    var result = occurences.ToDictionary(x => x.Key, x => x.Count, StringComparer.OrdinalIgnoreCase);
    return result;
}