


from abc import ABC, abstractmethod
from typing import List, Optional







class  snake_case_     (lowerCamelCase_   ):
					def __init__(   self     :int   )      ->					Dict:
												# test for the above condition
												self.test()
					def 				lowerCamelCase__(   self     :int   )      ->					Optional[Any]:
												a__    =  0
												a__    =  False
												while not completed:
																			if counter == 1:
																										self.reset()
																			a__    =  self.advance()
																			if not self.does_advance(__snake_case   ):
																										raise Exception(
																										    'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'   )

																			a__   ,      a__   ,      a__    =  self.update(__snake_case   )
																			counter += 1

																			if counter > 1_00_00:
																										raise Exception('update() does not fulfill the constraint.'   )

												if self.remaining() != 0:
																			raise Exception('Custom Constraint is not defined correctly.'   )
					@abstractmethod
					def 				lowerCamelCase__(   self     :List[Any]   )      ->					Tuple:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )
					@abstractmethod
					def 				lowerCamelCase__(   self     :Optional[Any]    ,__snake_case     :List[str]   )      ->					int:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )
					@abstractmethod
					def 				lowerCamelCase__(   self     :Optional[int]    ,__snake_case     :List[str]   )      ->					List[Any]:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )
					@abstractmethod
					def 				lowerCamelCase__(   self     :List[str]   )      ->					str:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )
					@abstractmethod
					def 				lowerCamelCase__(   self     :Any   )      ->					Dict:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )



					@abstractmethod
					def 				lowerCamelCase__(   self     :List[str]    ,__snake_case     :Tuple=False   )      ->					Optional[Any]:
												raise NotImplementedError(
												    F'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'   )







class  snake_case_     (lowerCamelCase_   ):
					def __init__(   self     :int    ,__snake_case     :Union[str, Any]   )      ->					Any:
												super(__snake_case    ,self   ).__init__()

												if not isinstance(__snake_case    ,__snake_case   ) or len(__snake_case   ) == 0:
																			raise ValueError(F'`token_ids` has to be a non-empty list, but is {token_ids}.'   )
												if any((not isinstance(__snake_case    ,__snake_case   ) or token_id < 0) for token_id in token_ids   ):
																			raise ValueError(F'Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'   )

												a__    =  token_ids

												a__    =  len(self.token_ids   )
												a__    =  -1  # the index of the currently fulfilled step
												a__    =  False
					def 				lowerCamelCase__(   self     :int   )      ->					Union[str, Any]:
												if self.completed:
																			return None
												return self.token_ids[self.fulfilled_idx + 1]
					def 				lowerCamelCase__(   self     :int    ,__snake_case     :Optional[int]   )      ->					int:
												if not isinstance(__snake_case    ,__snake_case   ):
																			raise ValueError(F'`token_id` has to be an `int`, but is {token_id} of type {type(__snake_case   )}'   )

												if self.completed:
																			return False

												return token_id == self.token_ids[self.fulfilled_idx + 1]
					def 				lowerCamelCase__(   self     :List[Any]    ,__snake_case     :str   )      ->					Any:
												if not isinstance(__snake_case    ,__snake_case   ):
																			raise ValueError(F'`token_id` has to be an `int`, but is {token_id} of type {type(__snake_case   )}'   )

												a__    =  False
												a__    =  False
												a__    =  False

												if self.does_advance(__snake_case   ):
																			self.fulfilled_idx += 1
																			a__    =  True
																			if self.fulfilled_idx == (self.seqlen - 1):
																										a__    =  True
																			a__    =  completed
												else:
																			# failed to make progress.
																			a__    =  True
																			self.reset()
												return stepped, completed, reset
					def 				lowerCamelCase__(   self     :int   )      ->					Union[str, Any]:
												a__    =  False
												a__    =  0
					def 				lowerCamelCase__(   self     :List[str]   )      ->					Union[str, Any]:
												return self.seqlen - (self.fulfilled_idx + 1)



					def 				lowerCamelCase__(   self     :Union[str, Any]    ,__snake_case     :List[str]=False   )      ->					List[str]:
												a__    =  PhrasalConstraint(self.token_ids   )

												if stateful:
																			a__    =  self.seqlen
																			a__    =  self.fulfilled_idx
																			a__    =  self.completed

												return new_constraint







