Пример #1
0
def verify_if_single_path(input_str_fsa: pynini.Fst, fst: pynini.Fst):
    """Does nothing if given string FST has only one path; throws otherwise.

  If there is more than one path in the FST, then string() method on the FST
  will throw FstOpError. That exception is converted to AssertError with
  relevant error message with input string from the given string FSA.

  Args:
    input_str_fsa: Input string FSA specific to the FST. Used in the
        exception message.
    fst: FST to be verified if it has only a single path.

  Raises:
    AssertionError: If the FST is found to have more than one path.
  """
    try:
        fst.string()
    except pynini.FstOpError as e:
        raise AssertionError(
            "Expected FST to be functional but input string `{input}`"
            " produced multiple output strings: {outputs}".format(
                input=input_str_fsa.string(),
                outputs=", ".join(
                    f"`{ostring}`"
                    for ostring in fst.optimize().paths().ostrings()))) from e
Пример #2
0
def verify_identity(input_str_fsa: pynini.Fst, fst: pynini.Fst):
    """Verifies if FST produces only the input string at its minimum weight path.

  Throws AssertError with a detailed error message on verification failure;
  otherwise do nothing.

  Args:
    input_str_fsa: Input string FSA to be compared with the minimum weight path
        in the FST.
    fst: FST to be verified.

  Raises:
    AssertionError: If the verification has failed; that is, if the given FST
    produces anything other than the input string at its minimum weight path.
  """
    input_str = input_str_fsa.string()
    out_weights = collections.defaultdict(set)
    for _, out, weight in fst.optimize().paths().items():
        out_weights[int(str(weight))].add(out)
    outstrs = out_weights[min(out_weights)]

    if outstrs - {input_str}:
        raise AssertionError(
            f'Expected FST to be idenity but input `{input_str}`'
            f' produced output string(s): `{", ".join(outstrs)}`')