×

Loading...
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!

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