class  snake_case_     :
					def __init__(   self     :int    ,__snake_case     :Optional[Any]    ,__snake_case     :List[Any]=True   )      ->					List[Any]:
												a__    =  max([len(__snake_case   ) for one in nested_token_ids]   )

												a__    =  {}
												for token_ids in nested_token_ids:
																			a__    =  root
																			for tidx, token_id in enumerate(__snake_case   ):
																										if token_id not in level:
																																	a__    =  {}

																										a__    =  level[token_id]

												if no_subsets and self.has_subsets(__snake_case    ,__snake_case   ):
																			raise ValueError(
																			    'Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'
																			    F' {nested_token_ids}.'   )

												a__    =  root
					def 				lowerCamelCase__(   self     :Tuple    ,__snake_case     :List[Any]   )      ->					Optional[int]:
												a__    =  self.trie

												for current_token in current_seq:
																			a__    =  start[current_token]

												a__    =  list(start.keys()   )

												return next_tokens
					def 				lowerCamelCase__(   self     :Optional[Any]    ,__snake_case     :List[Any]   )      ->					Optional[int]:
												a__    =  self.next_tokens(__snake_case   )

												return len(__snake_case   ) == 0
					def 				lowerCamelCase__(   self     :List[str]    ,__snake_case     :Any   )      ->					Union[str, Any]:
												a__    =  list(root.values()   )
												if len(__snake_case   ) == 0:
																			return 1
												else:
																			return sum([self.count_leaves(__snake_case   ) for nn in next_nodes]   )



					def 				lowerCamelCase__(   self     :Tuple    ,__snake_case     :List[Any]    ,__snake_case     :str   )      ->					str:
												a__    =  self.count_leaves(__snake_case   )
												return len(__snake_case   ) != leaf_count







class  snake_case_     (lowerCamelCase_   ):
					def __init__(   self     :List[str]    ,__snake_case     :Optional[Any]   )      ->					List[str]:
												super(__snake_case    ,self   ).__init__()

												if not isinstance(__snake_case    ,__snake_case   ) or len(__snake_case   ) == 0:
																			raise ValueError(F'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'   )
												if any(not isinstance(__snake_case    ,__snake_case   ) for token_ids in nested_token_ids   ):
																			raise ValueError(F'`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'   )
												if any(
												    any((not isinstance(__snake_case    ,__snake_case   ) or token_id < 0) for token_id in token_ids   )
												    for token_ids in nested_token_ids   ):
																			raise ValueError(
																			    F'Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'   )

												a__    =  DisjunctiveTrie(__snake_case   )
												a__    =  nested_token_ids

												a__    =  self.trie.max_height
												a__    =  []
												a__    =  False
					def 				lowerCamelCase__(   self     :Dict   )      ->					List[Any]:
												a__    =  self.trie.next_tokens(self.current_seq   )

												if len(__snake_case   ) == 0:
																			return None
												else:
																			return token_list
					def 				lowerCamelCase__(   self     :str    ,__snake_case     :Dict   )      ->					int:
												if not isinstance(__snake_case    ,__snake_case   ):
																			raise ValueError(F'`token_id` is supposed to be type `int`, but is {token_id} of type {type(__snake_case   )}'   )

												a__    =  self.trie.next_tokens(self.current_seq   )

												return token_id in next_tokens
					def 				lowerCamelCase__(   self     :Any    ,__snake_case     :Optional[int]   )      ->					str:
												if not isinstance(__snake_case    ,__snake_case   ):
																			raise ValueError(F'`token_id` is supposed to be type `int`, but is {token_id} of type {type(__snake_case   )}'   )

												a__    =  False
												a__    =  False
												a__    =  False

												if self.does_advance(__snake_case   ):
																			self.current_seq.append(__snake_case   )
																			a__    =  True
												else:
																			a__    =  True
																			self.reset()

												a__    =  self.trie.reached_leaf(self.current_seq   )
												a__    =  completed

												return stepped, completed, reset
					def 				lowerCamelCase__(   self     :Dict   )      ->					int:
												a__    =  False
												a__    =  []
					def 				lowerCamelCase__(   self     :Union[str, Any]   )      ->					Union[str, Any]:
												if self.completed:
																			# since this can be completed without reaching max height
																			return 0
												else:
																			return self.seqlen - len(self.current_seq   )



					def 				lowerCamelCase__(   self     :Any    ,__snake_case     :int=False   )      ->					List[Any]:
												a__    =  DisjunctiveConstraint(self.token_ids   )

												if stateful:
																			a__    =  self.seqlen
																			a__    =  self.current_seq
																			a__    =  self.completed

												return new_constraint







