'''simple docstring'''





import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


SCREAMING_SNAKE_CASE_:	Tuple    =re.compile(r'\b(a|an|the)\b', re.UNICODE)

SCREAMING_SNAKE_CASE_:	Optional[int]    =None





def       lowerCAmelCase_							(   )       -> int:


     '''simple docstring'''




     UpperCAmelCase_			       =  argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."     )
     parser.add_argument("data_file"   ,       metavar="data.json"   ,       help="Input data JSON file."     )
     parser.add_argument("pred_file"   ,       metavar="pred.json"   ,       help="Model predictions."     )
     parser.add_argument(
         "--out-file"   ,       "-o"   ,       metavar="eval.json"   ,       help="Write accuracy metrics to file (default is stdout)."     )
     parser.add_argument(
         "--na-prob-file"   ,       "-n"   ,       metavar="na_prob.json"   ,       help="Model estimates of probability of no answer."     )
     parser.add_argument(
         "--na-prob-thresh"   ,       "-t"   ,       type=snake_case_   ,       default=1.0   ,       help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."   ,       )
     parser.add_argument(
         "--out-image-dir"   ,       "-p"   ,       metavar="out_images"   ,       default=snake_case_   ,       help="Save precision-recall curves to directory."     )
     parser.add_argument("--verbose"   ,       "-v"   ,       action="store_true"     )
     if len(sys.argv     ) == 1:
          parser.print_help()
          sys.exit(1     )
     return parser.parse_args()





def       lowerCAmelCase_							(   snake_case_			:					List[Any]     )       -> List[Any]:


     '''simple docstring'''




     UpperCAmelCase_			       =  {}
     for article in dataset:
          for p in article["paragraphs"]:
               for qa in p["qas"]:
                    UpperCAmelCase_			       =  bool(qa["answers"]["text"]     )
     return qid_to_has_ans





def       lowerCAmelCase_							(   snake_case_			:					int     )       -> Optional[Any]:


     '''simple docstring'''




     def remove_articles(snake_case_			:					Optional[int]     ):
          return ARTICLES_REGEX.sub(" "   ,       snake_case_     )

     def white_space_fix(snake_case_			:					Tuple     ):
          return " ".join(text.split()     )

     def remove_punc(snake_case_			:					Optional[int]     ):
          UpperCAmelCase_			       =  set(string.punctuation     )
          return "".join(ch for ch in text if ch not in exclude     )

     def lower(snake_case_			:					Tuple     ):
          return text.lower()

     return white_space_fix(remove_articles(remove_punc(lower(snake_case_     )     )     )     )





def       lowerCAmelCase_							(   snake_case_			:					Optional[int]     )       -> Dict:


     '''simple docstring'''




     if not s:
          return []
     return normalize_answer(snake_case_     ).split()





def       lowerCAmelCase_							(   snake_case_			:					Optional[Any]   ,       snake_case_			:					Union[str, Any]     )       -> Optional[Any]:


     '''simple docstring'''




     return int(normalize_answer(snake_case_     ) == normalize_answer(snake_case_     )     )





def       lowerCAmelCase_							(   snake_case_			:					List[str]   ,       snake_case_			:					Any     )       -> Optional[int]:


     '''simple docstring'''




     UpperCAmelCase_			       =  get_tokens(snake_case_     )
     UpperCAmelCase_			       =  get_tokens(snake_case_     )
     UpperCAmelCase_			       =  collections.Counter(snake_case_     ) & collections.Counter(snake_case_     )
     UpperCAmelCase_			       =  sum(common.values()     )
     if len(snake_case_     ) == 0 or len(snake_case_     ) == 0:
          # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
          return int(gold_toks == pred_toks     )
     if num_same == 0:
          return 0
     UpperCAmelCase_			       =  1.0 * num_same / len(snake_case_     )
     UpperCAmelCase_			       =  1.0 * num_same / len(snake_case_     )
     UpperCAmelCase_			       =  (2 * precision * recall) / (precision + recall)
     return fa





def       lowerCAmelCase_							(   snake_case_			:					List[str]   ,       snake_case_			:					Optional[Any]     )       -> int:


     '''simple docstring'''




     UpperCAmelCase_			       =  {}
     UpperCAmelCase_			       =  {}
     for article in dataset:
          for p in article["paragraphs"]:
               for qa in p["qas"]:
                    UpperCAmelCase_			       =  qa["id"]
                    UpperCAmelCase_			       =  [t for t in qa["answers"]["text"] if normalize_answer(snake_case_     )]
                    if not gold_answers:
                         # For unanswerable questions, only correct answer is empty string
                         UpperCAmelCase_			       =  [""]
                    if qid not in preds:
                         print(f"""Missing prediction for {qid}"""     )
                         continue
                    UpperCAmelCase_			       =  preds[qid]
                    # Take max over all gold answers
                    UpperCAmelCase_			       =  max(compute_exact(snake_case_   ,       snake_case_     ) for a in gold_answers     )
                    UpperCAmelCase_			       =  max(compute_fa(snake_case_   ,       snake_case_     ) for a in gold_answers     )
     return exact_scores, fa_scores





def       lowerCAmelCase_							(   snake_case_			:					int   ,       snake_case_			:					Union[str, Any]   ,       snake_case_			:					Union[str, Any]   ,       snake_case_			:					List[str]     )       -> Optional[Any]:


     '''simple docstring'''




     UpperCAmelCase_			       =  {}
     for qid, s in scores.items():
          UpperCAmelCase_			       =  na_probs[qid] > na_prob_thresh
          if pred_na:
               UpperCAmelCase_			       =  float(not qid_to_has_ans[qid]     )
          else:
               UpperCAmelCase_			       =  s
     return new_scores





def       lowerCAmelCase_							(   snake_case_			:					str   ,       snake_case_			:					Any   ,       snake_case_			:					int=None     )       -> List[Any]:


     '''simple docstring'''




     if not qid_list:
          UpperCAmelCase_			       =  len(snake_case_     )
          return collections.OrderedDict(
              [
                  ("exact", 100.0 * sum(exact_scores.values()     ) / total),
                  ("f1", 100.0 * sum(fa_scores.values()     ) / total),
                  ("total", total),
              ]     )
     else:
          UpperCAmelCase_			       =  len(snake_case_     )
          return collections.OrderedDict(
              [
                  ("exact", 100.0 * sum(exact_scores[k] for k in qid_list     ) / total),
                  ("f1", 100.0 * sum(fa_scores[k] for k in qid_list     ) / total),
                  ("total", total),
              ]     )





def       lowerCAmelCase_							(   snake_case_			:					Tuple   ,       snake_case_			:					Tuple   ,       snake_case_			:					str     )       -> List[str]:


     '''simple docstring'''




     for k in new_eval:
          UpperCAmelCase_			       =  new_eval[k]





def       lowerCAmelCase_							(   snake_case_			:					Dict   ,       snake_case_			:					Any   ,       snake_case_			:					Optional[Any]   ,       snake_case_			:					Any     )       -> List[str]:


     '''simple docstring'''




     plt.step(snake_case_   ,       snake_case_   ,       color="b"   ,       alpha=0.2   ,       where="post"     )
     plt.fill_between(snake_case_   ,       snake_case_   ,       step="post"   ,       alpha=0.2   ,       color="b"     )
     plt.xlabel("Recall"     )
     plt.ylabel("Precision"     )
     plt.xlim([0.0, 1.05]     )
     plt.ylim([0.0, 1.05]     )
     plt.title(snake_case_     )
     plt.savefig(snake_case_     )
     plt.clf()





def       lowerCAmelCase_							(   snake_case_			:					Optional[int]   ,       snake_case_			:					Optional[Any]   ,       snake_case_			:					Any   ,       snake_case_			:					str   ,       snake_case_			:					Optional[Any]=None   ,       snake_case_			:					int=None     )       -> Tuple:


     '''simple docstring'''




     UpperCAmelCase_			       =  sorted(snake_case_   ,       key=lambda snake_case_     : na_probs[k]     )
     UpperCAmelCase_			       =  0.0
     UpperCAmelCase_			       =  1.0
     UpperCAmelCase_			       =  0.0
     UpperCAmelCase_			       =  [1.0]
     UpperCAmelCase_			       =  [0.0]
     UpperCAmelCase_			       =  0.0
     for i, qid in enumerate(snake_case_     ):
          if qid_to_has_ans[qid]:
               true_pos += scores[qid]
          UpperCAmelCase_			       =  true_pos / float(i + 1     )
          UpperCAmelCase_			       =  true_pos / float(snake_case_     )
          if i == len(snake_case_     ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
               # i.e., if we can put a threshold after this point
               avg_prec += cur_p * (cur_r - recalls[-1])
               precisions.append(snake_case_     )
               recalls.append(snake_case_     )
     if out_image:
          plot_pr_curve(snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_     )
     return {"ap": 100.0 * avg_prec}





def       lowerCAmelCase_							(   snake_case_			:					int   ,       snake_case_			:					Dict   ,       snake_case_			:					List[Any]   ,       snake_case_			:					List[Any]   ,       snake_case_			:					Any   ,       snake_case_			:					Dict     )       -> Union[str, Any]:


     '''simple docstring'''




     if out_image_dir and not os.path.exists(snake_case_     ):
          os.makedirs(snake_case_     )
     UpperCAmelCase_			       =  sum(1 for v in qid_to_has_ans.values() if v     )
     if num_true_pos == 0:
          return
     UpperCAmelCase_			       =  make_precision_recall_eval(
         snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       out_image=os.path.join(snake_case_   ,       "pr_exact.png"     )   ,       title="Precision-Recall curve for Exact Match score"   ,       )
     UpperCAmelCase_			       =  make_precision_recall_eval(
         snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       out_image=os.path.join(snake_case_   ,       "pr_f1.png"     )   ,       title="Precision-Recall curve for F1 score"   ,       )
     UpperCAmelCase_			       =  {k: float(snake_case_     ) for k, v in qid_to_has_ans.items()}
     UpperCAmelCase_			       =  make_precision_recall_eval(
         snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       out_image=os.path.join(snake_case_   ,       "pr_oracle.png"     )   ,       title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"   ,       )
     merge_eval(snake_case_   ,       snake_case_   ,       "pr_exact"     )
     merge_eval(snake_case_   ,       snake_case_   ,       "pr_f1"     )
     merge_eval(snake_case_   ,       snake_case_   ,       "pr_oracle"     )





def       lowerCAmelCase_							(   snake_case_			:					Dict   ,       snake_case_			:					List[Any]   ,       snake_case_			:					Tuple   ,       snake_case_			:					Union[str, Any]     )       -> List[Any]:


     '''simple docstring'''




     if not qid_list:
          return
     UpperCAmelCase_			       =  [na_probs[k] for k in qid_list]
     UpperCAmelCase_			       =  np.ones_like(snake_case_     ) / float(len(snake_case_     )     )
     plt.hist(snake_case_   ,       weights=snake_case_   ,       bins=20   ,       range=(0.0, 1.0)     )
     plt.xlabel("Model probability of no-answer"     )
     plt.ylabel("Proportion of dataset"     )
     plt.title(f"""Histogram of no-answer probability: {name}"""     )
     plt.savefig(os.path.join(snake_case_   ,       f"""na_prob_hist_{name}.png"""     )     )
     plt.clf()





def       lowerCAmelCase_							(   snake_case_			:					Union[str, Any]   ,       snake_case_			:					Tuple   ,       snake_case_			:					Union[str, Any]   ,       snake_case_			:					List[str]     )       -> Any:


     '''simple docstring'''




     UpperCAmelCase_			       =  sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]     )
     UpperCAmelCase_			       =  num_no_ans
     UpperCAmelCase_			       =  cur_score
     UpperCAmelCase_			       =  0.0
     UpperCAmelCase_			       =  sorted(snake_case_   ,       key=lambda snake_case_     : na_probs[k]     )
     for i, qid in enumerate(snake_case_     ):
          if qid not in scores:
               continue
          if qid_to_has_ans[qid]:
               UpperCAmelCase_			       =  scores[qid]
          else:
               if preds[qid]:
                    UpperCAmelCase_			       =  -1
               else:
                    UpperCAmelCase_			       =  0
          cur_score += diff
          if cur_score > best_score:
               UpperCAmelCase_			       =  cur_score
               UpperCAmelCase_			       =  na_probs[qid]
     return 100.0 * best_score / len(snake_case_     ), best_thresh





def       lowerCAmelCase_							(   snake_case_			:					List[str]   ,       snake_case_			:					Tuple   ,       snake_case_			:					Any   ,       snake_case_			:					Optional[int]   ,       snake_case_			:					Union[str, Any]   ,       snake_case_			:					str     )       -> Tuple:


     '''simple docstring'''




     UpperCAmelCase_       ,					UpperCAmelCase_			       =  find_best_thresh(snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_     )
     UpperCAmelCase_       ,					UpperCAmelCase_			       =  find_best_thresh(snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_     )
     UpperCAmelCase_			       =  best_exact
     UpperCAmelCase_			       =  exact_thresh
     UpperCAmelCase_			       =  best_fa
     UpperCAmelCase_			       =  fa_thresh





def       lowerCAmelCase_							(   )       -> List[str]:


     '''simple docstring'''




     with open(OPTS.data_file     ) as f:
          UpperCAmelCase_			       =  json.load(snake_case_     )
          UpperCAmelCase_			       =  dataset_json["data"]
     with open(OPTS.pred_file     ) as f:
          UpperCAmelCase_			       =  json.load(snake_case_     )
     if OPTS.na_prob_file:
          with open(OPTS.na_prob_file     ) as f:
               UpperCAmelCase_			       =  json.load(snake_case_     )
     else:
          UpperCAmelCase_			       =  {k: 0.0 for k in preds}
     UpperCAmelCase_			       =  make_qid_to_has_ans(snake_case_     )  # maps qid to True/False
     UpperCAmelCase_			       =  [k for k, v in qid_to_has_ans.items() if v]
     UpperCAmelCase_			       =  [k for k, v in qid_to_has_ans.items() if not v]
     UpperCAmelCase_       ,					UpperCAmelCase_			       =  get_raw_scores(snake_case_   ,       snake_case_     )
     UpperCAmelCase_			       =  apply_no_ans_threshold(snake_case_   ,       snake_case_   ,       snake_case_   ,       OPTS.na_prob_thresh     )
     UpperCAmelCase_			       =  apply_no_ans_threshold(snake_case_   ,       snake_case_   ,       snake_case_   ,       OPTS.na_prob_thresh     )
     UpperCAmelCase_			       =  make_eval_dict(snake_case_   ,       snake_case_     )
     if has_ans_qids:
          UpperCAmelCase_			       =  make_eval_dict(snake_case_   ,       snake_case_   ,       qid_list=snake_case_     )
          merge_eval(snake_case_   ,       snake_case_   ,       "HasAns"     )
     if no_ans_qids:
          UpperCAmelCase_			       =  make_eval_dict(snake_case_   ,       snake_case_   ,       qid_list=snake_case_     )
          merge_eval(snake_case_   ,       snake_case_   ,       "NoAns"     )
     if OPTS.na_prob_file:
          find_all_best_thresh(snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_     )
     if OPTS.na_prob_file and OPTS.out_image_dir:
          run_precision_recall_analysis(snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       snake_case_   ,       OPTS.out_image_dir     )
          histogram_na_prob(snake_case_   ,       snake_case_   ,       OPTS.out_image_dir   ,       "hasAns"     )
          histogram_na_prob(snake_case_   ,       snake_case_   ,       OPTS.out_image_dir   ,       "noAns"     )
     if OPTS.out_file:
          with open(OPTS.out_file   ,       "w"     ) as f:
               json.dump(snake_case_   ,       snake_case_     )
     else:
          print(json.dumps(snake_case_   ,       indent=2     )     )


if __name__ == "__main__":
    SCREAMING_SNAKE_CASE_:	List[str]    =parse_args()
    if OPTS.out_image_dir:
        import matplotlib

        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
    main()











'''simple docstring'''






from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('''3.8'''):
      import importlib_metadata
else:
      import importlib.metadata as importlib_metadata


A__							:  Dict    =''''''


if version.parse(importlib_metadata.version('''jiwer''')) < version.parse('''2.3.0'''):




      class 					UpperCAmelCase	(     tr.AbstractTransform  ):




            def __init__(     self     :					Any     ,     __snake_case     :					str = " "    )							-> Any:
                  _lowerCAmelCase					=				sentence_delimiter




            def 							lowercase__       (     self     :					str     ,     __snake_case     :					str    )							-> Optional[int]:
                  return list(__snake_case    )




            def 							lowercase__       (     self     :					Tuple     ,     __snake_case     :					List[str]    )							-> Union[str, Any]:
                  _lowerCAmelCase					=				[]
                  for sent_idx, sentence in enumerate(__snake_case    ):
                        chars.extend(self.process_string(__snake_case    )    )
                        if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(__snake_case    ) - 1:
                              chars.append(self.sentence_delimiter    )
                  return chars




      A__							:  Optional[Any]    =tr.Compose(
          [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
      )
else:
      A__							:  Tuple    =tr.Compose(
          [
              tr.RemoveMultipleSpaces(),
              tr.Strip(),
              tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
              tr.ReduceToListOfListOfChars(),
          ]
      )


A__							:  List[str]    ='''\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
'''

A__							:  Optional[int]    ='''\
Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.

CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.

Character error rate can be computed as:

CER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct characters,
N is the number of characters in the reference (N=S+D+C).

CER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the
performance of the ASR system with a CER of 0 being a perfect score.
'''

A__							:  List[str]    ='''
Computes CER score of transcribed segments against references.
Args:
    references: list of references for each speech input.
    predictions: list of transcribtions to score.
    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
Returns:
    (float): the character error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> cer = datasets.load_metric("cer")
    >>> cer_score = cer.compute(predictions=predictions, references=references)
    >>> print(cer_score)
    0.34146341463414637
'''




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




      def 							lowercase__       (     self     :					Any    )							-> Dict:
            return datasets.MetricInfo(
                description=_DESCRIPTION     ,     citation=_CITATION     ,     inputs_description=_KWARGS_DESCRIPTION     ,     features=datasets.Features(
                    {
                        """predictions""": datasets.Value("""string"""     ,     id="""sequence"""    ),
                        """references""": datasets.Value("""string"""     ,     id="""sequence"""    ),
                    }    )     ,     codebase_urls=["""https://github.com/jitsi/jiwer/"""]     ,     reference_urls=[
                    """https://en.wikipedia.org/wiki/Word_error_rate""",
                    """https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates""",
                ]     ,     )




      def 							lowercase__       (     self     :					Optional[Any]     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					List[str]     ,     __snake_case     :					Tuple=False    )							-> List[Any]:
            if concatenate_texts:
                  return jiwer.compute_measures(
                      __snake_case     ,     __snake_case     ,     truth_transform=__snake_case     ,     hypothesis_transform=__snake_case     ,     )["wer"]

            _lowerCAmelCase					=				0
            _lowerCAmelCase					=				0
            for prediction, reference in zip(__snake_case     ,     __snake_case    ):
                  _lowerCAmelCase					=				jiwer.compute_measures(
                      __snake_case     ,     __snake_case     ,     truth_transform=__snake_case     ,     hypothesis_transform=__snake_case     ,     )
                  incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
                  total += measures["substitutions"] + measures["deletions"] + measures["hits"]

            return incorrect / total






