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); }
public static void Main(string[] args) { /* * TransducerGraph fa = new TransducerGraph(); * fa.addArc(fa.getStartNode(),"1","a",""); * fa.addArc(fa.getStartNode(),"2","b",""); * fa.addArc(fa.getStartNode(),"3","c",""); * fa.addArc("1","4","a",""); * fa.addArc("2","4","a",""); * fa.addArc("3","5","c",""); * fa.addArc("4",fa.getEndNode(),"c",""); * fa.addArc("5",fa.getEndNode(),"c",""); * System.out.println(fa); * ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer(); * System.out.println(minimizer.minimizeFA(fa)); */ System.Console.Out.WriteLine("Starting minimizer test..."); IList pathList = new ArrayList(); TransducerGraph randomFA = TransducerGraph.CreateRandomGraph(5000, 5, 1.0, 5, pathList); IList outputs = randomFA.GetPathOutputs(pathList); TransducerGraph.IGraphProcessor quasiDeterminizer = new QuasiDeterminizer(); IAutomatonMinimizer minimizer = new FastExactAutomatonMinimizer(); TransducerGraph.INodeProcessor ntsp = new TransducerGraph.SetToStringNodeProcessor(new PennTreebankLanguagePack()); TransducerGraph.IArcProcessor isp = new TransducerGraph.InputSplittingProcessor(); TransducerGraph.IArcProcessor ocp = new TransducerGraph.OutputCombiningProcessor(); TransducerGraph detGraph = quasiDeterminizer.ProcessGraph(randomFA); TransducerGraph combGraph = new TransducerGraph(detGraph, ocp); // combine outputs into inputs TransducerGraph result = minimizer.MinimizeFA(combGraph); // minimize the thing System.Console.Out.WriteLine("Minimized from " + randomFA.GetNodes().Count + " to " + result.GetNodes().Count); result = new TransducerGraph(result, ntsp); // pull out strings from sets returned by minimizer result = new TransducerGraph(result, isp); // split outputs from inputs IList minOutputs = result.GetPathOutputs(pathList); System.Console.Out.WriteLine("Equal? " + outputs.Equals(minOutputs)); }
/// <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); }