Skip to content

euroeval.metrics.ifeval.constraints

[docs] module euroeval.metrics.ifeval.constraints

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
"""Constraint functions used for the IFEval metric."""

import collections
import collections.abc as c
import json
import logging
import re
import typing as t
from functools import wraps

import langdetect
import nltk

from ...exceptions import InvalidBenchmark

logger = logging.getLogger(__name__)


class Constraint(t.Protocol):
    """An instruction-following constraint."""

    def __call__(self, response: str, **constraint_kwargs) -> bool:
        """Apply the constraint.

        Args:
            response:
                The output to be checked.
            **constraint_kwargs:
                Extra keyword arguments for the constraint function.

        Returns:
            True if the constraint is satisfied, otherwise False.
        """
        ...


ALL_CONSTRAINTS: dict[str, Constraint] = dict()


def register(
    name: str, **desired_keys_and_types
) -> c.Callable[[Constraint], Constraint]:
    """Decorator that registers a function under the given name.

    Args:
        name:
            The name under which to register the function.
        **desired_keys_and_types:
            The keyword arguments and their types that the function expects.

    Returns:
        The decorator function.
    """

    def decorator(fn: Constraint) -> Constraint:
        """Register the function under the given name.

        Args:
            fn:
                The function to register.

        Returns:
            The function.
        """
        # This enables us to chain the register decorator
        if hasattr(fn, "_original_fn"):
            fn = fn._original_fn

        @wraps(fn)
        def wrapper(response: str, **constraint_kwargs) -> bool:
            """Wrapper function that checks the keyword arguments and their types.

            Args:
                response:
                    The response string to be checked.
                **constraint_kwargs:
                    Extra keyword arguments for the constraint function.

            Returns:
                The result of the function.

            Raises:
                InvalidBenchmark:
                    If a required keyword argument is missing or if a keyword argument
                    has the wrong type.
            """
            for key, type_ in desired_keys_and_types.items():
                if key not in constraint_kwargs:
                    raise InvalidBenchmark(
                        f"The function {fn.__name__!r} (registered as {name!r}) "
                        f"requires the keyword argument {key!r}."
                    )

                # Special case for Literal, since it does not support `isinstance`
                elif t.get_origin(type_) == t.Literal:
                    possible_values = t.get_args(type_)
                    if constraint_kwargs[key] not in possible_values:
                        raise InvalidBenchmark(
                            f"The function {fn.__name__!r} (registered as {name!r}) "
                            f"expects the keyword argument {key!r} to be one of "
                            f"{possible_values}, but got {constraint_kwargs[key]!r}."
                        )

                elif not isinstance(constraint_kwargs[key], type_):
                    if type_ is int and isinstance(constraint_kwargs[key], float):
                        constraint_kwargs[key] = int(constraint_kwargs[key])
                    elif type_ is float and isinstance(constraint_kwargs[key], int):
                        constraint_kwargs[key] = float(constraint_kwargs[key])
                    else:
                        raise InvalidBenchmark(
                            f"The function {fn.__name__!r} (registered as {name!r}) "
                            f"expects the keyword argument {key!r} to be of type "
                            f"{type_.__name__!r}, but got "
                            f"{type(constraint_kwargs[key]).__name__!r}."
                        )
            return fn(response, **constraint_kwargs)

        wrapper._original_fn = fn
        ALL_CONSTRAINTS[name] = wrapper
        return fn

    return decorator


@register("keywords:existence", keywords=list)
@register("fr:keywords:existence", keywords=list)
@register("es:keywords:existence", keywords=list)
@register("ca:keywords:existence", keywords=list)
def check_keyword_existence(response: str, **constraint_kwargs) -> bool:
    """Check that all keywords exist in the response.

    Args:
        response:
            The response string to be checked.
        **constraint_kwargs:
            Keyword arguments containing ``keywords`` – a list of keyword patterns
            (case‑insensitive) to search for.

    Returns:
        True if all keywords are found in the response, False otherwise.
    """
    keywords: list[str] = constraint_kwargs["keywords"]

    for keyword in keywords:
        if not re.search(pattern=keyword, string=response, flags=re.IGNORECASE):
            return False
    return True


@register(
    "keywords:frequency",
    keyword=str,
    frequency=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "fr:keywords:frequency",
    keyword=str,
    frequency=int,
    relation=t.Literal["moins de", "au moins"],
)
@register(
    "es:keywords:frequency",
    keyword=str,
    frequency=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "ca:keywords:frequency",
    keyword=str,
    frequency=int,
    relation=t.Literal["less than", "at least"],
)
def check_keyword_frequency(response: str, **constraint_kwargs) -> bool:
    """Check keyword appears with required frequency.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``keyword``, ``frequency`` and
            ``relation`` – the keyword pattern (case‑insensitive), the required
            frequency, and the comparison operator ("less than" or "at least").

    Returns:
        True if the keyword appears with the required frequency, False otherwise.
    """
    keyword: str = constraint_kwargs["keyword"]
    frequency: int = constraint_kwargs["frequency"]
    relation: str = constraint_kwargs["relation"]

    all_keyword_matches = re.findall(
        pattern=keyword, string=response, flags=re.IGNORECASE
    )
    if relation in {"less than", "moins de"}:
        return len(all_keyword_matches) < frequency
    return len(all_keyword_matches) >= frequency


@register("keywords:forbidden_words", forbidden_words=list)
@register("fr:keywords:forbidden_words", forbidden_words=list)
@register("es:keywords:forbidden_words", forbidden_words=list)
@register("ca:keywords:forbidden_words", forbidden_words=list)
def check_forbidden_words(response: str, **constraint_kwargs) -> bool:
    """Check that forbidden words don't appear.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``forbidden_words`` – a list of
            words that must not appear (case‑insensitive, whole‑word match).

    Returns:
        True if none of the forbidden words are found, False otherwise.
    """
    forbidden_words: list[str] = constraint_kwargs["forbidden_words"]

    for word in forbidden_words:
        if re.search(
            pattern=r"\b" + word + r"\b", string=response, flags=re.IGNORECASE
        ):
            return False
    return True


@register(
    "keywords:letter_frequency",
    letter=str,
    let_frequency=int,
    let_relation=t.Literal["less than", "at least"],
)
@register(
    "fr:keywords:letter_frequency",
    letter=str,
    let_frequency=int,
    let_relation=t.Literal["moins de", "au moins"],
)
@register(
    "es:keywords:letter_frequency",
    letter=str,
    let_frequency=int,
    let_relation=t.Literal["less than", "at least"],
)
@register(
    "ca:keywords:letter_frequency",
    letter=str,
    let_frequency=int,
    let_relation=t.Literal["less than", "at least"],
)
def check_letter_frequency(response: str, **constraint_kwargs) -> bool:
    """Check letter appears with required frequency.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``letter``, ``let_frequency`` and
            ``let_relation`` – the single character to count (case‑insensitive),
            the frequency threshold, and the comparison operator ("less than"
            or "at least").

    Returns:
        True if the letter frequency satisfies the relation, False otherwise.

    Raises:
        InvalidBenchmark:
            If letter is not a single character.
    """
    letter: str = constraint_kwargs["letter"]
    let_frequency: int = constraint_kwargs["let_frequency"]
    let_relation: str = constraint_kwargs["let_relation"]
    if len(letter) != 1:
        raise InvalidBenchmark("letter must be a single character")

    counts = collections.Counter(response.lower())
    if let_relation in {"less than", "moins de"}:
        return counts[letter.lower()] < let_frequency
    return counts[letter.lower()] >= let_frequency


@register(
    "length_constraints:number_sentences",
    num_sentences=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "fr:length_constraints:number_sentences",
    num_sentences=int,
    relation=t.Literal["moins de", "au moins"],
)
@register(
    "es:length_constraints:number_sentences",
    num_sentences=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "ca:length_constraints:number_sentences",
    num_sentences=int,
    relation=t.Literal["less than", "at least"],
)
def check_number_sentences(response: str, **constraint_kwargs) -> bool:
    """Check number of sentences.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_sentences`` and ``relation``.

    Returns:
        True if the sentence count satisfies the relation, False otherwise.
    """
    num_sentences: int = constraint_kwargs["num_sentences"]
    relation: str = constraint_kwargs["relation"]

    actual = len(nltk.tokenize.sent_tokenize(text=response))
    if relation in {"less than", "moins de"}:
        return actual < num_sentences
    return actual >= num_sentences


