How do you even give an (openFST-made) FST input? Where does the output go?

后端 未结 2 468
不知归路
不知归路 2021-02-07 03:04

Before I start, note that I\'m using the linux shell (via using subprocess.call() from Python), and I am using openFST.

I\'ve been sifting through documents

2条回答
  •  广开言路
    2021-02-07 03:39

    The example from Paul Dixon is great. As the OP uses Python I thought I'd add a quick example on how you can "run" transducers with Open FST's Python wrapper. It's a shame that you can not create "linear chain automata" with Open FST, but it's simple to automate as seen below:

    def linear_fst(elements, automata_op, keep_isymbols=True, **kwargs):
        """Produce a linear automata."""
        compiler = fst.Compiler(isymbols=automata_op.input_symbols().copy(), 
                                acceptor=keep_isymbols,
                                keep_isymbols=keep_isymbols, 
                                **kwargs)
    
        for i, el in enumerate(elements):
            print >> compiler, "{} {} {}".format(i, i+1, el)
        print >> compiler, str(i+1)
    
        return compiler.compile()
    
    def apply_fst(elements, automata_op, is_project=True, **kwargs):
        """Compose a linear automata generated from `elements` with `automata_op`.
    
        Args:
            elements (list): ordered list of edge symbols for a linear automata.
            automata_op (Fst): automata that will be applied.
            is_project (bool, optional): whether to keep only the output labels.
            kwargs:
                Additional arguments to the compiler of the linear automata .
        """
        linear_automata = linear_fst(elements, automata_op, **kwargs)
        out = fst.compose(linear_automata, automata_op)
        if is_project:
            out.project(project_output=True)
        return out
    

    Let's define a simple Transducer that uppercases the letter "a":

    f_ST = fst.SymbolTable()
    f_ST.add_symbol("", 0)
    f_ST.add_symbol("A", 1)
    f_ST.add_symbol("a", 2)
    f_ST.add_symbol("b", 3)
    compiler = fst.Compiler(isymbols=f_ST, osymbols=f_ST, keep_isymbols=True, keep_osymbols=True)
    
    print >> compiler, "0 0 a A"
    print >> compiler, "0 0 b b"
    print >> compiler, "0"
    caps_A = compiler.compile()
    caps_A
    

    Now we can simply apply the transducer using :

    apply_fst(list("abab"), caps_A)
    

    Output:

    To see how to use it for an acceptor look at my other answer

提交回复
热议问题