Exemple #1
0
        /// <summary>
        /// Processes the file and creates occurences for all words in the file and then
        /// puts them into a dictionary
        /// </summary>
        /// <param name="filename">filesname of the file to be read</param>
        /// <param name="dictionary">Dictionary for the occurence objects to be read into</param>
        /// <param name="fileNumber">Which files is being read</param>
        /// <returns>Word count</returns>
        private int processFile(string filename, ref Dictionary <string, Occurences> dictionary, int fileNumber) //return total # of word in that file
        {
            int    totalWords = 0;
            string temp       = "";

            using (StreamReader input = new StreamReader(filename))
            {
                while (input.EndOfStream != true)
                {
                    temp = input.ReadLine();
                    temp = temp.ToLower();
                    string[] result = Regex.Split(temp, "[^abcdefghijklmnopqrstuvwxyz]+");

                    for (int i = 0; i < result.Length; i++)
                    {
                        Occurences outResult;
                        if (result[i] != "")
                        {
                            totalWords++;
                            if (!dictionary.TryGetValue(result[i], out outResult))
                            {
                                Occurences tempOcc = new Occurences(result[i], 2);
                                dictionary.Add(result[i], tempOcc);
                            }
                            dictionary[result[i]].incrementOccurences(fileNumber);
                        }
                    }
                }
            }
            return(totalWords);
        }
        private float[] _frequency; //frequency of the word in the file

        /// <summary>
        /// Makes a new frequency object with all variables filled
        /// </summary>
        /// <param name="occ">Occurence object that the frequency object gets the data from</param>
        /// <param name="words">Number of words in each file</param>
        public Frequency(Occurences occ, int[] words)
        {
            _word = occ.Word;

            if (words.Length != occ.getNumberOfFiles())
            {
                throw new ArgumentException();
            }
            _frequency = new float[words.Length];

            for (int i = 0; i < words.Length; i++)
            {
                _frequency[i] = (((float)occ[i]) / ((float)words[i]));
            }
        }