Example #1
0
 public Node(String info, Node lchild, Node rchild)
 {
     this.info = info;
     this.leafFlag = false;
     this.lChild = lchild;
     this.rChild = rchild;
 }
Example #2
0
 public Node(String info)
 {
     this.info = info;
     this.leafFlag = true;
     this.lChild = null;
     this.rChild = null;
 }
Example #3
0
 public Node()
 {
     this.info = null;
     this.leafFlag = true;
     this.lChild = null;
     this.rChild = null;
 }
Example #4
0
        public void add(Node node)
        {
            if (dict.ContainsKey(node.info))
            {
                dict[node.info] += 1;
                bool nodeExist = false;
                List<Rule> list = ruleDict[node.info];
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Equals(node))
                    {
                        nodeExist = true;
                        list[i].count++;
                        break;
                    }
                }

                if (!nodeExist)
                {
                    if (node.rChild == null)
                    {
                        ruleDict[node.info].Add(new Rule(node.info, node.lChild.info, null));
                    }
                    else
                    {
                        ruleDict[node.info].Add(new Rule(node.info, node.lChild.info, node.rChild.info));
                    }
                }

            }
            else
            {
                dict[node.info] = 1;
                List<Rule> list = new List<Rule>();
                if (node.rChild == null)
                {
                    list.Add(new Rule(node.info, node.lChild.info, null));
                }
                else
                {
                    list.Add(new Rule(node.info, node.lChild.info, node.rChild.info));
                }
                ruleDict[node.info] = list;
            }
        }
Example #5
0
        public bool Equals(Node node)
        {
            if (node.rChild == null)
            {
                if (this.info.Equals(node.info) && this.lChild.Equals(node.lChild.info))
                {
                    return true;
                }
            }
            else
            {
                if (this.info.Equals(node.info) && this.lChild.Equals(node.lChild.info) && this.rChild.Equals(node.rChild.info))
                {
                    return true;
                }
            }

            return false;
        }
Example #6
0
            public static void getRuleList(Node head)
            {
                Stack<Node> stack = new Stack<Node>();
                stack.Push(head);
                while (stack.Count > 0)
                {
                    Node peek = stack.Pop();

                    if (peek.leafFlag)
                    {
                        continue;
                    }

                    ruleprob.add(peek);
                    if (peek.rChild != null)
                    {
                        stack.Push(peek.rChild);
                    }
                    if (peek.lChild != null)
                    {
                        stack.Push(peek.lChild);
                    }
                }
            }
Example #7
0
        public Node readInput(String sInput)
        {
            Stack<String> stack = new Stack<String>();
            Stack<Node> nodeStack = new Stack<Node>();
            Queue<Node> queue = new Queue<Node>();

            sInput = Regex.Replace(sInput, @"[(]", " ( ");
            sInput = Regex.Replace(sInput, @"[)]", " ) ");
            sInput = sInput.Trim();
            String[] st = Regex.Split(sInput, @"\s+");

            for (int i = 0; i < st.Length; i++)
            {
                if (st[i].Equals("("))
                {
                    stack.Push(st[i]);
                }
                else if (st[i].Equals(")"))
                {
                    List<String> list = new List<String>();
                    String sTop = stack.Pop();
                    while (!sTop.Equals("("))
                    {
                        list.Add(sTop);
                        sTop = stack.Pop();
                    }

                    if (list.Count == 2)
                    {
                        //lexical rule
                        Node node1 = new Node(list[0]);
                        Node node2 = new Node(list[1], node1);
                        nodeStack.Push(node2);
                    }
                    else
                    {
                        //phrasal rule
                        Node nt1 = nodeStack.Pop();
                        Node nt2 = nodeStack.Pop();
                        Node node1 = new Node(list[0], nt2, nt1);
                        nodeStack.Push(node1);
                    }
                }
                else
                {
                    stack.Push(st[i]);
                }
            }

            return nodeStack.Pop();
        }
Example #8
0
            public static double calcInnerProb(int lChildIndex, int rChildIndex, Node father)
            {
                double maxProb = 0;
                List<Rule> rules = chart[lChildIndex, rChildIndex];
                NodeProb nodeProb;
                Node lNode = new Node(), rNode = new Node();
                double pprob = 0;

                if (lChildIndex == rChildIndex)
                {
                    father.info = rules[0].info;
                    father.lChild = new Node(rules[0].lChild);

                    nodeProb = new NodeProb(rules[0].info, rules[0].lChild, null, lChildIndex, rChildIndex, rules[0].prob, rules[0].prob);
                    nodeProbList.Add(nodeProb);

                    return rules[0].prob;
                }

                for (int i = 0; i < rules.Count; i++ )
                {
                    int jIndex = 0;
                    lNode = new Node();
                    rNode = new Node();

                    lNode.leafFlag = false;
                    rNode.leafFlag = false;

                    for (int j = lChildIndex; j < rChildIndex; j++ )
                    {
                        bool ltree = false, rtree = false;

                        for (int k = 0; k < chart[lChildIndex, j].Count; k++ )
                        {
                            if (chart[lChildIndex, j][k].info.Equals(rules[i].lChild))
                            {
                                ltree = true;
                                break;
                            }
                        }

                        if (ltree)
                        {
                            for (int k = 0; k < chart[j + 1, rChildIndex].Count; k++)
                            {
                                if (chart[j + 1, rChildIndex][k].info.Equals(rules[i].rChild))
                                {
                                    rtree = true;
                                    break;
                                }
                            }
                        }

                        if (ltree && rtree)
                        {
                            jIndex = j;
                            double prob = rules[i].prob * calcInnerProb(lChildIndex, jIndex, lNode) * calcInnerProb(jIndex + 1, rChildIndex, rNode);
                            if (prob > maxProb)
                            {
                                maxProb = prob;
                                father.info = rules[i].info;

                                lNode.info = chart[lChildIndex, rChildIndex][i].lChild;
                                rNode.info = chart[lChildIndex, rChildIndex][i].rChild;

                                father.lChild = lNode;
                                father.rChild = rNode;
                                father.leafFlag = false;
                                pprob = rules[i].prob;
                            }
                        }
                    }

                }

                nodeProb = new NodeProb(father.info, father.lChild.info, father.rChild.info, lChildIndex, rChildIndex, maxProb, pprob);
                nodeProbList.Add(nodeProb);

                return maxProb;
            }
Example #9
0
            public static String maxProbTree(Node node)
            {
                String str;

                String s = "";
                if (node.rChild != null)
                {
                    s = node.info + maxProbTree(node.lChild) + maxProbTree(node.rChild);
                }
                else
                {
                    s = node.info + " " + node.lChild.info;
                }

                str = "(" + s + ")";

                return str;
            }