Skip to content

Instantly share code, notes, and snippets.

@hodzanassredin
Last active August 29, 2015 14:06
Show Gist options
  • Save hodzanassredin/c79c34ce02185cc092af to your computer and use it in GitHub Desktop.
Save hodzanassredin/c79c34ce02185cc092af to your computer and use it in GitHub Desktop.
bagofwords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication4{
class Program{
static string text = @"Functional programming is like describing your problem to a
mathematician. Imperative programming is like giving instructions to
an idiot.
-- arcus, #scheme on Freenode";
private static IEnumerable<string> BagOfWords()
{
return Regex.Split(text.ToLower(), @"\W+")
.GroupBy(x => x)
.GroupBy(x => x.Count())
.OrderByDescending(x => x.Key)
.Select(x => String.Format("{0}:{1}", x.Key, String.Join(",", x.Select(y => y.Key))));
}
static void Main(string[] args){
foreach (var str in BagOfWords())
{
Console.WriteLine(str);
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment