Exemple #1
0
    def from_openfst(cls, s: str, acceptor: bool = True) -> 'Fsa':
        '''Create an Fsa from a string in OpenFST format.

        The given string `s` consists of lines with the following format:

        (1) When it represents an acceptor::

                src_state dest_state label score

        (2) When it represents a transducer::

                src_state dest_state label aux_label score

        The line for the final state consists of two fields::

                final_state score

        Note:
          Fields are separated by space(s), tab(s) or both. The `score`
          field is a float, while other fields are integers.

          There might be multiple final states. Also, OpenFst may omit the score
          if it is 0.0.

        Args:
          s:
            The input string. Refer to the above comment for its format.
          acceptor:
            Optional. If true, interpret the input string as an acceptor;
            otherwise, interpret it as a transducer.
        '''
        arcs, aux_labels = _k2.fsa_from_str(s, acceptor, True)
        return Fsa(arcs, aux_labels=aux_labels)
Exemple #2
0
    def from_str(cls, s: str) -> 'Fsa':
        '''Create an Fsa from a string in the k2 format.
        (See also from_openfst).

        The given string `s` consists of lines with the following format:

        (1) When it represents an acceptor::

                src_state dest_state label score

        (2) When it represents a transducer::

                src_state dest_state label aux_label score

        The line for the final state consists of only one field::

                final_state

        Note:
          Fields are separated by space(s), tab(s) or both. The `score`
          field is a float, while other fields are integers.

        Caution:
          The first column has to be non-decreasing.

        Caution:
          The final state has the largest state number. There is **ONLY**
          ONE final state. All arcs that are connected to the final state
          have label -1. If there are aux_labels, they are also -1 for
          arcs entering the final state.

        Args:
          s:
            The input string. Refer to the above comment for its format.
        '''
        # Figure out acceptor/transducer for k2 fsa.
        acceptor = True
        line = s.strip().split('\n', 1)[0]
        fields = line.strip().split()
        assert len(fields) == 4 or len(fields) == 5
        if len(fields) == 5:
            acceptor = False
        arcs, aux_labels = _k2.fsa_from_str(s, acceptor, False)
        ans = Fsa(arcs, aux_labels=aux_labels)
        return ans
Exemple #3
0
    def from_openfst(cls,
                     s: str,
                     acceptor: Optional[bool] = None,
                     num_aux_labels: Optional[int] = None,
                     aux_label_names: Optional[List[str]] = None) -> 'Fsa':
        '''Create an Fsa from a string in OpenFST format (or a slightly more
        general format, if num_aux_labels > 1). See also :func:`from_str`.

        The given string `s` consists of lines with the following format::

           src_state dest_state label [aux_label1 aux_label2...] [cost]

       (the cost defaults to 0.0 if not present).

        The line for the final state consists of two fields::

           final_state [cost]

        Note:
          Fields are separated by space(s), tab(s) or both. The `cost`
          field is a float, while other fields are integers.

          There might be multiple final states. Also, OpenFst may omit the cost
          if it is 0.0.

        Caution:
          We use `cost` here to indicate that its value will be negated so that
          we can get `scores`. That is, `score = -1 * cost`.

        Note:
          At most one of `acceptor`, `num_aux_labels`, and `aux_label_names`
          must be supplied; if none are supplied, acceptor format is assumed.

        Args:
          s:
            The input string. Refer to the above comment for its format.
          acceptor:
            Set to true to denote acceptor format which is num_aux_labels == 0,
            or false to denote transducer format (i.e. num_aux_labels == 1
            with name 'aux_labels').
          num_aux_labels:
            The number of auxiliary labels to expect on each line (in addition
            to the 'acceptor' label; is 1 for traditional transducers but can be
            any non-negative number.
          aux_label_names:
            If provided, the length of this list dictates the number of
            aux_labels. By default the names are 'aux_labels', 'aux_labels2',
            'aux_labels3' and so on.
        '''

        (num_aux_labels, aux_label_names) = \
                get_aux_label_info(acceptor, num_aux_labels, aux_label_names)
        try:
            arcs, aux_labels = _k2.fsa_from_str(s, num_aux_labels, True)
            ans = Fsa(arcs)
            if num_aux_labels != 0:
                for i in range(num_aux_labels):
                    setattr(ans, aux_label_names[i], aux_labels[i, :])
            return ans
        except Exception:
            raise ValueError(
                f'The following is not a valid Fsa in OpenFst format '
                f'(with num_aux_labels={num_aux_labels}): {s}')
Exemple #4
0
    def from_str(cls,
                 s: str,
                 acceptor: Optional[bool] = None,
                 num_aux_labels: Optional[int] = None,
                 aux_label_names: Optional[List[str]] = None) -> 'Fsa':
        '''Create an Fsa from a string in the k2 or OpenFst format.
        (See also :func:`from_openfst`).

        The given string `s` consists of lines with the following format::

          src_state dest_state label [aux_label1 aux_label2...] [score]

        The line for the final state consists of only one field::

                final_state

        Note:
          Fields are separated by space(s), tab(s) or both. The `score`
          field is a float, while other fields are integers.

        Caution:
          The first column has to be non-decreasing.

        Caution:
          The final state has the largest state number. There is **ONLY**
          ONE final state. All arcs that are connected to the final state
          have label -1. If there are aux_labels, they are also -1 for
          arcs entering the final state.

        Note:
          At most one of `acceptor`, `num_aux_labels`, and `aux_label_names`
          must be supplied; if none are supplied, acceptor format is assumed.

        Args:
          s:
            The input string. Refer to the above comment for its format.

          acceptor:
            Set to true to denote acceptor format which is num_aux_labels == 0,
            or false to denote transducer format (i.e. num_aux_labels == 1
            with name 'aux_labels').
          num_aux_labels:
            The number of auxiliary labels to expect on each line (in addition
            to the 'acceptor' label; is 1 for traditional transducers but can be
            any non-negative number.  The names of the aux_labels default to
            'aux_labels' then 'aux_labels2', 'aux_labels3' and so on.
          aux_label_names:
            If provided, the length of this list dictates the number of
            aux_labels and this list dictates their names.
        '''
        (num_aux_labels, aux_label_names) = \
                get_aux_label_info(acceptor, num_aux_labels, aux_label_names)
        try:
            arcs, aux_labels = _k2.fsa_from_str(s, num_aux_labels, False)
            ans = Fsa(arcs)
            if aux_labels is not None:
                for i in range(aux_labels.shape[0]):
                    setattr(ans, aux_label_names[i], aux_labels[i, :])
            return ans
        except Exception:
            raise ValueError(f'The following is not a valid Fsa (with '
                             f'num_aux_labels={num_aux_labels}): {s}')