





"""simple docstring"""




import json
import os
import re
import unicodedata
from json.encoder import INFINITY
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import regex

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_flax_available, is_tf_available, is_torch_available, logging
from ...utils.generic import _is_jax, _is_numpy


_SCREAMING_SNAKE_CASE 		=						logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE 		=						{
    'artists_file': 'artists.json',
    'lyrics_file': 'lyrics.json',
    'genres_file': 'genres.json',
}

_SCREAMING_SNAKE_CASE 		=						{
    'artists_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/artists.json',
    },
    'genres_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/genres.json',
    },
    'lyrics_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/lyrics.json',
    },
}

_SCREAMING_SNAKE_CASE 		=						{
    'jukebox': 5_12,
}
class SCREAMING_SNAKE_CASE_  (   lowerCAmelCase__   ):
						__magic_name__:     Any	 =    VOCAB_FILES_NAMES
						__magic_name__:     int	 =    PRETRAINED_VOCAB_FILES_MAP
						__magic_name__:     Optional[int]	 =    PRETRAINED_LYRIC_TOKENS_SIZES
						__magic_name__:     Optional[Any]	 =    ["input_ids", "attention_mask"]






						def __init__( self : Union[str, Any]						,				_A : Dict						,				_A : Tuple						,				_A : Any						,				_A : Optional[Any]=["v3", "v2", "v2"]						,				_A : Dict=512						,				_A : List[Any]=5						,				_A : Dict="<|endoftext|>"						,				**_A : Union[str, Any]						,				)							->      Optional[int]:







							"""simple docstring"""


							snake_case_						:     Dict     =		AddedToken(_SCREAMING_SNAKE_CASE						,				lstrip=_SCREAMING_SNAKE_CASE						,				rstrip=_SCREAMING_SNAKE_CASE		) if isinstance(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE		) else unk_token
							super().__init__(
							    unk_token=_SCREAMING_SNAKE_CASE						,				n_genres=_SCREAMING_SNAKE_CASE						,				version=_SCREAMING_SNAKE_CASE						,				max_n_lyric_tokens=_SCREAMING_SNAKE_CASE						,				**_SCREAMING_SNAKE_CASE						,				)
							snake_case_						:     str     =		version
							snake_case_						:     Optional[int]     =		max_n_lyric_tokens
							snake_case_						:     int     =		n_genres

							with open(_SCREAMING_SNAKE_CASE						,				encoding='utf-8'		) as vocab_handle:
								snake_case_						:     int     =		json.load(_SCREAMING_SNAKE_CASE		)

							with open(_SCREAMING_SNAKE_CASE						,				encoding='utf-8'		) as vocab_handle:
								snake_case_						:     Any     =		json.load(_SCREAMING_SNAKE_CASE		)

							with open(_SCREAMING_SNAKE_CASE						,				encoding='utf-8'		) as vocab_handle:
								snake_case_						:     Any     =		json.load(_SCREAMING_SNAKE_CASE		)

							snake_case_						:     Union[str, Any]     =		R'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+'
							# In v2, we had a n_vocab=80 and in v3 we missed + and so n_vocab=79 of characters.
							if len(self.lyrics_encoder		) == 79:
								snake_case_						:     List[Any]     =		oov.replace(R'\-\''						,				R'\-+\''		)

							snake_case_						:     int     =		regex.compile(_SCREAMING_SNAKE_CASE		)
							snake_case_						:     str     =		{v: k for k, v in self.artists_encoder.items()}
							snake_case_						:     Optional[int]     =		{v: k for k, v in self.genres_encoder.items()}
							snake_case_						:     int     =		{v: k for k, v in self.lyrics_encoder.items()}






						@property
						def UpperCAmelCase_			( self : str		)							->      Union[str, Any]:







							"""simple docstring"""


							return len(self.artists_encoder		) + len(self.genres_encoder		) + len(self.lyrics_encoder		)






						def UpperCAmelCase_			( self : int		)							->      int:







							"""simple docstring"""


							return dict(self.artists_encoder						,				self.genres_encoder						,				self.lyrics_encoder		)






						def UpperCAmelCase_			( self : Any						,				_A : Union[str, Any]						,				_A : Dict						,				_A : Any		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		[self.artists_encoder.get(_SCREAMING_SNAKE_CASE						,				0		) for artist in list_artists]
							for genres in range(len(_SCREAMING_SNAKE_CASE		)		):
								snake_case_						:     int     =		[self.genres_encoder.get(_SCREAMING_SNAKE_CASE						,				0		) for genre in list_genres[genres]]
								snake_case_						:     int     =		list_genres[genres] + [-1] * (self.n_genres - len(list_genres[genres]		))

							snake_case_						:     Union[str, Any]     =		[[self.lyrics_encoder.get(_SCREAMING_SNAKE_CASE						,				0		) for character in list_lyrics[0]], [], []]
							return artists_id, list_genres, lyric_ids






						def UpperCAmelCase_			( self : Dict						,				_A : str		)							->      List[Any]:







							"""simple docstring"""


							return list(_SCREAMING_SNAKE_CASE		)






						def UpperCAmelCase_			( self : List[str]						,				_A : Optional[int]						,				_A : Dict						,				_A : Optional[Any]						,				**_A : List[Any]		)							->      Tuple:







							"""simple docstring"""


							snake_case_  ,snake_case_  ,snake_case_						:     str     =		self.prepare_for_tokenization(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE		)
							snake_case_						:     List[str]     =		self._tokenize(_SCREAMING_SNAKE_CASE		)
							return artist, genre, lyrics






						def UpperCAmelCase_			( self : List[Any]						,				_A : Optional[int]						,				_A : Union[str, Any]						,				_A : Tuple						,				_A : List[Any] = False		)							->      Tuple[str, str, str, Dict[str, Any]]:







							"""simple docstring"""


							for idx in range(len(self.version		)		):
								if self.version[idx] == "v3":
									snake_case_						:     Optional[int]     =		artists[idx].lower()
									snake_case_						:     Tuple     =		[genres[idx].lower()]
								else:
									snake_case_						:     Tuple     =		self._normalize(artists[idx]		) + '.v2'
									snake_case_						:     Union[str, Any]     =		[
									    self._normalize(_SCREAMING_SNAKE_CASE		) + '.v2' for genre in genres[idx].split('_'		)
									]  # split is for the full dictionary with combined genres

							if self.version[0] == "v2":
								snake_case_						:     List[str]     =		regex.compile(R'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+'		)
								snake_case_						:     Any     =		'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;!?-+\'\"()[] \t\n'
								snake_case_						:     Any     =		{vocab[index]: index + 1 for index in range(len(_SCREAMING_SNAKE_CASE		)		)}
								snake_case_						:     str     =		0
								snake_case_						:     Dict     =		len(_SCREAMING_SNAKE_CASE		) + 1
								snake_case_						:     Any     =		self.vocab
								snake_case_						:     Dict     =		{v: k for k, v in self.vocab.items()}
								snake_case_						:     Any     =		''
							else:
								snake_case_						:     Tuple     =		regex.compile(R'[^A-Za-z0-9.,:;!?\-+\'\"()\[\] \t\n]+'		)

							snake_case_						:     Optional[int]     =		self._run_strip_accents(_SCREAMING_SNAKE_CASE		)
							snake_case_						:     Union[str, Any]     =		lyrics.replace('\\'						,				'\n'		)
							snake_case_						:     int     =		self.out_of_vocab.sub(''						,				_SCREAMING_SNAKE_CASE		), [], []
							return artists, genres, lyrics






						def UpperCAmelCase_			( self : Dict						,				_A : List[Any]		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		unicodedata.normalize('NFD'						,				_SCREAMING_SNAKE_CASE		)
							snake_case_						:     Union[str, Any]     =		[]
							for char in text:
								snake_case_						:     Tuple     =		unicodedata.category(_SCREAMING_SNAKE_CASE		)
								if cat == "Mn":
									continue
								output.append(_SCREAMING_SNAKE_CASE		)
							return "".join(_SCREAMING_SNAKE_CASE		)






						def UpperCAmelCase_			( self : List[Any]						,				_A : Optional[int]		)							->      str:







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		(
							    [chr(_SCREAMING_SNAKE_CASE		) for i in range(ord('a'		)						,				ord('z'		) + 1		)]
							    + [chr(_SCREAMING_SNAKE_CASE		) for i in range(ord('A'		)						,				ord('Z'		) + 1		)]
							    + [chr(_SCREAMING_SNAKE_CASE		) for i in range(ord('0'		)						,				ord('9'		) + 1		)]
							    + ['.']
							)
							snake_case_						:     Tuple     =		frozenset(_SCREAMING_SNAKE_CASE		)
							snake_case_						:     Tuple     =		re.compile(R'_+'		)
							snake_case_						:     str     =		''.join([c if c in accepted else '_' for c in text.lower()]		)
							snake_case_						:     Dict     =		pattern.sub('_'						,				_SCREAMING_SNAKE_CASE		).strip('_'		)
							return text






						def UpperCAmelCase_			( self : Union[str, Any]						,				_A : Optional[Any]		)							->      str:







							"""simple docstring"""


							return " ".join(_SCREAMING_SNAKE_CASE		)






						def UpperCAmelCase_			( self : str						,				_A : Optional[int]						,				_A : Optional[Any] = None						,				_A : Tuple = False		)							->      Union[str, Any]:







							"""simple docstring"""


							if not isinstance(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE		):
								snake_case_						:     Union[str, Any]     =		TensorType(_SCREAMING_SNAKE_CASE		)

							# Get a function reference for the correct framework
							if tensor_type == TensorType.TENSORFLOW:
								if not is_tf_available():
									raise ImportError(
									    'Unable to convert output to TensorFlow tensors format, TensorFlow is not installed.'		)
								import tensorflow as tf

								snake_case_						:     Dict     =		tf.constant
								snake_case_						:     str     =		tf.is_tensor
							elif tensor_type == TensorType.PYTORCH:
								if not is_torch_available():
									raise ImportError('Unable to convert output to PyTorch tensors format, PyTorch is not installed.'		)
								import torch

								snake_case_						:     List[str]     =		torch.tensor
								snake_case_						:     int     =		torch.is_tensor
							elif tensor_type == TensorType.JAX:
								if not is_flax_available():
									raise ImportError('Unable to convert output to JAX tensors format, JAX is not installed.'		)
								import jax.numpy as jnp  # noqa: F811

								snake_case_						:     Union[str, Any]     =		jnp.array
								snake_case_						:     Union[str, Any]     =		_is_jax
							else:
								snake_case_						:     List[Any]     =		np.asarray
								snake_case_						:     Optional[int]     =		_is_numpy

							# Do the tensor conversion in batch

							try:
								if prepend_batch_axis:
									snake_case_						:     Dict     =		[inputs]

								if not is_tensor(_SCREAMING_SNAKE_CASE		):
									snake_case_						:     int     =		as_tensor(_SCREAMING_SNAKE_CASE		)
							except:  # noqa E722
								raise ValueError(
								    'Unable to create tensor, you should probably activate truncation and/or padding '
								    'with \'padding=True\' \'truncation=True\' to have batched tensors with the same length.'		)

							return inputs






						def __call__( self : Dict						,				_A : Union[str, Any]						,				_A : Any						,				_A : str=""						,				_A : Tuple="pt"		)							->      BatchEncoding:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		[0, 0, 0]
							snake_case_						:     str     =		[artist] * len(self.version		)
							snake_case_						:     Tuple     =		[genres] * len(self.version		)

							snake_case_  ,snake_case_  ,snake_case_						:     int     =		self.tokenize(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE		)
							snake_case_  ,snake_case_  ,snake_case_						:     Union[str, Any]     =		self._convert_token_to_id(_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE		)

							snake_case_						:     Any     =		[-INFINITY] * len(full_tokens[-1]		)
							snake_case_						:     int     =		[
							    self.convert_to_tensors(
							        [input_ids + [artists_id[i]] + genres_ids[i] + full_tokens[i]]						,				tensor_type=_SCREAMING_SNAKE_CASE		)
							    for i in range(len(self.version		)		)
							]
							return BatchEncoding({'input_ids': input_ids, 'attention_masks': attention_masks}		)






						def UpperCAmelCase_			( self : int						,				_A : Dict						,				_A : Any = None		)							->      Tuple[str]:







							"""simple docstring"""


							if not os.path.isdir(_SCREAMING_SNAKE_CASE		):
								logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""		)
								return

							snake_case_						:     List[str]     =		os.path.join(
							    _SCREAMING_SNAKE_CASE						,				(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['artists_file']		)
							with open(_SCREAMING_SNAKE_CASE						,				'w'						,				encoding='utf-8'		) as f:
								f.write(json.dumps(self.artists_encoder						,				ensure_ascii=_SCREAMING_SNAKE_CASE		)		)

							snake_case_						:     str     =		os.path.join(
							    _SCREAMING_SNAKE_CASE						,				(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['genres_file']		)
							with open(_SCREAMING_SNAKE_CASE						,				'w'						,				encoding='utf-8'		) as f:
								f.write(json.dumps(self.genres_encoder						,				ensure_ascii=_SCREAMING_SNAKE_CASE		)		)

							snake_case_						:     List[Any]     =		os.path.join(
							    _SCREAMING_SNAKE_CASE						,				(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['lyrics_file']		)
							with open(_SCREAMING_SNAKE_CASE						,				'w'						,				encoding='utf-8'		) as f:
								f.write(json.dumps(self.lyrics_encoder						,				ensure_ascii=_SCREAMING_SNAKE_CASE		)		)

							return (artists_file, genres_file, lyrics_file)






						def UpperCAmelCase_			( self : Optional[Any]						,				_A : List[str]						,				_A : Dict						,				_A : Dict		)							->      List[str]:







							"""simple docstring"""


							snake_case_						:     List[Any]     =		self.artists_decoder.get(_SCREAMING_SNAKE_CASE		)
							snake_case_						:     Any     =		[self.genres_decoder.get(_SCREAMING_SNAKE_CASE		) for genre in genres_index]
							snake_case_						:     List[Any]     =		[self.lyrics_decoder.get(_SCREAMING_SNAKE_CASE		) for character in lyric_index]
							return artist, genres, lyrics








import re
import string

import numpy as np

import datasets


_SCREAMING_SNAKE_CASE 		=						"""
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
"""

_SCREAMING_SNAKE_CASE 		=						"""
Args:
    predictions: List of predicted texts.
    references: List of reference texts.
    regexes_to_ignore: List, defaults to None. Regex expressions of characters to
        ignore when calculating the exact matches. Note: these regexes are removed
        from the input data before the changes based on the options below (e.g. ignore_case,
        ignore_punctuation, ignore_numbers) are applied.
    ignore_case: Boolean, defaults to False. If true, turns everything
        to lowercase so that capitalization differences are ignored.
    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
Returns:
    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.
Examples:
    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results[\"exact_match\"], 1))
    25.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results[\"exact_match\"], 1))
    50.0


    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results[\"exact_match\"], 1))
    75.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)
    >>> print(round(results[\"exact_match\"], 1))
    100.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]
    >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results[\"exact_match\"], 1))
    33.3

"""

_SCREAMING_SNAKE_CASE 		=						"""
"""
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    ,			_KWARGS_DESCRIPTION   )
class SCREAMING_SNAKE_CASE_  (   datasets.Metric   ):






						def UpperCAmelCase_			( self : Union[str, Any]		)							->      Optional[Any]:







							"""simple docstring"""


							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'		),
							        }		)						,				reference_urls=[]						,				)






						def UpperCAmelCase_			( self : List[Any]						,				_A : Optional[Any]						,				_A : Optional[int]						,				_A : Optional[int]=None						,				_A : Dict=False						,				_A : Dict=False						,				_A : Optional[Any]=False						,				)							->      List[str]:







							"""simple docstring"""


							if regexes_to_ignore is not None:
								for s in regexes_to_ignore:
									snake_case_						:     List[str]     =		np.array([re.sub(_A						,				''						,				_A		) for x in predictions]		)
									snake_case_						:     int     =		np.array([re.sub(_A						,				''						,				_A		) for x in references]		)
							else:
								snake_case_						:     Optional[Any]     =		np.asarray(_A		)
								snake_case_						:     Optional[Any]     =		np.asarray(_A		)

							if ignore_case:
								snake_case_						:     int     =		np.char.lower(_A		)
								snake_case_						:     List[str]     =		np.char.lower(_A		)

							if ignore_punctuation:
								snake_case_						:     str     =		string.punctuation.maketrans(''						,				''						,				string.punctuation		)
								snake_case_						:     str     =		np.char.translate(_A						,				table=_A		)
								snake_case_						:     Any     =		np.char.translate(_A						,				table=_A		)

							if ignore_numbers:
								snake_case_						:     int     =		string.digits.maketrans(''						,				''						,				string.digits		)
								snake_case_						:     Tuple     =		np.char.translate(_A						,				table=_A		)
								snake_case_						:     Optional[Any]     =		np.char.translate(_A						,				table=_A		)

							snake_case_						:     Optional[Any]     =		predictions == references

							return {"exact_match": np.mean(_A		) * 100}

