def build_it(linesstring): builder = SetGraphBuilder() boxen = set() for arc in StringIO.StringIO(linesstring): parts = arc.split() if not parts or parts[0].startswith("#"): continue if len(parts) == 1: builder.add_node(*parts) elif len(parts) == 2: builder.add_arc(*parts) else: assert False, str(parts) return FrozenGraph(builder)
def labels_to_lattice(labels): """ From a sequence of labels, build a linear lattice with labels on the arcs. The result will have node labels which are guaranteed to be unique. >>> label_dict = {'A': 1, 'B': 4, 'C': 9} >>> labels_to_lattice(('A', 'B', 'C')) FrozenGraph(GraphTables(((0, 1, 2, 3), (0, 1, 2), (1, 2, 3), ('A', 'B', 'C')))) """ gb = SetGraphBuilder() counter = itertools.count() start = gb.add_node(counter.next()) for l in labels: end = gb.add_node(counter.next()) gb.add_arc(start, end, l) start = end return FrozenGraph(gb)
def build_it(linesstring): builder = SetGraphBuilder() boxen = set() for arc in StringIO.StringIO(linesstring): parts = arc.split() if not parts or parts[0].startswith('#'): continue if len(parts) == 1: builder.add_node(*parts) elif len(parts) == 2: builder.add_arc(*parts) else: assert False, str(parts) return FrozenGraph(builder)
def build_model_lattice(label_lattice, model_dict, epsilon_index): """ From a lattice with labels on the arcs and a dict mapping labels to model indices, build a lattice with (node-index, model index) pairs on the nodes, usable for constructing a TrainingGraph. The resulting lattice may have new epsilon nodes as the new start and end nodes; these will be given epsilon_index as their model indices. Note that this function requires that label_lattice have unique labels on nodes. XXX maybe do this node-labeling ourselves here? >>> label_dict = {'A': 1, 'B': 4, 'C': 9} >>> lat = labels_to_lattice(('A', 'B', 'C')) >>> lat FrozenGraph(GraphTables(((0, 1, 2, 3), (0, 1, 2), (1, 2, 3), ('A', 'B', 'C')))) >>> result = build_model_lattice(lat, label_dict, 15) >>> print result FrozenGraph(GraphTables((((0, 1), (1, 4), (2, 9)), (0, 1), (1, 2), (None, None)))) # >>> result.dot_display() """ if not label_lattice.is_lattice() or label_lattice.has_self_loop(): raise ValueError("label_lattice is not a lattice or has a self loop") counter = itertools.count() # we need our node labels to be pairs of ints in which the first int is # unique and the second is the index of the model from the callers # label_dict def model_node_labeler(pred_node_label, arc_label, succ_node_label): if not model_dict.has_key(arc_label): raise KeyError("Failed on lookup of label %s" % (arc_label)) model_index = model_dict[arc_label] return (counter.next(), model_index) def empty_arc_labeler(in_arc_label, node_label, out_arc_label): return None line_graph = label_lattice.get_line_graph(model_node_labeler, empty_arc_labeler) starts, ends = line_graph.get_terminals() num_starts = len(starts) num_ends = len(ends) # If we started with a lattice, the line graph must have some terminals assert num_starts >= 1 and num_ends >= 1 start_labels = (line_graph.get_label(node_id) for node_id in starts) end_labels = (line_graph.get_label(node_id) for node_id in ends) gb = SetGraphBuilder(line_graph) # Tie terminals together with epsilons if necessary if num_starts > 1: new_start_label = gb.add_node((counter.next(), epsilon_index)) for node_label in start_labels: gb.add_arc(new_start_label, node_label) if num_ends > 1: new_end_label = gb.add_node((counter.next(), epsilon_index)) for node_label in end_labels: gb.new_arc(node_label, new_end_label) return FrozenGraph(gb)