×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

By LINQ, it’s just a few lines of code.

public Dictionary<string, int> TopWords(int topCount, bool caseSensitive, string content)
{
char[] signs = { ',', '.', '?', ';', '!', '\t', '\n' };
IEnumerable<string> words = content.Trim(signs).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (caseSensitive)
{
return words.GroupBy(w => w.Trim()).OrderByDescending(g => g.Count()).Take(topCount).ToDictionary(g => g.Key, g => g.Count());
}
else
{
return words.GroupBy(w => w.ToLower().Trim()).OrderByDescending(g => g.Count()).Take(topCount).ToDictionary(g => g.First(), g => g.Count());
}
}
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 大家要是闲的利害就帮楼主做做题吧!咱们自己写一个他要求的软件,在《电脑用户》板块,#5584096
    • By LINQ, it’s just a few lines of code.
      public Dictionary<string, int> TopWords(int topCount, bool caseSensitive, string content)
      {
      char[] signs = { ',', '.', '?', ';', '!', '\t', '\n' };
      IEnumerable<string> words = content.Trim(signs).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
      if (caseSensitive)
      {
      return words.GroupBy(w => w.Trim()).OrderByDescending(g => g.Count()).Take(topCount).ToDictionary(g => g.Key, g => g.Count());
      }
      else
      {
      return words.GroupBy(w => w.ToLower().Trim()).OrderByDescending(g => g.Count()).Take(topCount).ToDictionary(g => g.First(), g => g.Count());
      }
      }