Exemple #1
0
    def _py_func(self, hypotheses, references):
        """Wrapper function that converts tensors to unicode and slices
      them until the EOS token is found.
    """
        # Deal with byte chars
        if hypotheses.dtype.kind == np.dtype("U"):
            hypotheses = np.char.encode(hypotheses, "utf-8")
        if references.dtype.kind == np.dtype("U"):
            references = np.char.encode(references, "utf-8")

        # Convert back to unicode object
        hypotheses = [_.decode("utf-8") for _ in hypotheses]
        references = [_.decode("utf-8") for _ in references]

        # Slice all hypotheses and references up to SOS -> EOS
        sliced_hypotheses = [
            postproc.slice_text(_, self._eos_token, self._sos_token)
            for _ in hypotheses
        ]
        sliced_references = [
            postproc.slice_text(_, self._eos_token, self._sos_token)
            for _ in references
        ]

        # Apply postprocessing function
        if self._postproc_fn:
            sliced_hypotheses = [
                self._postproc_fn(_) for _ in sliced_hypotheses
            ]
            sliced_references = [
                self._postproc_fn(_) for _ in sliced_references
            ]

        return self.metric_fn(sliced_hypotheses, sliced_references)
  def _py_func(self, hypotheses, references):
    """Wrapper function that converts tensors to unicode and slices
      them until the EOS token is found.
    """
    # Deal with byte chars
    if hypotheses.dtype.kind == np.dtype("U"):
      hypotheses = np.char.encode(hypotheses, "utf-8")
    if references.dtype.kind == np.dtype("U"):
      references = np.char.encode(references, "utf-8")

    # Convert back to unicode object
    hypotheses = [_.decode("utf-8") for _ in hypotheses]
    references = [_.decode("utf-8") for _ in references]

    # Slice all hypotheses and references up to SOS -> EOS
    sliced_hypotheses = [postproc.slice_text(
        _, self._eos_token, self._sos_token) for _ in hypotheses]
    sliced_references = [postproc.slice_text(
        _, self._eos_token, self._sos_token) for _ in references]

    # Apply postprocessing function
    if self._postproc_fn:
      sliced_hypotheses = [self._postproc_fn(_) for _ in sliced_hypotheses]
      sliced_references = [self._postproc_fn(_) for _ in sliced_references]

    return self.metric_fn(sliced_hypotheses, sliced_references) #pylint: disable=E1102