Exemplo n.º 1
0
        ///<summary>
        /// Complete the rest of incomplete word
        ///</summary>
        /// <param name="subWord">Incomplete word</param>
        /// <returns>Completed words start with incomplete word</returns>
        public string[] Complete(string subWord)
        {
            NodeWithFreqandPOS node = IndexOf(subWord);

            if (node == null)
            {
                return(new string[0]);
            }

            string[] restOfWord = TraverseFrom(node);

            List <string> completeWords = new List <string>();
            //StringBuilder sb = new StringBuilder(subWord.Substring(0, subWord.Length - 1));
            //int start = subWord.Length - 1;
            string origstr = subWord.Substring(0, subWord.Length - 1);

            foreach (string str in restOfWord)
            {
                //sb.Append(str);
                //completeWords.Add(sb.ToString());
                completeWords.Add(origstr + str);
                //sb.Remove(start, str.Length);
            }

            return(completeWords.ToArray());
        }
Exemplo n.º 2
0
 ///<summary>
 /// Remove a child node
 ///</summary>
 ///<param name="node">Child node</param>
 public void RemoveLink(NodeWithFreqandPOS node)
 {
     if (this.m_links.ContainsKey(node.m_value))
     {
         this.m_links.Remove(node.m_value);
     }
 }
Exemplo n.º 3
0
        private void RemoveFromMemory(string word)
        {
            NodeWithFreqandPOS leaf = IndexOf(word);

            if (leaf != null)
            {
                leaf.LogicalRemove();
            }
        }
Exemplo n.º 4
0
        private static void Clear(NodeWithFreqandPOS node)
        {
            foreach (NodeWithFreqandPOS curNode in node.GetLinks())
            {
                Clear(curNode);
            }

            node.Clear();
        }
Exemplo n.º 5
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)
        {
            NodeWithFreqandPOS leaf = IndexOf(word);

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

            return(true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Word frequency
        /// </summary>
        /// <param name="word">word</param>
        /// <returns>usage frequency</returns>
        public int WordFrequency(string word)
        {
            NodeWithFreqandPOS leaf = IndexOf(word);

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

            return(leaf.WordFrequency);
        }
Exemplo n.º 7
0
        protected string[] TraverseFrom(NodeWithFreqandPOS node)
        {
            string[]      words   = DoDFSTraverse(node);
            List <string> strList = new List <string>();

            foreach (string str in words)
            {
                strList.Add(ReverseString(str));
            }
            return(strList.ToArray());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Word pos
        /// </summary>
        /// <param name="word">word</param>
        /// <returns>POS tag</returns>
        public PersianPOSTag WordPOS(string word)
        {
            NodeWithFreqandPOS leaf = IndexOf(word);

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

            return(leaf.POSTag.ToEnum <PersianPOSTag>());
        }
Exemplo n.º 9
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;
            NodeWithFreqandPOS leaf = IndexOf(word);

            if (leaf == null || !leaf.IsEndOfWord)
            {
                return(false);
            }
            freq = leaf.WordFrequency;
            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Check if a word exists
        /// </summary>
        /// <param name="word">Word</param>
        /// <param name="posTag">Word's POS tag</param>
        /// <returns>If the dictionary contains the word, returns true, else returns false.</returns>
        public bool Contain(string word, out PersianPOSTag posTag)
        {
            posTag = PersianPOSTag.UserPOS;

            NodeWithFreqandPOS leaf = IndexOf(word);

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

            posTag = leaf.POSTag.ToEnum <PersianPOSTag>();

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Teaverse Tree on Breath First Type
        /// </summary>
        /// <returns></returns>
        private string[] DoBFSTraverse(NodeWithFreqandPOS node)
        {
            if (node.HaveLinks())
            {
                List <string> suggestions = new List <string>();
                StringBuilder sb          = new StringBuilder();
                foreach (NodeWithFreqandPOS 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() });
        }
Exemplo n.º 12
0
        /// <summary>
        /// Teaverse Tree on Depth First Type
        /// </summary>
        /// <returns></returns>
        private string[] DoDFSTraverse(NodeWithFreqandPOS node)
        {
            if (node.HaveLinks())
            {
                List <string> retDFS = new List <string>();
                foreach (NodeWithFreqandPOS 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]);
            }
        }
Exemplo n.º 13
0
        protected NodeWithFreqandPOS IndexOf(string subWord)
        {
            NodeWithFreqandPOS 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);
        }
Exemplo n.º 14
0
        ///<summary>
        /// Add a child node
        ///</summary>
        ///<param name="node">Child node</param>
        ///<returns>Node pointer to added child node</returns>
        public NodeWithFreqandPOS AddLink(NodeWithFreqandPOS 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 (this.m_links[node.m_value].m_pos != node.m_pos)
                {
                    this.m_links[node.m_value].m_pos = mergePOS(node.m_pos, this.m_links[node.m_value].m_pos);
                }
            }

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

            return(this.m_links[node.m_value]);
        }
Exemplo n.º 15
0
        private void AddWordToMemory(string word, int freq, string pos)
        {
            NodeWithFreqandPOS 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, pos));

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

                    ++this.m_wordsCount;
                }
                else
                {
                    curNode = curNode.AddLink(GenerateNode(c, 0, false, pos));
                }
            }
        }
Exemplo n.º 16
0
        ///<summary>
        /// Complete the rest of incomplete word
        ///</summary>
        /// <param name="subWord">Incomplete word</param>
        ///<param name="count">Number of returned suggestions</param>
        /// <returns>Completed words start with incomplete word</returns>
        public string[] Complete(string subWord, int count)
        {
            NodeWithFreqandPOS node = IndexOf(subWord);

            if (node == null)
            {
                return(new string[0]);
            }

            string[] restOfWord = TraverseFrom(node);

            string origstr = subWord.Substring(0, subWord.Length - 1);

            List <string> completeWords = new List <string>();

            for (int i = 0; i < Math.Min(count, restOfWord.Length); ++i)
            {
                completeWords.Add(origstr + restOfWord[i]);
            }

            return(completeWords.ToArray());
        }
Exemplo n.º 17
0
 ///<summary>
 /// Class Constructor
 ///</summary>
 public WordFreqPOSContainerTree()
 {
     m_root = new NodeWithFreqandPOS('*', false, 0, PersianPOSTag.UserPOS.ToString());
 }