views:

34

answers:

2

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! :)

+2  A: 
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);
}
Daniel Renshaw
Wow! Thanks a ton Daniel!
Chocol8
+1, Beat me to it.
Ani
A: 
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);
shahkalpesh