如何计算字符串中每个单词的出现次数? [英] How to count number of occurrence of each word in string?

查看:144
本文介绍了如何计算字符串中每个单词的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从字符串输入中提取单词,如何获得每个单词的出现次数呢?

I use the following code to extract words from string input, how can I get the occurrence of each words too?

var words = Regex.Split(input, @"\W+")
                        .AsEnumerable()
                        .GroupBy(w => w)
                        .Where(g => g.Count() > 10)
                        .Select(g => g.Key);

推荐答案

您可以使用 string.Split 代替 Regex.Split 来获取每个单词的计数,例如:

Instead of Regex.Split you can use string.Split and get the count for each word like:

string str = "Some string with Some string repeated";
var result  = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                .GroupBy(r => r)
                .Select(grp => new
                    {
                        Word = grp.Key,
                        Count = grp.Count()
                    });

如果您想过滤出那些至少重复10次的单词,则可以在 Select 之前添加一个条件,例如 Where(grp => grp.Count> = 10)

If you want to filter out those words which are repeated 10 times atleast then you can add a condition before Select like Where(grp=> grp.Count >= 10)

对于输出:

foreach (var item in result)
{
    Console.WriteLine("Word: {0}, Count:{1}", item.Word, item.Count);
}

输出:

Word: Some, Count:2
Word: string, Count:2
Word: with, Count:1
Word: repeated, Count:1

对于不区分大小写的分组,您可以将当前的GroupBy替换为:

For case insensitive grouping you can replace the current GroupBy with:

.GroupBy(r => r, StringComparer.InvariantCultureIgnoreCase)

因此您的查询将是:

var result = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                .GroupBy(r => r, StringComparer.InvariantCultureIgnoreCase)
                .Where(grp => grp.Count() >= 10)
                .Select(grp => new
                    {
                        Word = grp.Key,
                        Count = grp.Count()
                    });

这篇关于如何计算字符串中每个单词的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