@register("length_constraints:number_paragraphs", num_paragraphs=int)
@register("fr:length_constraints:number_paragraphs", num_paragraphs=int)
@register("es:length_constraints:number_paragraphs", num_paragraphs=int)
@register("ca:length_constraints:number_paragraphs", num_paragraphs=int)
def check_number_paragraphs(response: str, **constraint_kwargs) -> bool:
    """Check number of paragraphs (separated by ***).

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_paragraphs`` – the exact number
            of paragraphs expected.

    Returns:
        True if the response contains exactly num_paragraphs non‑empty paragraphs,
        False otherwise.
    """
    num_paragraphs: int = constraint_kwargs["num_paragraphs"]

    paragraphs = re.split(pattern=r"\s?\*\*\*\s?", string=response)
    count = len(paragraphs)
    for i, p in enumerate(paragraphs):
        if not p.strip():
            if i == 0 or i == len(paragraphs) - 1:
                count -= 1
            else:
                return False
    return count == num_paragraphs


@register(
    "length_constraints:number_words",
    num_words=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "fr:length_constraints:number_words",
    num_words=int,
    relation=t.Literal["moins de", "au moins"],
)
@register(
    "es:length_constraints:number_words",
    num_words=int,
    relation=t.Literal["less than", "at least"],
)
@register(
    "ca:length_constraints:number_words",
    num_words=int,
    relation=t.Literal["less than", "at least"],
)
def check_number_words(response: str, **constraint_kwargs) -> bool:
    """Check number of words.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_words`` and ``relation``.

    Returns:
        True if the word count satisfies the relation, False otherwise.
    """
    num_words: int = constraint_kwargs["num_words"]
    relation: str = constraint_kwargs["relation"]

    words = nltk.tokenize.word_tokenize(text=response)
    actual = len(words)
    if relation in {"less than", "moins de"}:
        return actual < num_words
    return actual >= num_words


@register(
    "length_constraints:nth_paragraph_first_word",
    num_paragraphs=int,
    nth_paragraph=int,
    first_word=str,
)
@register(
    "fr:length_constraints:nth_paragraph_first_word",
    num_paragraphs=int,
    nth_paragraph=int,
    first_word=str,
)
@register(
    "es:length_constraints:nth_paragraph_first_word",
    num_paragraphs=int,
    nth_paragraph=int,
    first_word=str,
)
@register(
    "ca:length_constraints:nth_paragraph_first_word",
    num_paragraphs=int,
    nth_paragraph=int,
    first_word=str,
)
def check_nth_paragraph_first_word(response: str, **constraint_kwargs) -> bool:
    """Check paragraph count and first word of nth paragraph.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_paragraphs``, ``nth_paragraph``,
            and ``first_word`` – the expected first word of the nth paragraph
            (case‑insensitive).

    Returns:
        True if the response has exactly num_paragraphs paragraphs and the nth
        paragraph starts with first_word, False otherwise.

    Raises:
        InvalidBenchmark:
            If the n'th paragraph is greater than the number of paragraphs in the
            response.
    """
    num_paragraphs: int = constraint_kwargs["num_paragraphs"]
    nth_paragraph: int = constraint_kwargs["nth_paragraph"]
    first_word: str = constraint_kwargs["first_word"]
    if nth_paragraph > num_paragraphs:
        raise InvalidBenchmark(
            "The n'th paragraph is greater than the number of paragraphs in the "
            "`check_nth_paragraph_first_word` constraint. This should not happen."
        )

    paragraphs = re.split(pattern=r"\n\n", string=response)
    count = sum(1 for p in paragraphs if p.strip())
    if nth_paragraph > count:
        return False

    paragraph = paragraphs[nth_paragraph - 1].strip()
    if not paragraph:
        return False

    word = paragraph.split()[0].strip().lstrip("'\"")
    actual_first = ""
    for char in word:
        if char in ".,?!'\"":
            break
        actual_first += char.lower()

    return count == num_paragraphs and actual_first == first_word.lower()


