Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ All notable changes to this project will be documented in this file.
### Removed

### Fixed
- Ignore empty pieces in `verify_sentence_constraint` after a trailing terminator. (https://github.com/allenai/open-instruct/pull/1780).
4 changes: 2 additions & 2 deletions open_instruct/if_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ def verify_sentence_constraint(text: str, N: int, quantifier: str) -> bool:
Returns:
bool: True if the text contains the expected number of sentences, False otherwise
"""
# Split the text into sentences
sentences = re.split(r"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|!)\s", text)
# Split the text into sentences; drop empty trailing pieces from a final terminator.
sentences = [s for s in re.split(r"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|!)\s", text) if s.strip()]

# Count the number of sentences
actual_count = len(sentences)
Expand Down
14 changes: 14 additions & 0 deletions open_instruct/test_if_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,19 @@ def test_validate_frequency_capital_words(self, _name, text, n, quantifier, expe
self.assertEqual(if_functions.validate_frequency_capital_words(text, n, quantifier), expected)



class TestVerifySentenceConstraint(unittest.TestCase):
@parameterized.expand(
[
("two_sentences", "One. Two.", 2, "at least", True),
("trailing_space", "One. Two. ", 2, "at most", True),
("trailing_was_three", "One. Two. ", 3, "at least", False),
("empty", "", 1, "at least", False),
]
)
def test_verify_sentence_constraint(self, _name, text, n, quantifier, expected):
self.assertEqual(if_functions.verify_sentence_constraint(text, n, quantifier), expected)


if __name__ == "__main__":
unittest.main()