开发者

How to find maximum occured word from text?

开发者 https://www.devze.com 2023-04-01 19:29 出处:网络
I have a database field which contains str开发者_运维百科ing values. I am looking a way to find top 10 maximum occured words from the fieldFirst get all the words from that field:

I have a database field which contains str开发者_运维百科ing values. I am looking a way to find top 10 maximum occured words from the field


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


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


Here you have an easy example with numbers:

class Program
{
    static void Main(string[] args)
    {
        int[] nums = new int[] { 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 1, 7, 12, 451, 13, 
            46, 1, 1, 3, 2, 3, 4, 5, 3, 2, 4, 4, 5, 6, 6, 8, 9, 0};
        var numberGroups =
            (from n in nums
            group n by n into g
            orderby g.Count() descending
            select new { Number = g.Key, Count = g.Count() }
            ).Take(10);

        Console.ReadLine();
    }
}

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消