简体   繁体   English

如何从文本中找到最大出现的单词?

[英]How to find maximum occured word from text?

I have a database field which contains string values. 我有一个包含string值的数据库字段。 I am looking a way to find top 10 maximum occured words from the field 我正在寻找一种方法来找到该字段中排名前10位的最大单词

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: 获得前10名的成绩:

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 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM