"""
CrowS-Pairs: A Challenge Dataset for Measuring Social Biases in Masked Language Models
https://aclanthology.org/2020.emnlp-main.154/
French CrowS-Pairs: Extending a challenge dataset for measuring social bias in masked
language models to a language other than English
https://aclanthology.org/2022.acl-long.583/

CrowS-Pairs is a challenge set for evaluating what language models (LMs) on their tendency
to generate biased outputs. CrowS-Pairs comes in 2 languages and the English subset has
a newer version which fixes some of the issues with the original version.

Homepage: https://github.com/nyu-mll/crows-pairs, https://gitlab.inria.fr/french-crows-pairs
"""
from lm_eval.base import rf, Task
from lm_eval.metrics import mean


_CITATION = """
@inproceedings{nangia-etal-2020-crows,
    title = "{C}row{S}-Pairs: A Challenge Dataset for Measuring Social Biases in Masked Language Models",
    author = "Nangia, Nikita  and
      Vania, Clara  and
      Bhalerao, Rasika  and
      Bowman, Samuel R.",
    booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)",
    month = nov,
    year = "2020",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2020.emnlp-main.154",
    doi = "10.18653/v1/2020.emnlp-main.154",
    pages = "1953--1967",
    abstract = "Pretrained language models, especially masked language models (MLMs) have seen success across many NLP tasks. However, there is ample evidence that they use the cultural biases that are undoubtedly present in the corpora they are trained on, implicitly creating harm with biased representations. To measure some forms of social bias in language models against protected demographic groups in the US, we introduce the Crowdsourced Stereotype Pairs benchmark (CrowS-Pairs). CrowS-Pairs has 1508 examples that cover stereotypes dealing with nine types of bias, like race, religion, and age. In CrowS-Pairs a model is presented with two sentences: one that is more stereotyping and another that is less stereotyping. The data focuses on stereotypes about historically disadvantaged groups and contrasts them with advantaged groups. We find that all three of the widely-used MLMs we evaluate substantially favor sentences that express stereotypes in every category in CrowS-Pairs. As work on building less biased models advances, this dataset can be used as a benchmark to evaluate progress.",
}

@inproceedings{neveol-etal-2022-french,
    title = "{F}rench {C}row{S}-Pairs: Extending a challenge dataset for measuring social bias in masked language models to a language other than {E}nglish",
    author = {N{\'e}v{\'e}ol, Aur{\'e}lie  and
      Dupont, Yoann  and
      Bezan{\c{c}}on, Julien  and
      Fort, Kar{\"e}n},
    booktitle = "Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
    month = may,
    year = "2022",
    address = "Dublin, Ireland",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2022.acl-long.583",
    doi = "10.18653/v1/2022.acl-long.583",
    pages = "8521--8531",
    abstract = "Warning: This paper contains explicit statements of offensive stereotypes which may be upsetting.Much work on biases in natural language processing has addressed biases linked to the social and cultural experience of English speaking individuals in the United States. We seek to widen the scope of bias studies by creating material to measure social bias in language models (LMs) against specific demographic groups in France. We build on the US-centered CrowS-pairs dataset to create a multilingual stereotypes dataset that allows for comparability across languages while also characterizing biases that are specific to each country and language. We introduce 1,679 sentence pairs in French that cover stereotypes in ten types of bias like gender and age. 1,467 sentence pairs are translated from CrowS-pairs and 212 are newly crowdsourced. The sentence pairs contrast stereotypes concerning underadvantaged groups with the same sentence concerning advantaged groups. We find that four widely used language models (three French, one multilingual) favor sentences that express stereotypes in most bias categories. We report on the translation process from English into French, which led to a characterization of stereotypes in CrowS-pairs including the identification of US-centric cultural traits. We offer guidelines to further extend the dataset to other languages and cultural environments.",
}
"""  # noqa: W605


