示例#1
0
        private HyperEdge GetNext()
        {
            HyperEdge e;
            if (next >= pool.Count)
            {
                e = new HyperEdge();
                pool.Add(e);
            }
            else
            {
                e = pool[next];
            }

            next += 1;
            e.Clear();
            e.next = null;
            return e;
        }
示例#2
0
        public void Reset(bool isTerminal, int tag, int subtagCounts)
        {
            this.tag = tag;
            this.TYPE = isTerminal ? VTYPE.TERMINAL : VTYPE.NONTERMINAL;
            this.posteriorScore = isTerminal ? 0.0f : double.NegativeInfinity;
            this.posteriorViterbiAlpha = isTerminal ? 0.0f : double.NegativeInfinity;
            this.subtagCount = subtagCounts;

            if (isTerminal && subtagCount != 1)
            {
                throw new Exception("terminal vertex cannot have subtags!");
            }

            alpha.Length = subtagCounts;
            beta.Length = subtagCounts;

            Array.Clear (pruned, 0, pruned.Length);

            if (!isTerminal) {
                for (int i = 0; i < _alpha.Length; ++i) {
                    _alpha [i] = double.NegativeInfinity;
                }
            } else {
                Array.Clear (_alpha, 0, _alpha.Length);
            }

            for (int i = 0; i < _beta.Length; ++i) {
                _beta [i] = double.NegativeInfinity;
            }

            InDegree = 0;
            OutDegree = 0;

            first = null;
            last = null;
        }
示例#3
0
 //public void RemoveIncomingLink(HyperEdge edge)
 //{
 //    incomings.Remove(edge);
 //}
 public void RemoveOutgoingLink(HyperEdge edge)
 {
     //outgoings.Remove(edge);
     OutDegree -= 1;
 }
示例#4
0
 public void Clear()
 {
     first = null;
     last = null;
     InDegree = 0;
     OutDegree = 0;
     posteriorViterbiEdge = null;
     TYPE = VTYPE.DEAD;
     traces = null;
 }
示例#5
0
 public void AddOutgoingLink(HyperEdge edge)
 {
     //outgoings.Add(edge);
     OutDegree += 1;
 }
示例#6
0
 public void AddIncomingLink(HyperEdge edge)
 {
     //incomings.Add(edge);
     if (first == null) {
         first = edge;
         last = edge;
     } else {
         last.next = edge;
         last = edge;
     }
     InDegree += 1;
 }
示例#7
0
 public void Clear()
 {
     to = null;
     from0 = null;
     from1 = null;
     next = null;
     TYPE = ETYPE.NULL;
 }
示例#8
0
        public static void SumBackward(
            RiggedArray pbeta,
            double[][][] binaryScores,
            RiggedArray lalpha,
            RiggedArray ralpha,
            RiggedArray lbeta,
            RiggedArray rbeta,
            double[] lbuf)
        {
            //double[] lbuf = new double[ralpha.Length + 1];

            for (int l = 0; l < lalpha.Length; ++l) {
                if (binaryScores [l] == null || lalpha.pruned [l]) {
                    continue;
                }

                lbuf [0] = lbeta.v [l];
                int next = 1;

                for (int r = 0; r < ralpha.Length; ++r) {
                    if (binaryScores [l] [r] == null || ralpha.pruned [r]) {
                        continue;
                    }

                    //lbeta[l] += ralpha[r] * MathHelper.InnerProduct(binaryScores[l][r], pbeta);

                    //rbeta[r] += lalpha[l] * MathHelper.InnerProduct(binaryScores[l][r], pbeta);

                    double x = MathHelper.LogInnerProduct (binaryScores [l] [r], pbeta.v, pbeta.pruned, pbeta.Length);

                    //if (!sb.Get(l, r, out x))
                    //{
                    //    x = MathHelper.LogInnerProduct(binaryScores[l][r], pbeta.v, pbeta.pruned, pbeta.Length);
                    //    sb.Set(l, r, x);
                    //}

                    //lbeta.v[l] = MathHelper.LogAdd(lbeta.v[l], ralpha.v[r] + x);
                    lbuf [next] = ralpha.v [r] + x;
                    next += 1;
                    rbeta.v [r] = MathHelper.LogAdd (rbeta.v [r], lalpha.v [l] + x);
                }

                if (next > 1) {
                    lbeta.v [l] = MathHelper.LogAdd (lbuf, next);
                }
            }
        }
示例#9
0
        private void BuildHyperGraph(
            PhrasalNode node,
            HyperGraph g,
            out HyperVertex v)
        {
            v = null;
            if (node == null)
            {
                return;
            }

            if (node.Children.Count == 0)
            {
                int pt = tagset.GetPTID(node.Tag);
                int wid = vocab.GetId(node.Lex, node.Start == 0);

                HyperVertex wv = new HyperVertex(true, wid, 1);

                HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt));

                HyperEdge pe = new HyperEdge(pv, wv,
                    rules.GetTerminalRuleScores(pt, wid),
                    rules.GetTerminalPosteriorCounts(pt, wid));

                g.Es.Add(pe);
                g.Vs.Add(wv);
                g.Vs.Add(pv);
                v = pv;
                return;
            } else if (node.Children.Count == 1)
            {
                HyperVertex cv;
                BuildHyperGraph(node.Children [0], g, out cv);

                int pt = tagset.GetID(node.Tag);
                HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt));

                HyperEdge pe = new HyperEdge(
                    pv, cv, rules.GetRuleScores(pt, cv.tag),
                    rules.GetPosteriorCounts(pt, cv.tag));

                g.Es.Add(pe);
                g.Vs.Add(pv);
                v = pv;
                return;
            } else if (node.Children.Count == 2)
            {
                HyperVertex lv;
                HyperVertex rv;

                BuildHyperGraph(node.Children [0], g, out lv);
                BuildHyperGraph(node.Children [1], g, out rv);

                int pt = tagset.GetID(node.Tag);
                HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt));

                HyperEdge pe = new HyperEdge(pv, lv, rv,
                    rules.GetRuleScores(pv.tag, lv.tag, rv.tag),
                    rules.GetPosteriorCounts(pv.tag, lv.tag, rv.tag));

                g.Es.Add(pe);
                g.Vs.Add(pv);
                v = pv;
                return;
            } else
            {
                throw new Exception("tree node can only have at most 2 children");
            }
        }