






"""simple docstring"""







import itertools
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast
from datasets.utils.py_utils import Literal


_UpperCamelCase	     =    datasets.utils.logging.get_logger(__name__)

_UpperCamelCase	     =    ["""names""", """prefix"""]
_UpperCamelCase	     =    ["""warn_bad_lines""", """error_bad_lines""", """mangle_dupe_cols"""]
_UpperCamelCase	     =    ["""encoding_errors""", """on_bad_lines"""]
_UpperCamelCase	     =    ["""date_format"""]







@dataclass
class    lowerCamelCase__   (			datasets.BuilderConfig					):
    SCREAMING_SNAKE_CASE						=     ","
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     "infer"
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     False
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     False
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     "."
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     '"'
    SCREAMING_SNAKE_CASE						=     0
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     0
    SCREAMING_SNAKE_CASE						=     True
    SCREAMING_SNAKE_CASE						=     False
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     10_000
    SCREAMING_SNAKE_CASE						=     None
    SCREAMING_SNAKE_CASE						=     "strict"
    SCREAMING_SNAKE_CASE						=     "error"
    SCREAMING_SNAKE_CASE						=     None






    def 							_UpperCamelCase				(					self   ):


          if self.delimiter is not None:
                UpperCAmelCase							     =			self.delimiter
          if self.column_names is not None:
                UpperCAmelCase							     =			self.column_names






    @property
    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			{
              """sep""": self.sep,
              """header""": self.header,
              """names""": self.names,
              """index_col""": self.index_col,
              """usecols""": self.usecols,
              """prefix""": self.prefix,
              """mangle_dupe_cols""": self.mangle_dupe_cols,
              """engine""": self.engine,
              """converters""": self.converters,
              """true_values""": self.true_values,
              """false_values""": self.false_values,
              """skipinitialspace""": self.skipinitialspace,
              """skiprows""": self.skiprows,
              """nrows""": self.nrows,
              """na_values""": self.na_values,
              """keep_default_na""": self.keep_default_na,
              """na_filter""": self.na_filter,
              """verbose""": self.verbose,
              """skip_blank_lines""": self.skip_blank_lines,
              """thousands""": self.thousands,
              """decimal""": self.decimal,
              """lineterminator""": self.lineterminator,
              """quotechar""": self.quotechar,
              """quoting""": self.quoting,
              """escapechar""": self.escapechar,
              """comment""": self.comment,
              """encoding""": self.encoding,
              """dialect""": self.dialect,
              """error_bad_lines""": self.error_bad_lines,
              """warn_bad_lines""": self.warn_bad_lines,
              """skipfooter""": self.skipfooter,
              """doublequote""": self.doublequote,
              """memory_map""": self.memory_map,
              """float_precision""": self.float_precision,
              """chunksize""": self.chunksize,
              """encoding_errors""": self.encoding_errors,
              """on_bad_lines""": self.on_bad_lines,
              """date_format""": self.date_format,
          }

          # some kwargs must not be passed if they don't have a default value
          # some others are deprecated and we can also not pass them if they are the default value
          for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS:
                if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig()      ,__lowerCAmelCase   ):
                      del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 2.0 new arguments
          if not (datasets.config.PANDAS_VERSION.major >= 2):
                for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS:
                      del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 1.3 new arguments
          if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3):
                for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS:
                      del pd_read_csv_kwargs[pd_read_csv_parameter]

          return pd_read_csv_kwargs