class  snake_case_     :
					def __init__(   self     :Tuple    ,__snake_case     :int   )      ->					Optional[Any]:
												a__    =  constraints

												# max # of steps required to fulfill a given constraint
												a__    =  max([c.seqlen for c in constraints]   )
												a__    =  len(__snake_case   )
												a__    =  False

												self.init_state()
					def 				lowerCamelCase__(   self     :Union[str, Any]   )      ->					Any:
												a__    =  []
												a__    =  None
												a__    =  [constraint.copy(stateful=__snake_case   ) for constraint in self.constraints]
					def 				lowerCamelCase__(   self     :str   )      ->					Any:
												a__    =  0
												if self.inprogress_constraint:
																			# extra points for having a constraint mid-fulfilled
																			add += self.max_seqlen - self.inprogress_constraint.remaining()

												return (len(self.complete_constraints   ) * self.max_seqlen) + add
					def 				lowerCamelCase__(   self     :Dict   )      ->					Optional[Any]:
												a__    =  []
												if self.inprogress_constraint is None:
																			for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																										a__    =  constraint.advance()
																										if isinstance(__snake_case    ,__snake_case   ):
																																	token_list.append(__snake_case   )
																										elif isinstance(__snake_case    ,__snake_case   ):
																																	token_list.extend(__snake_case   )
												else:
																			a__    =  self.inprogress_constraint.advance()
																			if isinstance(__snake_case    ,__snake_case   ):
																										token_list.append(__snake_case   )
																			elif isinstance(__snake_case    ,__snake_case   ):
																										token_list.extend(__snake_case   )

												if len(__snake_case   ) == 0:
																			return None
												else:
																			return token_list
					def 				lowerCamelCase__(   self     :Any    ,__snake_case     :Any   )      ->					int:
												self.init_state()

												if token_ids is not None:
																			for token in token_ids:
																										# completes or steps **one** constraint
																										a__   ,      a__    =  self.add(__snake_case   )

																										# the entire list of constraints are fulfilled
																										if self.completed:
																																	break
					def 				lowerCamelCase__(   self     :Any    ,__snake_case     :int   )      ->					str:
												if not isinstance(__snake_case    ,__snake_case   ):
																			raise ValueError(F'`token_id` should be an `int`, but is `{token_id}`.'   )

												a__   ,      a__    =  False, False

												if self.completed:
																			a__    =  True
																			a__    =  False
																			return complete, stepped

												if self.inprogress_constraint is not None:
																			# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
																			# job, simply update the state

																			a__   ,      a__   ,      a__    =  self.inprogress_constraint.update(__snake_case   )
																			if reset:
																										# 1. If the next token breaks the progress, then we must restart.
																										#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																										#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																										#     constraint, not the full list of constraints.

																										self.pending_constraints.append(self.inprogress_constraint.copy(stateful=__snake_case   )   )
																										a__    =  None

																			if complete:
																										# 2. If the next token completes the constraint, move it to completed list, set
																										#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																										#     is complete.

																										self.complete_constraints.append(self.inprogress_constraint   )
																										a__    =  None

																										if len(self.pending_constraints   ) == 0:
																																	# we're done!
																																	a__    =  True

												else:
																			# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
																			# of constraints?

																			for cidx, pending_constraint in enumerate(self.pending_constraints   ):
																										if pending_constraint.does_advance(__snake_case   ):
																																	a__   ,      a__   ,      a__    =  pending_constraint.update(__snake_case   )

																																	if not stepped:
																																								raise Exception(
																																								    '`constraint.update(token_id)` is not yielding incremental progress, '
																																								    'even though `constraint.does_advance(token_id)` is true.'   )

																																	if complete:
																																								self.complete_constraints.append(__snake_case   )
																																								a__    =  None

																																	if not complete and stepped:
																																								a__    =  pending_constraint

																																	if complete or stepped:
																																								# If we made any progress at all, then it's at least not a "pending constraint".

																																								a__    =  (
																																								    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																																								)

																																								if len(self.pending_constraints   ) == 0 and self.inprogress_constraint is None:
																																															# If there's no longer any pending after this and no inprogress either, then we must be
																																															# complete.

																																															a__    =  True

																																								break  # prevent accidentally stepping through multiple constraints with just one token.

												return complete, stepped



					def 				lowerCamelCase__(   self     :str    ,__snake_case     :List[str]=True   )      ->					Union[str, Any]:
												a__    =  ConstraintListState(self.constraints   )  # we actually never though self.constraints objects
												# throughout this process. So it's at initialization state.

												if stateful:
																			a__    =  [
																			    constraint.copy(stateful=__snake_case   ) for constraint in self.complete_constraints
																			]
																			if self.inprogress_constraint is not None:
																										a__    =  self.inprogress_constraint.copy(stateful=__snake_case   )
																			a__    =  [constraint.copy() for constraint in self.pending_constraints]

												return new_state





