ABB.Swum.Utilities.LibFileLoader.ReadWordCount C# (CSharp) Method

ReadWordCount() public static method

Reads a word count file and parses it into a dictionary. The file is assumed to contain one entry per line, with each entry of the format "[count] [word]". Count must be a non-negative whole number. If the file contains duplicate entries for a given word, only the last entry will be in the dictionary.
public static ReadWordCount ( string path, bool keepOriginalCase, Func includeFunction ) : int>.Dictionary
path string The file to read.
keepOriginalCase bool If True, words are added to the dictionary in their original case. If False, words are converted to lower case.
includeFunction Func A function specifying whether or not to include a given entry from the file. /// This takes a string and uint as parameters and returns True if the entry should be included and False otherwise.
return int>.Dictionary
        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;
        }

Same methods

LibFileLoader::ReadWordCount ( string path ) : int>.Dictionary
LibFileLoader::ReadWordCount ( string path, bool keepOriginalCasing ) : int>.Dictionary