Skip to content

Commit bfdaf5e

Browse files
fix: tokenize validate_word_constraint like IFEvalG
Whitespace split undercounted words with punctuation (It's → one token). Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 78d1e5a commit bfdaf5e

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ All notable changes to this project will be documented in this file.
1818
### Removed
1919

2020
### Fixed
21+
- Tokenize `validate_word_constraint` with word-character tokens like IFEvalG `count_words`.

open_instruct/if_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ def validate_word_constraint(text: str, N: int, quantifier: str) -> bool:
200200
Raises:
201201
ValueError: If an invalid qualifier is provided
202202
"""
203-
# Remove extra whitespace and split into words
204-
words = text.strip().split()
203+
# Match IFEvalG NumberOfWords / count_words: tokenize on word characters.
204+
words = re.findall(r"\w+", text)
205205
actual_count = len(words)
206206

207207
# Define tolerance for "around" qualifier (±10% of target count)

open_instruct/test_if_functions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,19 @@ def test_validate_frequency_capital_words(self, _name, text, n, quantifier, expe
3939
self.assertEqual(if_functions.validate_frequency_capital_words(text, n, quantifier), expected)
4040

4141

42+
43+
class TestValidateWordConstraint(unittest.TestCase):
44+
@parameterized.expand(
45+
[
46+
("punct_split_diff", "Hello, world! It's a test.", 6, "at least", True),
47+
("punct_old_split_would_be_5", "Hello, world! It's a test.", 5, "at most", False),
48+
("at_most", "one two three", 2, "at most", False),
49+
("around", "one two three", 3, "around", True),
50+
]
51+
)
52+
def test_validate_word_constraint(self, _name, text, n, quantifier, expected):
53+
self.assertEqual(if_functions.validate_word_constraint(text, n, quantifier), expected)
54+
55+
4256
if __name__ == "__main__":
4357
unittest.main()

0 commit comments

Comments
 (0)