你如何给出一个(openFSTmade)FST输入?输出到哪里去了?

2024-06-11 08:07:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在开始之前,请注意,我使用的是linuxshell(通过Python中的using subprocess.call()),我使用的是openFST。在

我一直在筛选有关openFST的文档和问题,但我似乎找不到这个问题的答案:如何真正地为openFST定义、编译和组合的FST提供输入?输出到哪里去了?我是否只执行“fstproject”?如果是这样的话,我如何,比如说,给它一个字符串来转换,并在到达结束状态时打印各种转换?在

如果这个问题看起来很明显,我很抱歉。我对openFST还不是很熟悉。在


Tags: 字符串答案文档定义状态callsubprocessusing
2条回答

一种方法是创建执行转换的机器。 一个非常简单的例子是将字符串大写。在

M.wfst公司

0 0 a A
0 0 b B
0 0 c C
0

附带的符号文件包含一行,用于字母表中的每个符号。注0是为空(epsilon)转换保留的,在许多操作中有特殊的含义。在

M.syms公司

^{pr2}$

然后编译机器

fstcompile  isymbols=M.syms  osymbols=M.syms M.wfst > M.ofst

对于输入字符串“abc”,创建一个线性链自动机,这是一个从左到右的链,每个字符都有一个弧。这是一个接受方,所以我们只需要 输入符号。在

I.wfst公司

0 1 a
1 2 b
2 3 c
3  

编译为接受程序

fstcompile  isymbols=M.syms  acceptor I.wfst > I.ofst

然后组装机器并打印

fstcompose I.ofst M.ofst | fstprint  isymbols=M.syms  osymbols=M.syms 

这将产生输出

0   1   a   A
1   2   b   B
2   3   c   C
3

fstcompose的输出是输入字符串的所有传输的格。(在这种情况下只有一个)。如果M.ofst更复杂,FSTSLUTestTPATH可以使用标志唯一n-最短=n来提取n个字符串。这个输出又是一个转换器,您可以丢弃FSTPress输出,或者使用C++代码和OpenFST库来运行深度优先搜索来提取字符串。在

插入fstproject_output将把输出转换为只包含输出标签的接受程序。在

fstcompose I.ofst M.ofst | fstproject  project_output |  fstprint  isymbols=M.syms  osymbols=M.syms 

给出了以下内容

0  1  A  A
1  2  B  B
2  3  C  C
3

这是一个acceptor,因为输入和输出标签是相同的,acceptor选项可以用来生成更简洁的输出。在

 fstcompose I.ofst M.ofst | fstproject  project_output |  fstprint  isymbols=M.syms  acceptor

保罗·狄克逊的例子很好。由于OP使用Python,我想我应该添加一个关于如何使用Open FST's Python wrapper来“运行”传感器的快速示例。遗憾的是,你不能用开放式FST创建“线性链自动机”,但它很容易实现自动化,如下所示:

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

让我们定义一个大写字母“a”的简单传感器:

^{pr2}$

enter image description here

现在我们可以简单地使用传感器:

apply_fst(list("abab"), caps_A)

输出: enter image description here

要了解如何使用它作为接受者,请看我的other answer

相关问题 更多 >