Counting Word Frequency (most significant words) in a String, excluding keywords

不问归期 提交于 2019-12-11 06:16:10

问题


I would like to count the frequency of words (excluding some keywords) in a string and sort them DESC. So, how can i do it?

In the following string...

This is stackoverflow. I repeat stackoverflow.

Where the excluding keywords are

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

the output should be like

stackoverflow  
repeat         
this           

P.S. NO! I am not re-designing google! :)


回答1:


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);
}



回答2:


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);


来源:https://stackoverflow.com/questions/3607765/counting-word-frequency-most-significant-words-in-a-string-excluding-keywords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!