Exemple #1
0
def go(wordnames, do_display=False):
    """
    A first example of composing finite CFGs.
    """

    comlexcfg = make_recognizer(StringIO(comlextop))
    #for non_terminal in sorted(comlexcfg.non_terminals):
    #    print non_terminal
    #for terminal in sorted(comlexcfg.terminals):
    #    print terminal
    phonecfg = make_recognizer(StringIO(comlexphones))

    wordrecognizer = comlexcfg.recognizer(wordnames)
    links, (start_id,
            is_sentential) = explore_finite(comlexcfg.recognizer(wordnames))
    g1 = FrozenGraph(make_initialized_set_graph_builder(links))

    printgraph(g1, 'Pronunciation lattice')
    do_display and display(g1, '\\n'.join(wordnames))

    breadth_first = deque()
    global_start = start_id = unstarted = object()
    seen_symbols = set()
    links = set()
    links2 = set()
    send_arg = None
    count = -1
    while True:
        is_sentential, end_id, legal, exception = wordrecognizer(send_arg)
        if global_start is unstarted:
            global_start = end_id
        if exception is not None: raise exception
        if not legal: assert is_sentential

        #print 'legal:', ' '.join(legal)

        if start_id is not unstarted:
            links.add(
                ((start_id, was_sentential), (end_id, is_sentential), symbol))

            count += 1
            count = 0
            substart2 = start_id, substart
            links2.add(((start_id, was_sentential), (substart2, False), '(-'))
            for (sub_start_id, sub_was_sentential), (
                    sub_end_id, sub_is_sentential), subsymbol in sublinks:
                sub_start_id = start_id, sub_start_id
                sub_end_id = start_id, sub_end_id
                ##                 if sub_start_id == substart:
                ##                     links2.add(((start_id, was_sentential), (sub_start_id, False), '(-'))
                links2.add((
                    (sub_start_id, False),
                    #((end_id, is_sentential) if sub_is_sentential else (sub_end_id, False)),
                    (sub_end_id, False),
                    subsymbol))
                if sub_is_sentential:
                    links2.add(
                        ((sub_end_id, False), (end_id, is_sentential), '(-'))

        for symbol in sorted(legal):
            breadth_first.appendleft(
                ((end_id, is_sentential), symbol,
                 explore_finite(phonecfg.recognizer([symbol]))))
            sublinks, (sub_start_id, sub_is_sentential) = explore_finite(
                phonecfg.recognizer([symbol]))
            if symbol not in seen_symbols:
                seen_symbols.add(symbol)
                subgraph = FrozenGraph(
                    make_initialized_set_graph_builder(sublinks))
                printgraph(subgraph, 'Phoneme %s' % (symbol, ))
                do_display and display(subgraph, symbol)
        if not breadth_first:
            break

        (start_id, was_sentential), symbol, (sublinks, (
            substart, substart_sentential)) = breadth_first.pop()
        send_arg = start_id, symbol

    g = FrozenGraph(make_initialized_set_graph_builder(links))
    None and do_display and display(g)

    g3 = FrozenGraph(make_initialized_set_graph_builder(links2))
    printgraph(g3, 'HMM graph')
    do_display and display(g3)
