// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace TextFun2 { public static class MissingStuff { public static T2 GetOrDefault(this IDictionary dic, T1 key, T2 defval) { if(dic.ContainsKey(key)) { return dic[key]; } else { return defval; } } } public class Program { private static Regex w = new Regex("[^a-z\\u00E0-\\u00FF]+", RegexOptions.Compiled); public static void Main(string[] args) { IDictionary count = new Dictionary(); foreach(string line in File.ReadAllLines(args[0])) { foreach(string word in w.Split(line.ToLower())) { if(word.Length > 0) { count[word] = count.GetOrDefault(word, 0) + 1; } } } foreach(KeyValuePair kvp in count.OrderByDescending(e => e.Value).Take(int.Parse(args[1]))) { Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value); } } } }