public virtual TransducerGraph.Arc ProcessArc(TransducerGraph.Arc a)
 {
     a = new TransducerGraph.Arc(a);
     a.SetInput(Generics.NewPair(a.GetInput(), a.GetOutput()));
     a.SetOutput(null);
     return(a);
 }
        /// <summary>For testing only.</summary>
        /// <remarks>For testing only.  Doubles combined by addition.</remarks>
        public virtual double GetOutputOfPathInGraph(IList path)
        {
            double score = 0.0;
            object node  = GetStartNode();

            foreach (object input in path)
            {
                TransducerGraph.Arc arc = GetArcBySourceAndInput(node, input);
                // next input in path
                if (arc == null)
                {
                    System.Console.Out.WriteLine(" NOT ACCEPTED :" + path);
                    return(double.NegativeInfinity);
                }
                score += ((double)arc.GetOutput());
                node   = arc.GetTargetNode();
            }
            return(score);
        }
Beispiel #3
0
        /// <summary>Takes time linear in number of arcs.</summary>
        public static ClassicCounter ComputeLambda(TransducerGraph graph)
        {
            ArrayList      queue  = new ArrayList();
            ClassicCounter lambda = new ClassicCounter();
            ClassicCounter length = new ClassicCounter();
            IDictionary    first  = new Hashtable();
            ISet           nodes  = graph.GetNodes();

            foreach (object node in nodes)
            {
                lambda.SetCount(node, 0);
                length.SetCount(node, double.PositiveInfinity);
            }
            ISet endNodes = graph.GetEndNodes();

            foreach (object o in endNodes)
            {
                lambda.SetCount(o, 0);
                length.SetCount(o, 0);
                queue.AddLast(o);
            }
            // Breadth first search
            // get the first node from the queue
            object node_1 = null;

            try
            {
                node_1 = queue.RemoveFirst();
            }
            catch (NoSuchElementException)
            {
            }
            while (node_1 != null)
            {
                double oldLen = length.GetCount(node_1);
                ISet   arcs   = graph.GetArcsByTarget(node_1);
                if (arcs != null)
                {
                    foreach (object arc1 in arcs)
                    {
                        TransducerGraph.Arc arc = (TransducerGraph.Arc)arc1;
                        object      newNode     = arc.GetSourceNode();
                        IComparable a           = (IComparable)arc.GetInput();
                        double      k           = ((double)arc.GetOutput());
                        double      newLen      = length.GetCount(newNode);
                        if (newLen == double.PositiveInfinity)
                        {
                            // we are discovering this
                            queue.AddLast(newNode);
                        }
                        IComparable f = (IComparable)first[newNode];
                        if (newLen == double.PositiveInfinity || (newLen == oldLen + 1 && a.CompareTo(f) < 0))
                        {
                            // f can't be null, since we have a newLen
                            // we do this to this to newNode when we have new info, possibly many times
                            first[newNode] = a;
                            // ejecting old one if necessary
                            length.SetCount(newNode, oldLen + 1);
                            // this may already be the case
                            lambda.SetCount(newNode, k + lambda.GetCount(node_1));
                        }
                    }
                }
                // get a new node from the queue
                node_1 = null;
                try
                {
                    node_1 = queue.RemoveFirst();
                }
                catch (NoSuchElementException)
                {
                }
            }
            return(lambda);
        }
 protected internal Arc(TransducerGraph.Arc <NODE, IN, OUT> a)
     : this(a.GetSourceNode(), a.GetTargetNode(), a.GetInput(), a.GetOutput())
 {
 }