Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.