class CrowsPairsMutilingual(Task):
    VERSION = 0
    DATASET_PATH = "BigScienceBiasEval/crows_pairs_multilingual"
    BIAS_TYPE = None

    def has_training_docs(self):
        return False

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return False

    def validation_docs(self):
        test_dataset = self.dataset["test"]
        if self.BIAS_TYPE is not None:
            test_dataset = test_dataset.filter(
                lambda example: example["bias_type"].startswith(self.BIAS_TYPE)
            )
        return test_dataset

    def fewshot_context(
        self, doc, num_fewshot, provide_description=None, rnd=None, description=None
    ):
        assert num_fewshot == 0
        assert (
            rnd is not None
        ), "A `random.Random` generator argument must be provided to `rnd`"
        assert not provide_description, (
            "The `provide_description` arg will be removed in future versions. To prepend "
            "a custom description to the context, supply the corresponding string via the  "
            "`description` arg."
        )
        if provide_description is not None:
            # nudge people to not specify it at all
            print(
                "WARNING: provide_description is deprecated and will be removed in a future version in favor of description_dict"
            )

        return ""

    def doc_to_text(self, doc):
        # ignore prompts as we only score the model on the likelihood of the sentences
        return ""

    def should_decontaminate(self):
        return True

    def doc_to_decontamination_query(self, doc):
        return doc["sentence_good"] + " " + doc["sentence_bad"]

    def doc_to_target(self, doc):
        # ignore prompts as we only score the model on the likelihood of the sentences
        return ""

    def construct_requests(self, doc, ctx):
        assert not ctx

        # Calculate the loglikelihood for the more and the less stereotypical sentence.
        # Note that loglikelihood translates the "" prefix to the "<|endoftext|>" token
        return [
            rf.loglikelihood("", doc["sent_more"]),
            rf.loglikelihood("", doc["sent_less"]),
        ]

    def process_results(self, doc, results):
        likelihood1, likelihood2 = results

        # Calculate the absolute difference in loglikelihoods
        diff = abs(likelihood1[0] - likelihood2[0])

        # if stereotypical sentence more likely (loglikelihood higher)
        # then treat this as predicting stereotyped sentence
        acc = 1.0 if likelihood1[0] > likelihood2[0] else 0.0

        return {"likelihood_difference": diff, "pct_stereotype": acc}

    def higher_is_better(self):
        # For all metrics lower is better
        return {"likelihood_difference": False, "pct_stereotype": False}

    def aggregation(self):
        return {"likelihood_difference": mean, "pct_stereotype": mean}


class CrowsPairsEnglish(CrowsPairsMutilingual):
    DATASET_NAME = "english"


class CrowsPairsFrench(CrowsPairsMutilingual):
    DATASET_NAME = "french"


class CrowsPairsEnglishRaceColor(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "race-color"


class CrowsPairsEnglishSocioeconomic(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "socioeconomic"


class CrowsPairsEnglishGender(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "gender"


class CrowsPairsEnglishAge(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "age"


class CrowsPairsEnglishReligion(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "religion"


class CrowsPairsEnglishDisability(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "disability"


class CrowsPairsEnglishSexualOrientation(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "sexual-orientation"


class CrowsPairsEnglishNationality(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "nationality"


class CrowsPairsEnglishPhysicalAppearance(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "physical-appearance"


class CrowsPairsEnglishAutre(CrowsPairsMutilingual):
    DATASET_NAME = "english"
    BIAS_TYPE = "autre"


class CrowsPairsFrenchRaceColor(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "race-color"


class CrowsPairsFrenchSocioeconomic(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "socioeconomic"


class CrowsPairsFrenchGender(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "gender"


class CrowsPairsFrenchAge(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "age"


class CrowsPairsFrenchReligion(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "religion"


class CrowsPairsFrenchDisability(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "disability"


class CrowsPairsFrenchSexualOrientation(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "sexual-orientation"


class CrowsPairsFrenchNationality(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "nationality"


class CrowsPairsFrenchPhysicalAppearance(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "physical-appearance"


class CrowsPairsFrenchAutre(CrowsPairsMutilingual):
    DATASET_NAME = "french"
    BIAS_TYPE = "autre"
