Example #1
0
    def test(self):
        s0 = '''
            0 1 1 0.1
            0 2 2 0.2
            1 2 3 0.3
            1 3 -1 0.4
            2 3 -1 0.5
            2 1 5 0.55
            3
        '''
        s1 = '''
            0 1 -1 0.6
            1
        '''
        s2 = '''
            0 1 6 0.7
            1 0 7 0.8
            1 0 8 0.9
            1 2 -1 1.0
            2
        '''

        fsa0 = k2.Fsa.from_str(s0)
        fsa1 = k2.Fsa.from_str(s1)
        fsa2 = k2.Fsa.from_str(s2)

        fsa_vec = k2.create_fsa_vec([fsa0, fsa1, fsa2])

        fsa = k2.union(fsa_vec)
        dot = k2.to_dot(fsa)
        dot.render('/tmp/fsa', format='pdf')
        # the fsa is saved to /tmp/fsa.pdf
        print(fsa)
Example #2
0
    def test_symbol_table_and_dot(self):
        isym_str = '''
            <eps> 0
            a 1
            b 2
            c 3
        '''

        osym_str = '''
            <eps> 0
            x 1
            y 2
            z 3
        '''
        symbols = k2.SymbolTable.from_str(isym_str)
        aux_symbols = k2.SymbolTable.from_str(osym_str)

        rules = '''
            0 1 1 1 0.5
            0 1 2 2 1.5
            1 2 3 3  2.5
            2 3 -1 0 3.5
            3
        '''
        fsa = k2.Fsa.from_str(_remove_leading_spaces(rules))
        fsa.symbols = symbols
        fsa.aux_symbols = aux_symbols
        dot = k2.to_dot(fsa)
        dot.render('/tmp/fsa', format='pdf')
Example #3
0
    def test_symbol_table_and_dot(self):
        isym_str = '''
            <eps> 0
            a 1
            b 2
            c 3
        '''

        osym_str = '''
            <eps> 0
            x 1
            y 2
            z 3
        '''
        symbols = k2.SymbolTable.from_str(isym_str)
        aux_symbols = k2.SymbolTable.from_str(osym_str)

        rules = '''
            0 1 1 1 0.5
            0 1 2 2 1.5
            1 2 3 3  2.5
            2 3 -1 0 3.5
            3
        '''
        fsa = k2.Fsa.from_str(_remove_leading_spaces(rules))
        fsa.symbols = symbols
        fsa.aux_symbols = aux_symbols
        dot = k2.to_dot(fsa)

        import tempfile
        with tempfile.TemporaryDirectory() as tmp_dir:
            dot.render(filename='fsa',
                       directory=tmp_dir,
                       format='pdf',
                       cleanup=True)
            # the fsa is saved to tmp_dir/fsa.pdf
            import os
            os.system('ls -l {}/fsa.pdf'.format(tmp_dir))