public static Dictionary<string, int> ReadWordCount(string path, bool keepOriginalCase, Func<string, int, bool> includeFunction)
{
Dictionary<string, int> obs = new Dictionary<string, int>();
using (StreamReader file = new StreamReader(path))
{
char[] space = { ' ' }; //the delimeter between the count and word fields
while (!file.EndOfStream)
{
string entry = file.ReadLine().Trim();
if (!entry.StartsWith("#") && !entry.StartsWith("//")) //ignore commented lines
{
//the format of an entry is "<count> <word>"
string[] parts = entry.Split(space, 2);
if (parts.Length == 2)
{
int count = int.Parse(parts[0]);
if (includeFunction(parts[1], count))
{
//add entry to dictionary
if (keepOriginalCase)
{
obs[parts[1]] = count;
}
else
{
obs[parts[1].ToLower()] = count;
}
}
}
else
{
//not a valid entry
Console.Error.WriteLine("Invalid entry found in {0}: {1}", path, entry);
}
}
}
}
return obs;
}