Exemple #2
0
def go(wordnames, do_display=False):
    """
    A first example of composing finite CFGs.
    """

    comlexcfg = make_recognizer(StringIO(comlextop))
    # for non_terminal in sorted(comlexcfg.non_terminals):
    #    print non_terminal
    # for terminal in sorted(comlexcfg.terminals):
    #    print terminal
    phonecfg = make_recognizer(StringIO(comlexphones))

    wordrecognizer = comlexcfg.recognizer(wordnames)
    links, (start_id, is_sentential) = explore_finite(comlexcfg.recognizer(wordnames))
    g1 = FrozenGraph(make_initialized_set_graph_builder(links))

    printgraph(g1, "Pronunciation lattice")
    do_display and display(g1, "\\n".join(wordnames))

    breadth_first = deque()
    global_start = start_id = unstarted = object()
    seen_symbols = set()
    links = set()
    links2 = set()
    send_arg = None
    count = -1
    while True:
        is_sentential, end_id, legal, exception = wordrecognizer(send_arg)
        if global_start is unstarted:
            global_start = end_id
        if exception is not None:
            raise exception
        if not legal:
            assert is_sentential

        # print 'legal:', ' '.join(legal)

        if start_id is not unstarted:
            links.add(((start_id, was_sentential), (end_id, is_sentential), symbol))

            count += 1
            count = 0
            substart2 = start_id, substart
            links2.add(((start_id, was_sentential), (substart2, False), "(-"))
            for (sub_start_id, sub_was_sentential), (sub_end_id, sub_is_sentential), subsymbol in sublinks:
                sub_start_id = start_id, sub_start_id
                sub_end_id = start_id, sub_end_id
                ##                 if sub_start_id == substart:
                ##                     links2.add(((start_id, was_sentential), (sub_start_id, False), '(-'))
                links2.add(
                    (
                        (sub_start_id, False),
                        # ((end_id, is_sentential) if sub_is_sentential else (sub_end_id, False)),
                        (sub_end_id, False),
                        subsymbol,
                    )
                )
                if sub_is_sentential:
                    links2.add(((sub_end_id, False), (end_id, is_sentential), "(-"))

        for symbol in sorted(legal):
            breadth_first.appendleft(((end_id, is_sentential), symbol, explore_finite(phonecfg.recognizer([symbol]))))
            sublinks, (sub_start_id, sub_is_sentential) = explore_finite(phonecfg.recognizer([symbol]))
            if symbol not in seen_symbols:
                seen_symbols.add(symbol)
                subgraph = FrozenGraph(make_initialized_set_graph_builder(sublinks))
                printgraph(subgraph, "Phoneme %s" % (symbol,))
                do_display and display(subgraph, symbol)
        if not breadth_first:
            break

        (start_id, was_sentential), symbol, (sublinks, (substart, substart_sentential)) = breadth_first.pop()
        send_arg = start_id, symbol

    g = FrozenGraph(make_initialized_set_graph_builder(links))
    None and do_display and display(g)

    g3 = FrozenGraph(make_initialized_set_graph_builder(links2))
    printgraph(g3, "HMM graph")
    do_display and display(g3)
def go(do_display=False):
    """
    Generate a dependency graph, and display it if optional do_display is True.

    >>> go(do_display=False)
    digraph  { 
      node [shape=box];
      ranksep=0.4;
      {rank=same; "n05";}
      {rank=same; "n01"; "n02";}
      {rank=same; "n04"; "n06";}
      {rank=same; "n00"; "n03"; "n08"; "n10";}
      {rank=same; "n07"; "n09";}
      n00  [label="Acoustic Models", style=bold, shape=box];
      n01  [label="Graph  /  Lattice", style=bold, shape=octagon];
      n02  [label="Serialization", style=bold, shape=octagon];
      n03  [label="Audio", style=bold, shape=box];
      n04  [label="Dataflow", style=bold, shape=octagon];
      n05  [label="          Utilities  /  Containers          ", style=bold, shape=octagon];
      n06  [label="CFG", style=bold, shape=octagon];
      n07  [label="Decoding", style=bold, shape=box];
      n08  [label="Lexicon", style=bold, shape=box];
      n09  [label="HTK Files", style=bold, shape=octagon];
      n10  [label="Signal Processing", style=bold, shape=box];
      n00 -> n01;
      n00 -> n02;
      n03 -> n04;
      n03 -> n05;
      n06 -> n01;
      n06 -> n05;
      n04 -> n01;
      n04 -> n05;
      n07 -> n06;
      n07 -> n04;
      n07 -> n08;
      n01 -> n02;
      n01 -> n05;
      n09 -> n00;
      n09 -> n04;
      n09 -> n08;
      n08 -> n06;
      n02 -> n05;
      n10 -> n04;
    }
    """

    g = FrozenGraph(make_initialized_set_graph_builder(dependencies))

    # make the rank sub graphs
    ranks = dict_of(set)
    for id in xrange(g.num_nodes):
        name, rank, color = g.get_node_label(id)
        ranks[rank].add('n%02d' %(id,))
    rankglobals = list()
    for rank, names in sorted(ranks.iteritems()):
        rankglobals.append('{rank=same; "' + '"; "'.join(sorted(names)) + '";}')

    # log it
    globals=['node [shape=box];', 'ranksep=0.4;'] + rankglobals
    node_label_callback=lambda x, *_: str(x[0])    
    #node_attributes_callback=lambda x, *_: ['color=%s' % (x[2],)]
    node_attributes_callback=lambda x, *_: ['style=bold', 'shape=octagon'] if x[2] else ['style=bold', 'shape=box']
    for line in g.dot_iter(globals=globals, node_label_callback=node_label_callback, node_attributes_callback=node_attributes_callback):
        print line,

    # display it
    do_display and g.dot_display(globals=globals, node_label_callback=node_label_callback, node_attributes_callback=node_attributes_callback)