public virtual TransducerGraph ProcessGraph(TransducerGraph g)
            {
                g = new TransducerGraph(g);
                ISet nodes = g.GetNodes();

                foreach (object node in nodes)
                {
                    ICollection <TransducerGraph.Arc> myArcs = null;
                    if (forward)
                    {
                        myArcs = g.GetArcsBySource(node);
                    }
                    else
                    {
                        myArcs = g.GetArcsByTarget(node);
                    }
                    // compute a total
                    double total = 0.0;
                    foreach (TransducerGraph.Arc a in myArcs)
                    {
                        total += ((double)a.GetOutput());
                    }
                    // divide each by total
                    foreach (TransducerGraph.Arc a_1 in myArcs)
                    {
                        a_1.SetOutput(Math.Log(((double)a_1.GetOutput()) / total));
                    }
                }
                return(g);
            }
Ejemplo n.º 2
0
        /// <summary>Takes time linear in number of arcs.</summary>
        public virtual TransducerGraph PushLambdas(TransducerGraph graph, ClassicCounter lambda)
        {
            TransducerGraph result = null;

            result = graph.Clone();
            // arcs have been copied too so we don't mess up graph
            ICollection <TransducerGraph.Arc> arcs = result.GetArcs();

            foreach (TransducerGraph.Arc arc in arcs)
            {
                double sourceLambda = lambda.GetCount(arc.GetSourceNode());
                double targetLambda = lambda.GetCount(arc.GetTargetNode());
                double oldOutput    = ((double)arc.GetOutput());
                double newOutput    = oldOutput + targetLambda - sourceLambda;
                arc.SetOutput(newOutput);
            }
            // do initialOutput
            double startLambda = lambda.GetCount(result.GetStartNode());

            if (startLambda != 0.0)
            {
                // add it back to the outbound arcs from start (instead of adding it to the initialOutput)
                ICollection <TransducerGraph.Arc> startArcs = result.GetArcsBySource(result.GetStartNode());
                foreach (TransducerGraph.Arc arc_1 in startArcs)
                {
                    double oldOutput = ((double)arc_1.GetOutput());
                    double newOutput = oldOutput + startLambda;
                    arc_1.SetOutput(newOutput);
                }
            }
            // do finalOutput
            foreach (object o in result.GetEndNodes())
            {
                double endLambda = lambda.GetCount(o);
                if (endLambda != 0.0)
                {
                    // subtract it from the inbound arcs to end (instead of subtracting it from the finalOutput)
                    ICollection <TransducerGraph.Arc> endArcs = result.GetArcsByTarget(o);
                    foreach (TransducerGraph.Arc arc_1 in endArcs)
                    {
                        double oldOutput = ((double)arc_1.GetOutput());
                        double newOutput = oldOutput - endLambda;
                        arc_1.SetOutput(newOutput);
                    }
                }
            }
            return(result);
        }