Exemplo n.º 1
0
 public TrieTreeNode AddWord(string word, double frequency, POSType pos)
 {
     if (word.Length == 1)
     {
         return(AddWord(word[0], frequency, pos));
     }
     else
     {
         char[]       chars = word.ToCharArray();
         TrieTreeNode node  = _root;
         for (int i = 0; i < chars.Length; i++)
         {
             TrieTreeNode newnode = node.AddChild(chars[i]);
             node = newnode;
         }
         if (frequency == double.NaN)
         {
             node.IncreaseFrequency();
         }
         else
         {
             node.Frequency += frequency;
         }
         node.WordEnded = true;
         if (pos != POSType.UNKNOWN && pos != POSType.NEWLINE)
         {
             node.AddPOS(pos);
         }
         return(node);
     }
 }
Exemplo n.º 2
0
        public TrieTreeNode AddChild(char ch)
        {
            TrieTreeNode matchedNode = null;

            if (_children != null)
            {
                matchedNode = _children.FirstOrDefault(n => n.Character == ch);
            }
            if (matchedNode != null)    //found the char in the list
            {
                //matchedNode.IncreaseFrequency();
                return(matchedNode);
            }
            else
            {   //not found
                TrieTreeNode node = new TrieTreeNode(ch, this.Depth + 1);
                node.Parent = this;
                //node.IncreaseFrequency();
                if (_children == null)
                {
                    _children = new HashSet <TrieTreeNode>();
                }
                _children.Add(node);
                return(node);
            }
        }
Exemplo n.º 3
0
        public TrieTreeNode GetNode(char ch, int pos)
        {
            TrieTreeNode matchedNode = null;

            if (pos == 0)
            {
                matchedNode = _root.Children.FirstOrDefault(n => n.Character == ch);
            }
            else
            {
                matchedNode = _root.Children.FirstOrDefault(n => n.Character == ch && (n.POSValue & pos) > 0);
            }
            if (matchedNode == null)
            {
                return(null);
            }
            return(matchedNode);
        }
Exemplo n.º 4
0
 public TrieTreeNode GetNode(string word, int pos)
 {
     if (word.Length == 1)
     {
         return(GetNode(word[0], pos));
     }
     else
     {
         char[]       chars = word.ToCharArray();
         TrieTreeNode node  = _root;
         for (int i = 0; i < chars.Length; i++)
         {
             if (node.Children == null)
             {
                 return(null);
             }
             TrieTreeNode matchednode = node.Children.FirstOrDefault(n => n.Character == chars[i]);
             if (matchednode == null)
             {
                 return(null);
             }
             node = matchednode;
         }
         if (node.WordEnded == true)
         {
             if (pos == 0)
             {
                 return(node);
             }
             else if ((node.POSValue & pos) > 0)
             {
                 return(node);
             }
             else
             {
                 return(null);
             }
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 5
0
        internal TrieTreeNode AddWord(char ch, double frequency, bool isWordEnded, POSType pos)
        {
            TrieTreeNode newnode = _root.AddChild(ch);

            if (frequency == double.NaN)
            {
                newnode.IncreaseFrequency();
            }
            else
            {
                newnode.Frequency += frequency;
            }
            newnode.WordEnded = true;
            if (pos != POSType.UNKNOWN && pos != POSType.NEWLINE)
            {
                newnode.AddPOS(pos);
            }
            return(newnode);
        }
Exemplo n.º 6
0
        void NGramRecursive(Dictionary <string, double> dict, TrieTreeNode tn, string r, int depth)
        {
            string nr = r + tn.Character;

            if (nr != string.Empty && tn.Depth == depth)
            {
                dict.Add(nr, tn.Frequency);
            }
            if (tn.Children == null || tn.Depth == depth)
            {
                return;
            }
            else
            {
                foreach (TrieTreeNode tnc in tn.Children)
                {
                    NGramRecursive(dict, tnc, nr, depth);
                }
            }
        }
Exemplo n.º 7
0
 private TrieTree()
 {
     _root = new TrieTreeNode(char.MaxValue, 0);
 }
Exemplo n.º 8
0
 private TrieTree()
 {
     _root = new TrieTreeNode(char.MaxValue,0);
 }
Exemplo n.º 9
0
 void NGramRecursive(Dictionary<string,double> dict,TrieTreeNode tn,string r,int depth)
 {
     string nr = r + tn.Character;
     if (nr != string.Empty&&tn.Depth==depth)
     {
         dict.Add(nr, tn.Frequency);
     }
     if (tn.Children== null||tn.Depth==depth)
     {
         return;
     }
     else
     {
         foreach(TrieTreeNode tnc in tn.Children)
         {
             NGramRecursive(dict, tnc,nr,depth);
         }
     }
 }
Exemplo n.º 10
0
 public TrieTreeNode AddChild(char ch)
 {
     TrieTreeNode matchedNode=null;
     if (_children != null)
     {
         matchedNode = _children.FirstOrDefault(n => n.Character == ch);
     }
     if (matchedNode != null)    //found the char in the list
     {
         //matchedNode.IncreaseFrequency();
         return matchedNode;
     }
     else
     {   //not found
         TrieTreeNode node = new TrieTreeNode(ch, this.Depth + 1);
         node.Parent = this;
         //node.IncreaseFrequency();
         if (_children == null)
             _children = new HashSet<TrieTreeNode>();
         _children.Add(node);
         return node;
     }
 }