/// <summary>
        /// Merge two lists of words together summing the frequency on same keys
        /// </summary>
        /// <param name="words1">First words list</param>
        /// <param name="words2">Second words list</param>
        /// <returns></returns>
        private List <Word> MergeWords(List <Word> words1, List <Word> words2)
        {
            if (words1 == null)
            {
                return(words2);
            }
            else if (words2 == null)
            {
                return(words1);
            }

            List <Word> concatWords = words1.Concat(words2).ToList();
            List <Word> returnWords = new List <Word>();
            List <IGrouping <string, Word> > groups = concatWords.GroupBy(x => x.Key).ToList();

            foreach (IGrouping <string, Word> group in groups)
            {
                Word first = group.FirstOrDefault();
                first.SetFrequency(group.Sum(x => x.Frequency));
                first.DocumentFrequency = group.Sum(x => x.DocumentFrequency);
                returnWords.Add(first);
            }

            return(returnWords);
        }
Esempio n. 2
0
        /// <summary>
        /// Get frequencies for all words in word list
        /// </summary>
        /// <param name="words">List of words to get frequencies for</param>
        /// <returns></returns>
        public List <Word> GetWordFrequencies(List <Word> words)
        {
            Dictionary <string, int> wordFrequencies = new Dictionary <string, int>();
            List <Word> wordList = words.GroupBy(x => x.Key).Select(w => w.First()).ToList();

            foreach (Word word in words)
            {
                if (wordFrequencies.TryGetValue(word.Key, out int freq))
                {
                    wordFrequencies[word.Key] = freq + 1;
                }
                else
                {
                    wordFrequencies[word.Key] = 1;
                }
            }
            foreach (KeyValuePair <string, int> kvp in wordFrequencies)
            {
                Word word = wordList.Where(x => x.Key == kvp.Key).First();
                word.SetFrequency(kvp.Value);
            }
            return(wordList);
        }