Example #1
0
        private void RemoveFromMemory(string word)
        {
            NodeWithFreq leaf = IndexOf(word);

            if (leaf != null)
            {
                leaf.LogicalRemove();
            }
        }
Example #2
0
        protected string[] TraverseFrom(NodeWithFreq node)
        {
            string[]      words   = DoDFSTraverse(node);
            List <string> strList = new List <string>();

            foreach (string str in words)
            {
                strList.Add(ReverseString(str));
            }
            return(strList.ToArray());
        }
Example #3
0
        /// <summary>
        /// Word frequency
        /// </summary>
        /// <param name="word">word</param>
        /// <returns>usage frequency</returns>
        public int WordFrequency(string word)
        {
            NodeWithFreq leaf = IndexOf(word);

            if (leaf == null || !leaf.IsEndOfWord)
            {
                return(0);
            }

            return(leaf.WordFrequency);
        }
Example #4
0
        /// <summary>
        /// Check if a word exists
        /// </summary>
        /// <param name="word">Word</param>
        /// <returns>If the dictionary contains the word, returns true, else returns false.</returns>
        public bool Contain(string word)
        {
            NodeWithFreq leaf = IndexOf(word);

            if (leaf == null || !leaf.IsEndOfWord)
            {
                return(false);
            }

            return(true);
        }
Example #5
0
        /// <summary>
        /// Check if a word exists
        /// </summary>
        /// <param name="word">Word</param>
        /// <param name="freq">Word's usage frequency</param>
        /// <returns>If the dictionary contains the word, returns true, else returns false.</returns>
        public bool Contain(string word, out int freq)
        {
            freq = 0;
            NodeWithFreq leaf = IndexOf(word);

            if (leaf == null || !leaf.IsEndOfWord)
            {
                return(false);
            }
            freq = leaf.WordFrequency;
            return(true);
        }
Example #6
0
        ///<summary>
        /// Add a child node
        ///</summary>
        ///<param name="node">Child node</param>
        ///<returns>Node pointer to added child node</returns>
        public NodeWithFreq AddLink(NodeWithFreq node)
        {
            if (!this.m_links.ContainsKey(node.m_value))
            {
                this.m_links.Add(node.m_value, node);
            }
            else if (node.IsEndOfWord)
            {
                this.m_links[node.m_value].m_isEndLetter = node.IsEndOfWord;
                this.m_links[node.m_value].m_freq        = this.m_links[node.m_value].m_freq > node.WordFrequency ? this.m_links[node.m_value].m_freq : node.WordFrequency;
            }

            //if (node.IsEndOfWord && !this.m_links[node.m_value].m_isEndLetter)

            return(this.m_links[node.m_value]);
        }
Example #7
0
        /// <summary>
        /// Teaverse Tree on Breath First Type
        /// </summary>
        /// <returns></returns>
        private string[] DoBFSTraverse(NodeWithFreq node)
        {
            if (node.HaveLinks())
            {
                List <string> suggestions = new List <string>();
                StringBuilder sb          = new StringBuilder();
                foreach (NodeWithFreq curNode in node.GetLinks())
                {
                    sb.Append(DoDFSTraverse(curNode));
                    suggestions.Add(sb.ToString());
                    sb.Remove(0, sb.Length);
                }

                return(suggestions.ToArray());
            }

            return(new string[] { node.Value.ToString() });
        }
Example #8
0
        /// <summary>
        /// Teaverse Tree on Depth First Type
        /// </summary>
        /// <returns></returns>
        private string[] DoDFSTraverse(NodeWithFreq node)
        {
            if (node.HaveLinks())
            {
                List <string> retDFS = new List <string>();
                foreach (NodeWithFreq curNode in node.GetLinks())
                {
                    retDFS.AddRange(DoDFSTraverse(curNode));
                }

                List <string> suggestions = new List <string>();
                if (node.Value != this.m_root.Value)
                {
                    if (node.IsEndOfWord)
                    {
                        suggestions.Add(new StringBuilder().Append(node.Value).ToString());
                    }

                    StringBuilder sb = new StringBuilder();
                    foreach (string str in retDFS)
                    {
                        sb.Append(str);
                        sb.Append(node.Value);
                        suggestions.Add(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }

                    return(suggestions.ToArray());
                }
                else
                {
                    return(retDFS.ToArray());
                }
            }

            if (node.IsEndOfWord)
            {
                return(new string[] { new StringBuilder().Append(node.Value).ToString() });
            }
            else
            {
                return(new string[0]);
            }
        }
Example #9
0
        protected NodeWithFreq IndexOf(string subWord)
        {
            NodeWithFreq curNode = this.m_root;

            foreach (char c in subWord)
            {
                if (curNode != null)
                {
                    curNode = curNode.GetNextNode(c);
                }
            }

            if (curNode == this.m_root)
            {
                curNode = null;
            }

            return(curNode);
        }
Example #10
0
        private void AddWordToMemory(string word, int freq)
        {
            NodeWithFreq curNode = this.m_root;
            int          i = 0, length = word.Length;

            foreach (char c in word)
            {
                if (++i == length)
                {
                    curNode = curNode.AddLink(GenerateNode(c, freq, true));

                    //add number of dictionary words
                    this.m_freqSummation += freq;
                    this.m_totalWordsNumber++;

                    ++this.m_wordsCount;
                }
                else
                {
                    curNode = curNode.AddLink(GenerateNode(c, 0, false));
                }
            }
        }
Example #11
0
 ///<summary>
 /// Class Constructor
 ///</summary>
 public WordFreqContainerTree()
 {
     m_root = new NodeWithFreq('*', false, 0);
 }