@register("detectable_content:number_placeholders", num_placeholders=int)
@register("fr:detectable_content:number_placeholders", num_placeholders=int)
@register("es:detectable_content:number_placeholders", num_placeholders=int)
@register("ca:detectable_content:number_placeholders", num_placeholders=int)
def check_number_placeholders(response: str, **constraint_kwargs) -> bool:
    """Check minimum number of [placeholder] brackets.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_placeholders`` – the minimum
            number of placeholder brackets expected.

    Returns:
        True if the response contains at least num_placeholders placeholders
        of the form [placeholder], False otherwise.
    """
    num_placeholders: int = constraint_kwargs["num_placeholders"]

    placeholders = re.findall(pattern=r"\[.*?\]", string=response)
    return len(placeholders) >= num_placeholders


@register("detectable_content:postscript", postscript_marker=str)
@register("fr:detectable_content:postscript", postscript_marker=str)
@register("es:detectable_content:postscript", postscript_marker=str)
@register("ca:detectable_content:postscript", postscript_marker=str)
def check_postscript(response: str, **constraint_kwargs) -> bool:
    """Check for postscript marker.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``postscript_marker`` – the postscript
            label to look for (e.g. "P.S.", "P.P.S").

    Returns:
        True if the postscript marker is found in the response, False otherwise.
    """
    postscript_marker: str = constraint_kwargs["postscript_marker"]

    response = response.lower()
    if postscript_marker == "P.P.S":
        pattern = r"\s*p\.\s?p\.\s?s.*$"
    elif postscript_marker == "P.S.":
        pattern = r"\s*p\.\s?s\..*$"
    else:
        pattern = r"\s*" + postscript_marker.lower() + r".*$"
    return bool(re.findall(pattern=pattern, string=response, flags=re.MULTILINE))


@register("detectable_format:number_bullet_lists", num_bullets=int)
@register("fr:detectable_format:number_bullet_lists", num_bullets=int)
@register("es:detectable_format:number_bullet_lists", num_bullets=int)
@register("ca:detectable_format:number_bullet_lists", num_bullets=int)
def check_number_bullet_lists(response: str, **constraint_kwargs) -> bool:
    """Check exact number of bullet points.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_bullets`` – the exact number
            of bullet points expected.

    Returns:
        True if the response contains exactly num_bullets bullet points, where bullet
        points are lines starting with ``*`` or ``-``, False otherwise.
    """
    num_bullets: int = constraint_kwargs["num_bullets"]

    bullets1 = re.findall(
        pattern=r"^\s*\*[^\*].*$", string=response, flags=re.MULTILINE
    )
    bullets2 = re.findall(pattern=r"^\s*-.*$", string=response, flags=re.MULTILINE)
    return len(bullets1) + len(bullets2) == num_bullets


@register("detectable_format:constrained_response")
def check_constrained_response_english(response: str, **_) -> bool:
    """Check response contains one of the constrained options.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains exactly one of "My answer is yes.", "My answer is
        no.", or "My answer is maybe.", False otherwise.
    """
    options = ("My answer is yes.", "My answer is no.", "My answer is maybe.")
    return any(opt in response.strip() for opt in options)


@register("es:detectable_format:constrained_response")
def check_constrained_response_spanish(response: str, **_) -> bool:
    """Check response contains one of the constrained options.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains exactly one of "Mi respuesta es sí" or
        "Mi respuesta es no" or "Mi respuesta es tal vez", False otherwise.
    """
    options = ("Mi respuesta es sí", "Mi respuesta es no", "Mi respuesta es tal vez")
    return any(opt in response.strip() for opt in options)


@register("ca:detectable_format:constrained_response")
def check_constrained_response_catalan(response: str, **_) -> bool:
    """Check response contains one of the constrained options.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains exactly one of "La meva resposta és sí.",
        "La meva resposta és no.", or "La meva resposta és potser.", False otherwise.
    """
    options = (
        "La meva resposta és sí.",
        "La meva resposta és no.",
        "La meva resposta és potser.",
    )
    return any(opt in response.strip() for opt in options)


@register("fr:detectable_format:constrained_response")
def check_constrained_response_french(response: str, **_) -> bool:
    """Check response contains one of the constrained options.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains exactly one of "Oui.", "Non.", or "Peut-être.",
        False otherwise.
    """
    options = ("Oui.", "Non.", "Peut-être.")
    return any(opt in response.strip() for opt in options)


