Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
Created December 22, 2009 06:19
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 NathanHowell/261545 to your computer and use it in GitHub Desktop.
Save NathanHowell/261545 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
namespace WordCountCS
{
class Program
{
static void Main(string[] args)
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
Directory.EnumerateFiles(@"C:\TEMP\20_newsgroups", "*", SearchOption.AllDirectories)
.AsParallel()
.AsUnordered()
.SelectMany(fn => File.ReadLines(fn))
.SelectMany(line => line.Split(' ', '.', ':', ';', '?'))
.GroupBy(key => key, element => element, (word, grouping) => new { word, count = grouping.Count() })
.ForAll(x => Console.WriteLine("{0}:{1}", x.word, x.count));
Console.Error.WriteLine("Elapsed: {0}", sw.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment