1

我想计算字符串中单词(不包括某些关键字)的频率并将它们排序为 DESC。那么,我该怎么做呢?

在以下字符串中...

This is stackoverflow. I repeat stackoverflow.

排除关键字在哪里

ExKeywords() ={"i","is"}

输出应该像

stackoverflow  
repeat         
this           

PS不!我不是在重新设计谷歌!:)

4

2 回答 2

4
string input = "This is stackoverflow. I repeat stackoverflow.";
string[] keywords = new[] {"i", "is"};
Regex regex = new Regex("\\w+");

foreach (var group in regex.Matches(input)
    .OfType<Match>()
    .Select(c => c.Value.ToLowerInvariant())
    .Where(c => !keywords.Contains(c))
    .GroupBy(c => c)
    .OrderByDescending(c => c.Count())
    .ThenBy(c => c.Key))
{
    Console.WriteLine(group.Key);
}
于 2010-08-31T09:55:59.523 回答
0
string s = "This is stackoverflow. I repeat stackoverflow.";
string[] notRequired = {"i", "is"};

var myData =
    from word in s.Split().Reverse()
    where (notRequired.Contains(word.ToLower()) == false)
    group word by word into g
    select g.Key;

foreach(string item in myData)
    Console.WriteLine(item);
于 2010-08-31T10:16:25.393 回答