@register("detectable_format:constrained_response_with_argument", options=list)
def check_constrained_response_with_argument(
    response: str, **constraint_kwargs
) -> bool:
    """Check response contains one of the constrained options.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``options`` – a list of strings to
            check for.

    Returns:
        True if the response contains exactly one of the options, False otherwise.
    """
    options: list[str] = constraint_kwargs["options"]
    return any(opt in response.strip() for opt in options)


@register("detectable_format:number_highlighted_sections", num_highlights=int)
@register("fr:detectable_format:number_highlighted_sections", num_highlights=int)
@register("es:detectable_format:number_highlighted_sections", num_highlights=int)
@register("ca:detectable_format:number_highlighted_sections", num_highlights=int)
def check_number_highlighted_sections(response: str, **constraint_kwargs) -> bool:
    """Check minimum highlighted *sections*.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``num_highlights`` – the minimum number
            of highlighted sections expected.

    Returns:
        True if the response contains at least num_highlights non‑empty highlighted
        sections, where highlights are text wrapped in single (*text*) or double
        (**text**) asterisks, False otherwise.
    """
    num_highlights: int = constraint_kwargs["num_highlights"]

    count = 0
    for h in re.findall(pattern=r"\*[^\n\*]*\*", string=response):
        if h.strip("*").strip():
            count += 1
    for h in re.findall(pattern=r"\*\*[^\n\*]*\*\*", string=response):
        if h.removeprefix("**").removesuffix("**").strip():
            count += 1
    return count >= num_highlights


@register("detectable_format:multiple_sections", section_spliter=str, num_sections=int)
@register(
    "fr:detectable_format:multiple_sections", section_spliter=str, num_sections=int
)
@register(
    "es:detectable_format:multiple_sections", section_spliter=str, num_sections=int
)
@register(
    "ca:detectable_format:multiple_sections", section_spliter=str, num_sections=int
)
def check_multiple_sections(response: str, **constraint_kwargs) -> bool:
    """Check for Section X markers.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``section_spliter`` and ``num_sections``.

    Returns:
        True if the response contains at least num_sections sections delimited
        by markers of the form "<section_spliter> <number>", False otherwise.
    """
    section_spliter: str = constraint_kwargs["section_spliter"]
    num_sections: int = constraint_kwargs["num_sections"]

    pattern = r"\s?" + section_spliter + r"\s?\d+\s?"
    sections = re.split(pattern=pattern, string=response)
    return len(sections) - 1 >= num_sections


@register("detectable_format:json_format")
@register("fr:detectable_format:json_format")
@register("es:detectable_format:json_format")
@register("ca:detectable_format:json_format")
def check_json_format(response: str, **_) -> bool:
    """Check response is valid JSON.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response (after stripping optional ```json``` fences) is valid JSON,
        False otherwise.
    """
    value = (
        response.strip()
        .removeprefix("```json")
        .removeprefix("```Json")
        .removeprefix("```JSON")
        .removeprefix("```")
        .removesuffix("```")
        .strip()
    )
    try:
        json.loads(value)
        return True
    except json.JSONDecodeError:
        return False


@register("detectable_format:title")
@register("fr:detectable_format:title")
@register("es:detectable_format:title")
@register("ca:detectable_format:title")
def check_title(response: str, **_) -> bool:
    """Check for <<title>> format.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains at least one non‑empty title wrapped in
        double angle brackets (e.g. <<My Title>>), False otherwise.
    """
    for title in re.findall(pattern=r"<<[^\n]+>>", string=response):
        if title.lstrip("<").rstrip(">").strip():
            return True
    return False


@register("combination:two_responses")
@register("fr:combination:two_responses")
@register("es:combination:two_responses")
@register("ca:combination:two_responses")
def check_two_responses(response: str, **_) -> bool:
    """Check for two different responses separated by ******.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains exactly two non‑empty, non‑identical sections
        separated by "******", False otherwise.
    """
    parts = response.split("******")
    valid = []
    for i, part in enumerate(parts):
        if not part.strip():
            if i != 0 and i != len(parts) - 1:
                return False
        else:
            valid.append(part)
    return len(valid) == 2 and valid[0].strip() != valid[1].strip()