class    lowerCamelCase__   (			datasets.ArrowBasedBuilder					):
    SCREAMING_SNAKE_CASE						=     CsvConfig






    def 							_UpperCamelCase				(					self   ):
          return datasets.DatasetInfo(features=self.config.features   )






    def 							_UpperCamelCase				(					self      ,A   ):


          if not self.config.data_files:
                raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}'''   )
          UpperCAmelCase							     =			dl_manager.download_and_extract(self.config.data_files   )
          if isinstance(__lowerCAmelCase      ,(str, list, tuple)   ):
                UpperCAmelCase							     =			data_files
                if isinstance(__lowerCAmelCase      ,__lowerCAmelCase   ):
                      UpperCAmelCase							     =			[files]
                UpperCAmelCase							     =			[dl_manager.iter_files(__lowerCAmelCase   ) for file in files]
                return [datasets.SplitGenerator(name=datasets.Split.TRAIN      ,gen_kwargs={"""files""": files}   )]
          UpperCAmelCase							     =			[]
          for split_name, files in data_files.items():
                if isinstance(__lowerCAmelCase      ,__lowerCAmelCase   ):
                      UpperCAmelCase							     =			[files]
                UpperCAmelCase							     =			[dl_manager.iter_files(__lowerCAmelCase   ) for file in files]
                splits.append(datasets.SplitGenerator(name=__lowerCAmelCase      ,gen_kwargs={"""files""": files}   )   )
          return splits






    def 							_UpperCamelCase				(					self      ,A   ):


          if self.config.features is not None:
                UpperCAmelCase							     =			self.config.features.arrow_schema
                if all(not require_storage_cast(__lowerCAmelCase   ) for feature in self.config.features.values()   ):
                      # cheaper cast
                      UpperCAmelCase							     =			pa.Table.from_arrays([pa_table[field.name] for field in schema]      ,schema=__lowerCAmelCase   )
                else:
                      # more expensive cast; allows str <-> int/float or str to Audio for example
                      UpperCAmelCase							     =			table_cast(__lowerCAmelCase      ,__lowerCAmelCase   )
          return pa_table






    def 							_UpperCamelCase				(					self      ,A   ):
          UpperCAmelCase							     =			self.config.features.arrow_schema if self.config.features else None
          # dtype allows reading an int column as str
          UpperCAmelCase							     =			(
              {
                  name: dtype.to_pandas_dtype() if not require_storage_cast(__lowerCAmelCase   ) else object
                  for name, dtype, feature in zip(schema.names      ,schema.types      ,self.config.features.values()   )
              }
              if schema is not None
              else None
          )
          for file_idx, file in enumerate(itertools.chain.from_iterable(__lowerCAmelCase   )   ):
                UpperCAmelCase							     =			pd.read_csv(__lowerCAmelCase      ,iterator=__lowerCAmelCase      ,dtype=__lowerCAmelCase      ,**self.config.pd_read_csv_kwargs   )
                try:
                      for batch_idx, df in enumerate(__lowerCAmelCase   ):
                            UpperCAmelCase							     =			pa.Table.from_pandas(__lowerCAmelCase   )
                            # Uncomment for debugging (will print the Arrow table size and elements)
                            # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                            # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                            yield (file_idx, batch_idx), self._cast_table(__lowerCAmelCase   )
                except ValueError as e:
                      logger.error(F'''Failed to read file \'{file}\' with error {type(__lowerCAmelCase   )}: {e}'''   )
                      raise




'''simple docstring'''




import sacrebleu as scb
from packaging import version
from sacrebleu import TER

import datasets


__lowercase        =    '''\
@inproceedings{snover-etal-2006-study,
    title = "A Study of Translation Edit Rate with Targeted Human Annotation",
    author = "Snover, Matthew  and
      Dorr, Bonnie  and
      Schwartz, Rich  and
      Micciulla, Linnea  and
      Makhoul, John",
    booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers",
    month = aug # " 8-12",
    year = "2006",
    address = "Cambridge, Massachusetts, USA",
    publisher = "Association for Machine Translation in the Americas",
    url = "https://aclanthology.org/2006.amta-papers.25",
    pages = "223--231",
}
@inproceedings{post-2018-call,
    title = "A Call for Clarity in Reporting {BLEU} Scores",
    author = "Post, Matt",
    booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers",
    month = oct,
    year = "2018",
    address = "Belgium, Brussels",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/W18-6319",
    pages = "186--191",
}
'''

__lowercase        =    '''\
TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a
hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu
(https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found
here: https://github.com/jhclark/tercom.

The implementation here is slightly different from sacrebleu in terms of the required input format. The length of
the references and hypotheses lists need to be the same, so you may need to transpose your references compared to
sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534

See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.
'''

