×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

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