/// <summary> /// /// </summary> /// <param name="key">The key to add to the matrix</param> /// <param name="sentenceIndex">Sentence index where this key is located</param> /// <returns>The frequency of the word</returns> public int AddToMatrix(string key, int sentenceIndex) { if (string.IsNullOrEmpty(key.ToString())) { return(0); } string similarKey; if (ContainsSimilarKey(key, out similarKey)) { Matrix[similarKey].Frequency++; if (!Matrix[similarKey].Locations.Contains(sentenceIndex)) { Matrix[similarKey].Locations.Add(sentenceIndex); } return(Matrix[similarKey].Frequency); } else { Matrix[key] = new FrequencyLocation(); Matrix[key].Frequency = 1; Matrix[key].Locations.Add(sentenceIndex); return(1); } }
public int AddToMatrix(string prevWord, string word, int sentenceIndex) { // Check for valid prevWord if (string.IsNullOrEmpty(prevWord)) { return(0); } // If the word is already in the table increment the count if (_Bigram.ContainsKey(prevWord)) { if (_Bigram[prevWord].ContainsKey(word)) { _Bigram[prevWord][word].Frequency++; if (!_Bigram[prevWord][word].Locations.Contains(sentenceIndex)) { _Bigram[prevWord][word].Locations.Add(sentenceIndex); } return(_Bigram[prevWord][word].Frequency); } else { _Bigram[prevWord][word] = new FrequencyLocation(); _Bigram[prevWord][word].Frequency = 1; _Bigram[prevWord][word].Locations.Add(sentenceIndex); return(1); } } // otherwise, add it with count = 1 else { _Bigram[prevWord] = new Dictionary <string, FrequencyLocation>(); _Bigram[prevWord][word] = new FrequencyLocation(); _Bigram[prevWord][word].Frequency = 1; _Bigram[prevWord][word].Locations.Add(sentenceIndex); return(1); } }