__lowercase        =    '''
Produces TER scores alongside the number of edits and reference length.

Args:
    predictions (list of str): The system stream (a sequence of segments).
    references (list of list of str): A list of one or more reference streams (each a sequence of segments).
    normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
    ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.
    support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters,
                                    as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana.
                                    Only applies if `normalized = True`. Defaults to `False`.
    case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.

Returns:
    \'score\' (float): TER score (num_edits / sum_ref_lengths * 100)
    \'num_edits\' (int): The cumulative number of edits
    \'ref_length\' (float): The cumulative average reference length

Examples:
    Example 1:
        >>> predictions = ["does this sentence match??",
        ...                     "what about this sentence?",
        ...                     "What did the TER metric user say to the developer?"]
        >>> references = [["does this sentence match", "does this sentence match!?!"],
        ...             ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
        ...             ["Your jokes are...", "...TERrible"]]
        >>> ter = datasets.load_metric("ter")
        >>> results = ter.compute(predictions=predictions,
        ...                         references=references,
        ...                         case_sensitive=True)
        >>> print(results)
        {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0}

    Example 2:
        >>> predictions = ["does this sentence match??",
        ...                     "what about this sentence?"]
        >>> references = [["does this sentence match", "does this sentence match!?!"],
        ...             ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
        >>> ter = datasets.load_metric("ter")
        >>> results = ter.compute(predictions=predictions,
        ...                         references=references,
        ...                         case_sensitive=True)
        >>> print(results)
        {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0}

    Example 3:
        >>> predictions = ["does this sentence match??",
        ...                     "what about this sentence?"]
        >>> references = [["does this sentence match", "does this sentence match!?!"],
        ...             ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
        >>> ter = datasets.load_metric("ter")
        >>> results = ter.compute(predictions=predictions,
        ...                         references=references,
        ...                         normalized=True,
        ...                         case_sensitive=True)
        >>> print(results)
        {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5}

    Example 4:
        >>> predictions = ["does this sentence match??",
        ...                     "what about this sentence?"]
        >>> references = [["does this sentence match", "does this sentence match!?!"],
        ...             ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]]
        >>> ter = datasets.load_metric("ter")
        >>> results = ter.compute(predictions=predictions,
        ...                         references=references,
        ...                         ignore_punct=True,
        ...                         case_sensitive=False)
        >>> print(results)
        {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0}

    Example 5:
        >>> predictions = ["does this sentence match??",
        ...                    "what about this sentence?",
        ...                    "What did the TER metric user say to the developer?"]
        >>> references = [["does this sentence match", "does this sentence match!?!"],
        ...             ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"],
        ...             ["Your jokes are...", "...TERrible"]]
        >>> ter = datasets.load_metric("ter")
        >>> results = ter.compute(predictions=predictions,
        ...                         references=references,
        ...                         ignore_punct=True,
        ...                         case_sensitive=False)
        >>> print(results)
        {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0}
'''






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					,		_KWARGS_DESCRIPTION      )
class 							a__(   datasets.Metric      ):


			'''simple docstring'''






			def 				a_			(  self):




					"""simple docstring"""


					if version.parse(scb.__version__) < version.parse("""1.4.12"""):
							raise ImportWarning(
							    """To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n"""
							    """You can install it with `pip install \"sacrebleu>=1.4.12\"`.""")
					return datasets.MetricInfo(
					    description=_DESCRIPTION			,       citation=_CITATION			,       homepage="""http://www.cs.umd.edu/~snover/tercom/"""			,       inputs_description=_KWARGS_DESCRIPTION			,       features=datasets.Features(
					        {
					            """predictions""": datasets.Value("""string"""			,       id="""sequence"""),
					            """references""": datasets.Sequence(datasets.Value("""string"""			,       id="""sequence""")			,       id="""references"""),
					        })			,       codebase_urls=["""https://github.com/mjpost/sacreBLEU#ter"""]			,       reference_urls=[
					        """https://github.com/jhclark/tercom""",
					    ]			,       )







			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase = False			,       __lowerCAmelCase = False			,       __lowerCAmelCase = False			,       __lowerCAmelCase = False			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    len(references[0])
					if any(len(__lowerCAmelCase) != references_per_prediction for refs in references):
							raise ValueError("""Sacrebleu requires the same number of references for each prediction""")
					lowerCAmelCase     =    [[refs[i] for refs in references] for i in range(__lowerCAmelCase)]

					lowerCAmelCase     =    TER(
					    normalized=__lowerCAmelCase			,       no_punct=__lowerCAmelCase			,       asian_support=__lowerCAmelCase			,       case_sensitive=__lowerCAmelCase			,       )
					lowerCAmelCase     =    sb_ter.corpus_score(__lowerCAmelCase			,       __lowerCAmelCase)

					return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}




