Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Last active February 16, 2017 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaru-b-cl/ebd3b2a52a75b9dc1aa4c85501e64fc7 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/ebd3b2a52a75b9dc1aa4c85501e64fc7 to your computer and use it in GitHub Desktop.
"与えられた文字列中に含まれる単語の個数を単語ごとにカウントする"LINQ
<Query Kind="Expression" />
"I have a pen. I have a apple. oh!! Apple pen! I have a pen. I have a pineapple. oh!! Pineapple pen! Apple pen. Pineapplepen. Pen pineapple apple pen."
.Split(' ')
.Select(s => Regex.Replace(s, "[\\.!]", ""))
.Select(s => s.ToLower())
.ToLookup(w => w)
.Select(g => new { Word = g.Key, Count = g.Count()})
// Word | Count
// ------------ | ------
// i | 4
// have | 4
// a | 4
// pen | 7
// apple | 4
// oh | 2
// pineapple | 3
// pineapplepen | 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment