



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


lowercase_	       =			''''''


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






				class 						__a (     tr.AbstractTransform				):



					def __init__(						self							:    Tuple				,	snake_case_							:    str = " ")->							List[str]:
										__lowerCAmelCase	   =sentence_delimiter



					def 		UpperCamelCase							(						self							:    Union[str, Any]				,	snake_case_							:    str)->							Dict:
										return list(snake_case_)



					def 		UpperCamelCase							(						self							:    Optional[Any]				,	snake_case_							:    List[str])->							Tuple:
										__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


				lowercase_	       =			tr.Compose(
				    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
				)
else:
				lowercase_	       =			tr.Compose(
				    [
				        tr.RemoveMultipleSpaces(),
				        tr.Strip(),
				        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
				        tr.ReduceToListOfListOfChars(),
				    ]
				)


lowercase_	       =			'''\
@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.}
}
'''

lowercase_	       =			'''\
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.
'''

lowercase_	       =			'''
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 						__a (     datasets.Metric				):



	def 		UpperCamelCase							(						self							:    int)->							List[str]:
						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 		UpperCamelCase							(						self							:    Optional[Any]				,	snake_case_							:    List[Any]				,	snake_case_							:    List[Any]				,	snake_case_							:    Union[str, Any]=False)->							Optional[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









import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


lowercase_	       =			pytest.mark.integration






@require_faiss
class 						__a (     SCREAMING_SNAKE_CASE				):



	def 		UpperCamelCase							(						self							:    Optional[int])->							List[Any]:
						__lowerCAmelCase	   =Dataset.from_dict({"""filename""": ["""my_name-train""" + """_""" + str(snake_case_) for x in np.arange(30).tolist()]})
						return dset



	def 		UpperCamelCase							(						self							:    Optional[Any])->							str:
						import faiss

						__lowerCAmelCase	   =self._create_dummy_dataset()
						__lowerCAmelCase	   =dset.map(
						    lambda snake_case_				,	snake_case_: {"vecs": i * np.ones(5				,	dtype=np.floataa)}				,	with_indices=snake_case_				,	keep_in_memory=snake_case_)
						__lowerCAmelCase	   =dset.add_faiss_index("""vecs"""				,	batch_size=1_00				,	metric_type=faiss.METRIC_INNER_PRODUCT)
						__lowerCAmelCase   ,	__lowerCAmelCase	   =dset.get_nearest_examples("""vecs"""				,	np.ones(5				,	dtype=np.floataa))
						self.assertEqual(examples["""filename"""][0]				,	"""my_name-train_29""")
						dset.drop_index("""vecs""")



	def 		UpperCamelCase							(						self							:    Tuple)->							int:
						import faiss

						__lowerCAmelCase	   =self._create_dummy_dataset()
						dset.add_faiss_index_from_external_arrays(
						    external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1				,	1)				,	index_name="""vecs"""				,	batch_size=1_00				,	metric_type=faiss.METRIC_INNER_PRODUCT				,	)
						__lowerCAmelCase   ,	__lowerCAmelCase	   =dset.get_nearest_examples("""vecs"""				,	np.ones(5				,	dtype=np.floataa))
						self.assertEqual(examples["""filename"""][0]				,	"""my_name-train_29""")



	def 		UpperCamelCase							(						self							:    int)->							List[str]:
						import faiss

						__lowerCAmelCase	   =self._create_dummy_dataset()
						dset.add_faiss_index_from_external_arrays(
						    external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1				,	1)				,	index_name="""vecs"""				,	metric_type=faiss.METRIC_INNER_PRODUCT				,	)

						# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
						# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
						# see https://bugs.python.org/issue14243 and
						# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
						with tempfile.NamedTemporaryFile(delete=snake_case_) as tmp_file:
											dset.save_faiss_index("""vecs"""				,	tmp_file.name)
											dset.load_faiss_index("""vecs2"""				,	tmp_file.name)
						os.unlink(tmp_file.name)

						__lowerCAmelCase   ,	__lowerCAmelCase	   =dset.get_nearest_examples("""vecs2"""				,	np.ones(5				,	dtype=np.floataa))
						self.assertEqual(examples["""filename"""][0]				,	"""my_name-train_29""")



	def 		UpperCamelCase							(						self							:    Any)->							Optional[int]:
						__lowerCAmelCase	   =self._create_dummy_dataset()
						dset.add_faiss_index_from_external_arrays(
						    external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1				,	1)				,	index_name="""vecs""")
						dset.drop_index("""vecs""")
						self.assertRaises(snake_case_				,	partial(dset.get_nearest_examples				,	"""vecs2"""				,	np.ones(5				,	dtype=np.floataa)))



	def 		UpperCamelCase							(						self							:    Optional[int])->							List[str]:
						from elasticsearch import Elasticsearch

						__lowerCAmelCase	   =self._create_dummy_dataset()
						with patch("""elasticsearch.Elasticsearch.search""") as mocked_search, patch(
						    """elasticsearch.client.IndicesClient.create""") as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""") as mocked_bulk:
											__lowerCAmelCase	   ={"""acknowledged""": True}
											mocked_bulk.return_value([(True, None)] * 30)
											__lowerCAmelCase	   ={"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 29}]}}
											__lowerCAmelCase	   =Elasticsearch()

											dset.add_elasticsearch_index("""filename"""				,	es_client=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =dset.get_nearest_examples("""filename"""				,	"""my_name-train_29""")
											self.assertEqual(examples["""filename"""][0]				,	"""my_name-train_29""")






@require_faiss
class 						__a (     SCREAMING_SNAKE_CASE				):



	def 		UpperCamelCase							(						self							:    Dict)->							int:
						import faiss

						__lowerCAmelCase	   =FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT)

						# add vectors
						index.add_vectors(np.eye(5				,	dtype=np.floataa))
						self.assertIsNotNone(index.faiss_index)
						self.assertEqual(index.faiss_index.ntotal				,	5)
						index.add_vectors(np.zeros((5, 5)				,	dtype=np.floataa))
						self.assertEqual(index.faiss_index.ntotal				,	10)

						# single query
						__lowerCAmelCase	   =np.zeros(5				,	dtype=np.floataa)
						__lowerCAmelCase	   =1
						__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search(snake_case_)
						self.assertRaises(snake_case_				,	index.search				,	query.reshape(-1				,	1))
						self.assertGreater(scores[0]				,	0)
						self.assertEqual(indices[0]				,	1)

						# batched queries
						__lowerCAmelCase	   =np.eye(5				,	dtype=np.floataa)[::-1]
						__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search_batch(snake_case_)
						self.assertRaises(snake_case_				,	index.search_batch				,	queries[0])
						__lowerCAmelCase	   =[scores[0] for scores in total_scores]
						__lowerCAmelCase	   =[indices[0] for indices in total_indices]
						self.assertGreater(np.min(snake_case_)				,	0)
						self.assertListEqual([4, 3, 2, 1, 0]				,	snake_case_)



	def 		UpperCamelCase							(						self							:    Optional[Any])->							str:
						import faiss

						__lowerCAmelCase	   =FaissIndex(string_factory="""Flat""")
						index.add_vectors(np.eye(5				,	dtype=np.floataa))
						self.assertIsInstance(index.faiss_index				,	faiss.IndexFlat)
						__lowerCAmelCase	   =FaissIndex(string_factory="""LSH""")
						index.add_vectors(np.eye(5				,	dtype=np.floataa))
						self.assertIsInstance(index.faiss_index				,	faiss.IndexLSH)
						with self.assertRaises(snake_case_):
											__lowerCAmelCase	   =FaissIndex(string_factory="""Flat"""				,	custom_index=faiss.IndexFlat(5))



	def 		UpperCamelCase							(						self							:    List[str])->							Any:
						import faiss

						__lowerCAmelCase	   =faiss.IndexFlat(5)
						__lowerCAmelCase	   =FaissIndex(custom_index=snake_case_)
						index.add_vectors(np.eye(5				,	dtype=np.floataa))
						self.assertIsInstance(index.faiss_index				,	faiss.IndexFlat)



	def 		UpperCamelCase							(						self							:    str)->							Union[str, Any]:
						import faiss

						__lowerCAmelCase	   =FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT)
						index.add_vectors(np.eye(5				,	dtype=np.floataa))

						# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
						# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
						# see https://bugs.python.org/issue14243 and
						# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
						with tempfile.NamedTemporaryFile(delete=snake_case_) as tmp_file:
											index.save(tmp_file.name)
											__lowerCAmelCase	   =FaissIndex.load(tmp_file.name)
						os.unlink(tmp_file.name)

						__lowerCAmelCase	   =np.zeros(5				,	dtype=np.floataa)
						__lowerCAmelCase	   =1
						__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search(snake_case_)
						self.assertGreater(scores[0]				,	0)
						self.assertEqual(indices[0]				,	1)






@require_faiss
def 					__lowerCAmelCase		(  __lowerCamelCase	:    Optional[Any]	)  ->						List[str]:
					import faiss

					__lowerCAmelCase	   =FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT	)
					index.add_vectors(np.eye(5				,   dtype=np.floataa	)	)

					__lowerCAmelCase	   ="""index.faiss"""
					__lowerCAmelCase	   =f"""mock://{index_name}"""
					index.save(__lowerCamelCase				,   storage_options=mockfs.storage_options	)
					__lowerCAmelCase	   =FaissIndex.load(__lowerCamelCase				,   storage_options=mockfs.storage_options	)

					__lowerCAmelCase	   =np.zeros(5				,   dtype=np.floataa	)
					__lowerCAmelCase	   =1
					__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search(__lowerCamelCase	)
					assert scores[0] > 0
					assert indices[0] == 1






@require_elasticsearch
class 						__a (     SCREAMING_SNAKE_CASE				):



	def 		UpperCamelCase							(						self							:    List[str])->							Optional[int]:
						from elasticsearch import Elasticsearch

						with patch("""elasticsearch.Elasticsearch.search""") as mocked_search, patch(
						    """elasticsearch.client.IndicesClient.create""") as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""") as mocked_bulk:
											__lowerCAmelCase	   =Elasticsearch()
											__lowerCAmelCase	   ={"""acknowledged""": True}
											__lowerCAmelCase	   =ElasticSearchIndex(es_client=snake_case_)
											mocked_bulk.return_value([(True, None)] * 3)
											index.add_documents(["""foo""", """bar""", """foobar"""])

											# single query
											__lowerCAmelCase	   ="""foo"""
											__lowerCAmelCase	   ={"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}}
											__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search(snake_case_)
											self.assertEqual(scores[0]				,	1)
											self.assertEqual(indices[0]				,	0)

											# single query with timeout
											__lowerCAmelCase	   ="""foo"""
											__lowerCAmelCase	   ={"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}}
											__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search(snake_case_				,	request_timeout=30)
											self.assertEqual(scores[0]				,	1)
											self.assertEqual(indices[0]				,	0)

											# batched queries
											__lowerCAmelCase	   =["""foo""", """bar""", """foobar"""]
											__lowerCAmelCase	   ={"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}}
											__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search_batch(snake_case_)
											__lowerCAmelCase	   =[scores[0] for scores in total_scores]
											__lowerCAmelCase	   =[indices[0] for indices in total_indices]
											self.assertGreater(np.min(snake_case_)				,	0)
											self.assertListEqual([1, 1, 1]				,	snake_case_)

											# batched queries with timeout
											__lowerCAmelCase	   =["""foo""", """bar""", """foobar"""]
											__lowerCAmelCase	   ={"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}}
											__lowerCAmelCase   ,	__lowerCAmelCase	   =index.search_batch(snake_case_				,	request_timeout=30)
											__lowerCAmelCase	   =[scores[0] for scores in total_scores]
											__lowerCAmelCase	   =[indices[0] for indices in total_indices]
											self.assertGreater(np.min(snake_case_)				,	0)
											self.assertListEqual([1, 1, 1]				,	snake_case_)




