Last active
September 2, 2018 23:06
-
-
Save fearofcode/e2e668688edd9d35896f9b9b635836bf to your computer and use it in GitHub Desktop.
simple C# concurrent word counter based on https://www.youtube.com/watch?v=B5xYBrxVSiE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Diagnostics; | |
using System.Collections.Concurrent; | |
namespace wordcounter | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var stopWatch = new Stopwatch(); | |
stopWatch.Start(); | |
// TODO: take command line parameter | |
var textDirectory = @"C:\Users\Warren\Downloads\show-master\episode\9\text\"; | |
var finalTally = new ConcurrentDictionary<string, int>(); | |
Directory.GetFiles(textDirectory).AsParallel().ForAll((filePath) => | |
{ | |
var fileContents = File.ReadAllText(filePath); | |
foreach (var word in fileContents.Split(null)) | |
{ | |
var lowercased = word.ToLower(); | |
finalTally.AddOrUpdate(lowercased, 1, (key, oldValue) => oldValue + 1); | |
} | |
}); | |
foreach (var entry in finalTally) | |
{ | |
if (entry.Value > 1) | |
{ | |
Console.WriteLine(entry); | |
} | |
} | |
stopWatch.Stop(); | |
Console.WriteLine(stopWatch.Elapsed); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment