

import itertools
from dataclasses import dataclass
from io import StringIO
from typing import Optional

import pyarrow as pa

import datasets
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


UpperCamelCase__					: Union[str, Any]								=      datasets.utils.logging.get_logger(__name__)



@dataclass
class  __snake_case					(      datasets.BuilderConfig    ):
  __lowerCAmelCase       :		Optional[datasets.Features]  	=					None
  __lowerCAmelCase       :		str  	=					"utf-8"
  __lowerCAmelCase       :		Optional[str]  	=					None
  __lowerCAmelCase       :		int  	=					10 << 20  # 10MB
  __lowerCAmelCase       :		bool  	=					False
  __lowerCAmelCase       :		str  	=					"line"






class  __snake_case					(      datasets.ArrowBasedBuilder    ):
  __lowerCAmelCase       :		int  	=					TextConfig

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

  def   lowerCAmelCase__       (   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}""")
      SCREAMING_SNAKE_CASE_													=  dl_manager.download_and_extract(self.config.data_files)
      if isinstance(_A  ,						(str, list, tuple)):
          SCREAMING_SNAKE_CASE_													=  data_files
          if isinstance(_A  ,						_A):
              SCREAMING_SNAKE_CASE_													=  [files]
          SCREAMING_SNAKE_CASE_													=  [dl_manager.iter_files(_A) for file in files]
          return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,						gen_kwargs={'files': files})]
      SCREAMING_SNAKE_CASE_													=  []
      for split_name, files in data_files.items():
          if isinstance(_A  ,						_A):
              SCREAMING_SNAKE_CASE_													=  [files]
          SCREAMING_SNAKE_CASE_													=  [dl_manager.iter_files(_A) for file in files]
          splits.append(datasets.SplitGenerator(name=_A  ,						gen_kwargs={'files': files}))
      return splits

  def   lowerCAmelCase__       (   self  ,						_A):


      if self.config.features is not None:
          SCREAMING_SNAKE_CASE_													=  self.config.features.arrow_schema
          if all(not require_storage_cast(_A) for feature in self.config.features.values()):
              # cheaper cast
              SCREAMING_SNAKE_CASE_													=  pa_table.cast(_A)
          else:
              # more expensive cast; allows str <-> int/float or str to Audio for example
              SCREAMING_SNAKE_CASE_													=  table_cast(_A  ,						_A)
          return pa_table
      else:
          return pa_table.cast(pa.schema({'text': pa.string()}))


  def   lowerCAmelCase__       (   self  ,						_A):
      SCREAMING_SNAKE_CASE_													=  list(self.config.features) if self.config.features is not None else ["text"]
      for file_idx, file in enumerate(itertools.chain.from_iterable(_A)):
          # open in text mode, by default translates universal newlines ("\n", "\r\n" and "\r") into "\n"
          with open(_A  ,						encoding=self.config.encoding  ,						errors=self.config.errors) as f:
              if self.config.sample_by == "line":
                  SCREAMING_SNAKE_CASE_													=  0
                  while True:
                      SCREAMING_SNAKE_CASE_													=  f.read(self.config.chunksize)
                      if not batch:
                          break
                      batch += f.readline()  # finish current line
                      # StringIO.readlines, by default splits only on "\n" (and keeps line breaks)
                      SCREAMING_SNAKE_CASE_													=  StringIO(_A).readlines()
                      if not self.config.keep_linebreaks:
                          SCREAMING_SNAKE_CASE_													=  [line.rstrip('\n') for line in batch]
                      SCREAMING_SNAKE_CASE_													=  pa.Table.from_arrays([pa.array(_A)]  ,						names=_A)
                      # 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(_A)
                      batch_idx += 1
              elif self.config.sample_by == "paragraph":
                  SCREAMING_SNAKE_CASE_													=  0
                  SCREAMING_SNAKE_CASE_													=  ""
                  while True:
                      SCREAMING_SNAKE_CASE_													=  f.read(self.config.chunksize)
                      if not new_batch:
                          break
                      batch += new_batch
                      batch += f.readline()  # finish current line
                      SCREAMING_SNAKE_CASE_													=  batch.split('\n\n')
                      SCREAMING_SNAKE_CASE_													=  pa.Table.from_arrays(
                          [pa.array([example for example in batch[:-1] if example])]  ,						names=_A)
                      # 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(_A)
                      batch_idx += 1
                      SCREAMING_SNAKE_CASE_													=  batch[-1]
                  if batch:
                      SCREAMING_SNAKE_CASE_													=  pa.Table.from_arrays([pa.array([batch])]  ,						names=_A)
                      yield (file_idx, batch_idx), self._cast_table(_A)
              elif self.config.sample_by == "document":
                  SCREAMING_SNAKE_CASE_													=  f.read()
                  SCREAMING_SNAKE_CASE_													=  pa.Table.from_arrays([pa.array([text])]  ,						names=_A)
                  yield file_idx, self._cast_table(_A)



from sklearn.metrics import mean_absolute_error

import datasets


UpperCamelCase__					: Union[str, Any]								=      "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n"

UpperCamelCase__					: Optional[Any]								=      "\\nMean Absolute Error (MAE) is the mean of the magnitude of difference between the predicted and actual\nvalues.\n"


UpperCamelCase__					: Tuple								=      "\nArgs:\n    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Estimated target values.\n    references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Ground truth (correct) target values.\n    sample_weight: array-like of shape (n_samples,), default=None\n        Sample weights.\n    multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"\n        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n                 \"raw_values\" : Returns a full set of errors in case of multioutput input.\n\n                 \"uniform_average\" : Errors of all outputs are averaged with uniform weight.\n\nReturns:\n    mae : mean absolute error.\n        If multioutput is \"raw_values\", then mean absolute error is returned for each output separately. If multioutput is \"uniform_average\" or an ndarray of weights, then the weighted average of all output errors is returned.\n        MAE output is non-negative floating point. The best value is 0.0.\nExamples:\n\n    >>> mae_metric = datasets.load_metric(\"mae\")\n    >>> predictions = [2.5, 0.0, 2, 8]\n    >>> references = [3, -0.5, 2, 7]\n    >>> results = mae_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'mae': 0.5}\n\n    If you're using multi-dimensional lists, then set the config as follows :\n\n    >>> mae_metric = datasets.load_metric(\"mae\", \"multilist\")\n    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> references = [[0, 2], [-1, 2], [8, -5]]\n    >>> results = mae_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'mae': 0.75}\n    >>> results = mae_metric.compute(predictions=predictions, references=references, multioutput='raw_values')\n    >>> print(results)\n    {'mae': array([0.5, 1. ])}\n"



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

  def   lowerCAmelCase__       (   self):
      return datasets.MetricInfo(
          description=_DESCRIPTION  ,						citation=_CITATION  ,						inputs_description=_KWARGS_DESCRIPTION  ,						features=datasets.Features(self._get_feature_types())  ,						reference_urls=[
              'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_error.html'
          ]  ,						)

  def   lowerCAmelCase__       (   self):
      if self.config_name == "multilist":
          return {
              "predictions": datasets.Sequence(datasets.Value('float')),
              "references": datasets.Sequence(datasets.Value('float')),
          }
      else:
          return {
              "predictions": datasets.Value('float'),
              "references": datasets.Value('float'),
          }


  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A=None  ,						_A="uniform_average"):
      SCREAMING_SNAKE_CASE_													=  mean_absolute_error(_A  ,						_A  ,						sample_weight=_A  ,						multioutput=_A)

      return {"mae": mae_score}


