How to use the pynini.transducer function in pynini

To help you get started, we’ve selected a few pynini examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kylebgorman / EditTransducer / edit_transducer / edit_transducer.py View on Github external
"""Constructor.

    Args:
      alphabet: edit alphabet (an iterable of strings).
      insert_cost: the cost for the insertion operation.
      delete_cost: the cost for the deletion operation.
      substitute_cost: the cost for the substitution operation.
    """
    # Left factor; note that we divide the edit costs by two because they also
    # will be incurred when traversing the right factor.
    match = union(*alphabet).optimize(True)
    i_insert = transducer("", "[{}]".format(self.INSERT),
                          weight=insert_cost / 2).optimize(True)
    i_delete = transducer(match, "[{}]".format(self.DELETE),
                          weight=delete_cost / 2).optimize(True)
    i_substitute = transducer(match, "[{}]".format(self.SUBSTITUTE),
                              weight=substitute_cost / 2).optimize(True)
    i_ops = union(match, i_insert, i_delete, i_substitute).optimize(True)
    # Right factor; this is constructed by inverting the left factor (i.e.,
    # swapping the input and output labels), then swapping the insert and delete
    # labels on what is now the input side.
    o_ops = invert(i_ops)
    syms = o_ops.input_symbols()
    insert_label = syms.find(self.INSERT)
    delete_label = syms.find(self.DELETE)
    o_ops.relabel_pairs(ipairs=((insert_label, delete_label),
                                (delete_label, insert_label)))
    # Computes the closure for both sets of ops.
    self._e_i = i_ops.closure().optimize(True)
    self._e_o = o_ops.closure().optimize(True)
github kylebgorman / EditTransducer / edit_transducer / edit_transducer.py View on Github external
alphabet,
               insert_cost=DEFAULT_INSERT_COST,
               delete_cost=DEFAULT_DELETE_COST,
               substitute_cost=DEFAULT_SUBSTITUTE_COST):
    """Constructor.

    Args:
      alphabet: edit alphabet (an iterable of strings).
      insert_cost: the cost for the insertion operation.
      delete_cost: the cost for the deletion operation.
      substitute_cost: the cost for the substitution operation.
    """
    # Left factor; note that we divide the edit costs by two because they also
    # will be incurred when traversing the right factor.
    match = union(*alphabet).optimize(True)
    i_insert = transducer("", "[{}]".format(self.INSERT),
                          weight=insert_cost / 2).optimize(True)
    i_delete = transducer(match, "[{}]".format(self.DELETE),
                          weight=delete_cost / 2).optimize(True)
    i_substitute = transducer(match, "[{}]".format(self.SUBSTITUTE),
                              weight=substitute_cost / 2).optimize(True)
    i_ops = union(match, i_insert, i_delete, i_substitute).optimize(True)
    # Right factor; this is constructed by inverting the left factor (i.e.,
    # swapping the input and output labels), then swapping the insert and delete
    # labels on what is now the input side.
    o_ops = invert(i_ops)
    syms = o_ops.input_symbols()
    insert_label = syms.find(self.INSERT)
    delete_label = syms.find(self.DELETE)
    o_ops.relabel_pairs(ipairs=((insert_label, delete_label),
                                (delete_label, insert_label)))
    # Computes the closure for both sets of ops.

pynini

Finite-state grammar compilation

MIT
Latest version published 4 months ago

Package Health Score

79 / 100
Full package analysis