@register("combination:repeat_prompt", prompt_to_repeat=str)
@register("fr:combination:repeat_prompt", prompt_to_repeat=str)
@register("es:combination:repeat_prompt", prompt_to_repeat=str)
@register("ca:combination:repeat_prompt", prompt_to_repeat=str)
def check_repeat_prompt(response: str, **constraint_kwargs) -> bool:
    """Check response starts with the prompt.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``prompt_to_repeat`` – the exact prompt text
            the response must begin with (case‑insensitive).

    Returns:
        True if the response starts with prompt_to_repeat, False otherwise.
    """
    prompt_to_repeat: str = constraint_kwargs["prompt_to_repeat"]
    return response.strip().lower().startswith(prompt_to_repeat.strip().lower())


@register("startend:end_checker", end_phrase=str)
@register("fr:startend:end_checker", end_phrase=str)
@register("es:startend:end_checker", end_phrase=str)
@register("ca:startend:end_checker", end_phrase=str)
def check_end_phrase(response: str, **constraint_kwargs) -> bool:
    """Check response ends with exact phrase.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``end_phrase`` – the exact phrase the
            response must end with (case‑insensitive).

    Returns:
        True if the response ends with end_phrase, False otherwise.
    """
    end_phrase: str = constraint_kwargs["end_phrase"]
    return response.strip().strip('"').lower().endswith(end_phrase.strip().lower())


@register(
    "change_case:capital_word_frequency",
    capital_frequency=int,
    capital_relation=t.Literal["less than", "at least"],
)
@register(
    "fr:change_case:capital_word_frequency",
    capital_frequency=int,
    capital_relation=t.Literal["moins de", "au moins"],
)
@register(
    "es:change_case:capital_word_frequency",
    capital_frequency=int,
    capital_relation=t.Literal["less than", "at least"],
)
@register(
    "ca:change_case:capital_word_frequency",
    capital_frequency=int,
    capital_relation=t.Literal["less than", "at least"],
)
def check_capital_word_frequency(response: str, **constraint_kwargs) -> bool:
    """Check frequency of ALL CAPS words.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``capital_frequency`` and
            ``capital_relation``.

    Returns:
        True if the count of fully uppercased words satisfies the relation,
        False otherwise.
    """
    capital_frequency: int = constraint_kwargs["capital_frequency"]
    capital_relation: str = constraint_kwargs["capital_relation"]

    words = nltk.word_tokenize(response)
    count = sum(1 for w in words if w.isupper())
    if capital_relation in {"less than", "moins de"}:
        return count < capital_frequency
    return count >= capital_frequency


@register("change_case:english_capital")
def check_english_capital(response: str, **_) -> bool:
    """Check response is English and all caps.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely uppercase and detected as English,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.isupper() and langdetect.detect(response) == "en"
    except langdetect.LangDetectException:
        return True


@register("es:change_case:spanish_capital")
def check_spanish_capital(response: str, **_) -> bool:
    """Check response is Spanish and all caps.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely uppercase and detected as Spanish, False
        otherwise. Returns True if language detection fails.
    """
    try:
        return response.isupper() and langdetect.detect(response) == "es"
    except langdetect.LangDetectException:
        return True


@register("fr:change_case:french_capital")
def check_french_capital(response: str, **_) -> bool:
    """Check response is French and all caps.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely uppercase and detected as French,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.isupper() and langdetect.detect(response) == "fr"
    except langdetect.LangDetectException:
        return True


@register("ca:change_case:catalan_capital")
def check_catalan_capital(response: str, **_) -> bool:
    """Check response is Catalan and all caps.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely uppercase and detected as Catalan,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.isupper() and langdetect.detect(response) == "ca"
    except langdetect.LangDetectException:
        return True


@register("change_case:english_lowercase")
def check_english_lowercase(response: str, **_) -> bool:
    """Check response is English and all lowercase.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely lowercase and detected as English,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.islower() and langdetect.detect(response) == "en"
    except langdetect.LangDetectException:
        return True


@register("es:change_case:spanish_lowercase")
def check_spanish_lowercase(response: str, **_) -> bool:
    """Check response is Spanish and all lowercase.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely lowercase and detected as Spanish,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.islower() and langdetect.detect(response) == "es"
    except langdetect.LangDetectException:
        return True


@register("fr:change_case:french_lowercase")
def check_french_lowercase(response: str, **_) -> bool:
    """Check response is French and all lowercase.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely lowercase and detected as French,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.islower() and langdetect.detect(response) == "fr"
    except langdetect.LangDetectException:
        return True