from sklearn.metrics import fa_score

import datasets


snake_case							: Optional[int]      =   '''
The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:
F1 = 2 * (precision * recall) / (precision + recall)
'''


snake_case							: List[Any]      =   '''
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.
    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.
    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.

        - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.
        - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives.
        - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
        - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.
        - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.

Examples:

    Example 1-A simple binary example
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])
        >>> print(results)
        {\'f1\': 0.5}

    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)
        >>> print(round(results[\'f1\'], 2))
        0.67

    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])
        >>> print(round(results[\'f1\'], 2))
        0.35

    Example 4-A multiclass example, with different values for the `average` input.
        >>> predictions = [0, 2, 1, 0, 0, 1]
        >>> references = [0, 1, 2, 0, 1, 2]
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro")
        >>> print(round(results[\'f1\'], 2))
        0.33
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)
        >>> print(results)
        {\'f1\': array([0.8, 0. , 0. ])}
'''


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







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,		_KWARGS_DESCRIPTION   )
class  snake_case_     (datasets.Metric   ):
					def 				lowerCamelCase__(   self     :Any   )      ->					Any:
												return datasets.MetricInfo(
												    description=_DESCRIPTION    ,citation=_CITATION    ,inputs_description=_KWARGS_DESCRIPTION    ,features=datasets.Features(
												        {
												            'predictions': datasets.Sequence(datasets.Value('int32'   )   ),
												            'references': datasets.Sequence(datasets.Value('int32'   )   ),
												        }
												        if self.config_name == 'multilabel'
												        else {
												            'predictions': datasets.Value('int32'   ),
												            'references': datasets.Value('int32'   ),
												        }   )    ,reference_urls=['https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html']    ,)



					def 				lowerCamelCase__(   self     :Dict    ,__snake_case     :str    ,__snake_case     :str    ,__snake_case     :Dict=None    ,__snake_case     :str=1    ,__snake_case     :Optional[int]="binary"    ,__snake_case     :Union[str, Any]=None   )      ->					Tuple:
												a__    =  fa_score(
												    __snake_case    ,__snake_case    ,labels=__snake_case    ,pos_label=__snake_case    ,average=__snake_case    ,sample_weight=__snake_case   )
												return {"f1": float(__snake_case   ) if score.size == 1 else score}




