Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find maximum occured word from text?

Tags:

c#

asp.net

linq

I have a database field which contains string values. I am looking a way to find top 10 maximum occured words from the field

like image 585
huMpty duMpty Avatar asked Sep 02 '11 12:09

huMpty duMpty


2 Answers

First get all the words from that field:

IEnumerable<string> allWords = from entry in table
                               from word in entry.Field.Split(' ')
                               select word;

Then group them by their counts:

IEnumerable<string> result = from word in allWords
                             group word by word into grouped
                                 let count = grouped.Count()
                                 orderby count descending
                                 select grouped.Key;

Get top 10 results:

result.Take(10);
like image 132
Ufuk Hacıoğulları Avatar answered Oct 11 '22 14:10

Ufuk Hacıoğulları


var result =
    Regex.Matches(s, @"\b\w+\b").OfType<Match>()
        .GroupBy(k => k.Value, (g, u) => new { Word = g, Count = u.Count() })
        .OrderBy(n => n.Count)
        .Take(10);
like image 45
Kirill Polishchuk Avatar answered Oct 11 '22 16:10

Kirill Polishchuk