@register("ca:change_case:catalan_lowercase")
def check_catalan_lowercase(response: str, **_) -> bool:
    """Check response is Catalan and all lowercase.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response is entirely lowercase and detected as Catalan,
        False otherwise. Returns True if language detection fails.
    """
    try:
        return response.islower() and langdetect.detect(response) == "ca"
    except langdetect.LangDetectException:
        return True


@register("punctuation:no_comma")
@register("fr:punctuation:no_comma")
@register("es:punctuation:no_comma")
@register("ca:punctuation:no_comma")
def check_no_comma(response: str, **_) -> bool:
    """Check response has no commas.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains no comma characters, False otherwise.
    """
    return "," not in response


@register("startend:quotation")
@register("fr:startend:quotation")
@register("es:startend:quotation")
@register("ca:startend:quotation")
def check_quotation(response: str, **_) -> bool:
    """Check response is wrapped in double quotes.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response (after stripping whitespace) begins and ends with a double
        quote character, False otherwise.
    """
    response = response.strip()
    return len(response) > 1 and response[0] == '"' and response[-1] == '"'


@register("language:response_language", language=str)
@register("fr:language:response_language", language=str)
@register("es:language:response_language", language=str)
@register("ca:language:response_language", language=str)
def check_response_language(response: str, **constraint_kwargs) -> bool:
    """Check response is in specified language.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``language`` – the language code of the
            language the response must be in.

    Returns:
        True if the response is detected as the specified language, False otherwise.
        Returns True if language detection fails.
    """
    language: str = constraint_kwargs["language"]
    try:
        return langdetect.detect(response) == language
    except langdetect.LangDetectException:
        return True


@register("change_case:lowercase_letters")
@register("change_case:lowercase")
def check_lowercase_letters(response: str, **_) -> bool:
    """Check response has no uppercase letters.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains no uppercase letters, False otherwise.
    """
    return response.islower()


@register("change_case:capital_letters")
@register("change_case:capital")
def check_capital_letters(response: str, **_) -> bool:
    """Check response has no lowercase letters.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains no lowercase letters, False otherwise.
    """
    return response.isupper()


@register("fr:detectable_content:no_digits")
def check_no_digits(response: str, **_) -> bool:
    """Check response contains no digits.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains no digits, False otherwise.
    """
    return not any(char.isdigit() for char in response)


@register("fr:special_character:ethel_or_cedilla", forbidden_char=t.Literal["œ", "ç"])
def check_ethel_or_cedilla_not_present(response: str, **constraint_kwargs) -> bool:
    """Check response contains no forbidden character.

    Args:
        response:
            The response string to check.
        **constraint_kwargs:
            Keyword arguments containing ``forbidden_char`` – the character that is
            forbidden in the response (must not be present).

    Returns:
        True if the forbidden character is not present in the response, False otherwise.
    """
    forbidden_char: t.Literal["œ", "ç"] = constraint_kwargs["forbidden_char"]
    return forbidden_char not in response


@register("fr:special_character:no_accents")
def check_no_accents(response: str, **_) -> bool:
    """Check response contains no accents.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains no accents, False otherwise.
    """
    accented_chars = re.compile(
        pattern=r"[àáâãäåçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ]"
    )
    return accented_chars.search(response) is None


@register("fr:special_character:accents")
def check_accents(response: str, **_) -> bool:
    """Check response contains accents.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains accents, False otherwise.
    """
    accented_chars = re.compile(
        pattern=r"[àáâãäåçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ]"
    )
    return accented_chars.search(response) is not None


@register("fr:detectable_content:informal_address")
def check_informal_address(response: str, **_) -> bool:
    """Check response contains informal address.

    Args:
        response:
            The response string to check.

    Returns:
        True if the response contains informal address, False otherwise.
    """
    tu_indicators = [
        r"\btu\b",  # "tu" pronoun
        r"\bte\b",  # "te" pronoun
        r"\bt'\b",  # "t'" pronoun
        r"\btoi\b",  # "toi" pronoun
        r"\bton\b",  # possessive adjective
        r"\bta\b",  # possessive adjective
        r"\btes\b",  # possessive adjective
    ]

    pattern = "|".join(tu_indicators)
    return re.search(pattern, response, re.IGNORECASE) is not None