public static void WriteToCsv <TKey>(this IFrequencyStatistics <TKey> stat, TextWriter writer, int take = -1) { foreach (var pair in take == -1 ? stat.KeyFrequency : stat.KeyFrequency.Take(take)) { writer.WriteLine($"{pair.Key}, {pair.Value}"); } }
public static void Add <TKey>(this IFrequencyStatistics <TKey> stat, IEnumerable <KeyValuePair <TKey, int> > pairs) { foreach (var pair in pairs) { stat.Add(pair.Key, pair.Value); } }
public static void ReadFromCsv <TKey>(this IFrequencyStatistics <TKey> stat, TextReader reader, Func <string, TKey> keyParser) { stat.Clear(); while (reader.Peek() != -1) { var ss = reader.ReadLine().Split(','); var key = keyParser(ss[0]); var freq = int.Parse(ss[1]); stat.Add(key, freq); } }