/** * for testing only. doubles combined by addition. */ public List sampleUniformPathFromGraph() { List list = new ArrayList(); Object node = this.getStartNode(); Set endNodes = this.getEndNodes(); while (!endNodes.contains(node)) { List<Arc> arcs = new ArrayList<>(this.getArcsBySource(node)); TransducerGraph.Arc arc = arcs.get(r.nextInt(arcs.size())); list.add(arc.getInput()); node = arc.getTargetNode(); } return list; }
public void depthFirstSearch(boolean forward, StringBuilder b) { if (forward) { depthFirstSearchHelper(getStartNode(), new HashSet(), 0, true, b); } else { for (Object o : getEndNodes()) { depthFirstSearchHelper(o, new HashSet(), 0, false, b); } } }
public TransducerGraph(TransducerGraph other, ArcProcessor arcProcessor) { this(other.getArcs(), other.getStartNode(), other.getEndNodes(), arcProcessor, null); }
public TransducerGraph(TransducerGraph other, NodeProcessor nodeProcessor) { this(other.getArcs(), other.getStartNode(), other.getEndNodes(), null, nodeProcessor); }
/** * For testing only. Doubles combined by addition. */ public double getOutputOfPathInGraph(List path) { double score = 0.0; Object node = getStartNode(); for (Object input : path) { Arc arc = getArcBySourceAndInput(node, input); // next input in path if (arc == null) { System.out.println(" NOT ACCEPTED :" + path); return Double.NEGATIVE_INFINITY; } score += ((Double) arc.getOutput()).doubleValue(); node = arc.getTargetNode(); } return score; }
public static void addOnePathToGraph(List path, double count, int markovOrder, TransducerGraph graph) { Object source = graph.getStartNode(); for (int j = 0; j < path.size(); j++) { Object input = path.get(j); Arc a = graph.getArcBySourceAndInput(source, input); if (a != null) { // increment the arc weight a.output = new Double(((Double) a.output).doubleValue() + count); } else { Object target; if (input.equals(TransducerGraph.EPSILON_INPUT)) { target = "END"; // to ensure they all share the same end node } else if (markovOrder == 0) { // we all transition back to the same state target = source; } else if (markovOrder > 0) { // the state is described by the partial history target = path.subList((j < markovOrder ? 0 : j - markovOrder + 1), j + 1); } else { // the state is described by the full history target = path.subList(0, j + 1); } Double output = new Double(count); a = new Arc(source, target, input, output); graph.addArc(a); } source = a.getTargetNode(); } graph.setEndNode(source); }
/** * For testing only. Doubles combined by multiplication. */ private boolean canAddPath(List path) { Object node = this.getStartNode(); for (int j = 0; j < path.size() - 1; j++) { Object input = path.get(j); Arc arc = this.getArcBySourceAndInput(node, input); // next input in path if (arc == null) { return true; } node = arc.getTargetNode(); } Object input = path.get(path.size() - 1); // last element Arc arc = this.getArcBySourceAndInput(node, input); // next input in path if (arc == null) { return true; } else { return getEndNodes().contains(arc.getTargetNode()); } }
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", "6", "c", ""); fa.addArc("5", "6", "c", ""); fa.setEndNode("6"); System.out.println(fa); ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer(); System.out.println(minimizer.minimizeFA(fa)); System.out.println("Starting..."); Timing.startTime(); TransducerGraph randomFA = TransducerGraph.createRandomGraph(100, 10, 1.0, 10, new ArrayList()); TransducerGraph minimizedRandomFA = minimizer.minimizeFA(randomFA); System.out.println(randomFA); System.out.println(minimizedRandomFA); Timing.tick("done. ( " + randomFA.getArcs().size() + " arcs to " + minimizedRandomFA.getArcs().size() + " arcs)"); }
double startLambda = lambda.getCount(result.getStartNode()); if (startLambda != 0.0) { Set<TransducerGraph.Arc> startArcs = result.getArcsBySource(result.getStartNode()); for (TransducerGraph.Arc arc : startArcs) { double oldOutput = ((Double) arc.getOutput()).doubleValue();
protected boolean addOneUnaryRule(UnaryRule rule, Map<String, TransducerGraph> graphs) { String parentString = stateIndex.get(rule.parent); String childString = stateIndex.get(rule.child); if (isSyntheticState(parentString)) { String topcat = getTopCategoryOfSyntheticState(parentString); TransducerGraph graph = getGraphFromMap(graphs, topcat); Double output = new Double(smartNegate(rule.score())); graph.addArc(graph.getStartNode(), parentString, childString, output); return true; } else if (isSyntheticState(childString)) { // need to add Arc from synthetic state to endState TransducerGraph graph = getGraphFromMap(graphs, parentString); Double output = new Double(smartNegate(rule.score())); graph.addArc(childString, parentString, END, output); // parentString should the the same as endState graph.setEndNode(parentString); return true; } else { return false; } }
Object startNode = graph.getStartNode(); for (Arc arc : graph.getArcs()) {
protected TransducerGraph buildMinimizedFA() { TransducerGraph minimizedFA = new TransducerGraph(); TransducerGraph unminimizedFA = getUnminimizedFA(); for (TransducerGraph.Arc arc : unminimizedFA.getArcs()) { Object source = projectNode(arc.getSourceNode()); Object target = projectNode(arc.getTargetNode()); try { if (minimizedFA.canAddArc(source, target, arc.getInput(), arc.getOutput())) { minimizedFA.addArc(source, target, arc.getInput(), arc.getOutput()); } } catch (Exception e) { //throw new IllegalArgumentException(); } } minimizedFA.setStartNode(projectNode(unminimizedFA.getStartNode())); for (Object o : unminimizedFA.getEndNodes()) { minimizedFA.setEndNode(projectNode(o)); } return minimizedFA; }
protected TransducerGraph buildMinimizedFA() { TransducerGraph minimizedFA = new TransducerGraph(); TransducerGraph unminimizedFA = getUnminimizedFA(); for (TransducerGraph.Arc arc : unminimizedFA.getArcs()) { Set<Arc> source = projectNode(arc.getSourceNode()); Set<Arc> target = projectNode(arc.getTargetNode()); try { if (minimizedFA.canAddArc(source, target, arc.getInput(), arc.getOutput())) { minimizedFA.addArc(source, target, arc.getInput(), arc.getOutput()); } } catch (Exception e) { //throw new IllegalArgumentException(); } } minimizedFA.setStartNode(projectNode(unminimizedFA.getStartNode())); for (Object o : unminimizedFA.getEndNodes()) { minimizedFA.setEndNode(projectNode(o)); } return minimizedFA; }
public void depthFirstSearch(boolean forward, StringBuffer b) { if (forward) { depthFirstSearchHelper(getStartNode(), new HashSet(), 0, true, b); } else { for (Iterator endIter = getEndNodes().iterator(); endIter.hasNext();) { Object o = endIter.next(); depthFirstSearchHelper(o, new HashSet(), 0, false, b); } } }
public void depthFirstSearch(boolean forward, StringBuilder b) { if (forward) { depthFirstSearchHelper(getStartNode(), new HashSet(), 0, true, b); } else { for (Object o : getEndNodes()) { depthFirstSearchHelper(o, new HashSet(), 0, false, b); } } }
public void depthFirstSearch(boolean forward, StringBuilder b) { if (forward) { depthFirstSearchHelper(getStartNode(), new HashSet(), 0, true, b); } else { for (Object o : getEndNodes()) { depthFirstSearchHelper(o, new HashSet(), 0, false, b); } } }
public void depthFirstSearch(boolean forward, StringBuilder b) { if (forward) { depthFirstSearchHelper(getStartNode(), new HashSet(), 0, true, b); } else { for (Object o : getEndNodes()) { depthFirstSearchHelper(o, new HashSet(), 0, false, b); } } }
public TransducerGraph(TransducerGraph other, ArcProcessor arcProcessor) { this(other.getArcs(), other.getStartNode(), other.getEndNodes(), arcProcessor, null); }
public TransducerGraph(TransducerGraph other, NodeProcessor nodeProcessor) { this(other.getArcs(), other.getStartNode(), other.getEndNodes(), null, nodeProcessor); }
public TransducerGraph(TransducerGraph other, NodeProcessor nodeProcessor) { this(other.getArcs(), other.getStartNode(), other.getEndNodes(), null, nodeProcessor); }