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
4 changes: 3 additions & 1 deletion concordia/utils/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

def _extract_parenthesized_choice(sample: str):
"""Given text formatted as 'lorum(a)ipsum', return 'a'."""
match = re.search(r'\(?(\w)\)', sample)
# The word boundary stops the last letter of a parenthesized word, such as
# the 'y' in '(probably)', from being mistaken for a choice.
match = re.search(r'\(?\b(\w)\)', sample)
if match:
return match.group(1)
else:
Expand Down
7 changes: 7 additions & 0 deletions concordia/utils/sampling_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class ExtractChoiceResponseTest(parameterized.TestCase):
('parenthesized_in_middle', 'foo(a)bar', 'a'),
('parenthesized_at_end', 'The answer is (b).', 'b'),
('letter_then_paren_in_longer_string', 'a) because', 'a'),
(
'parenthesized_word_before_choice',
'I think (probably) the answer is (b)',
'b',
),
('unopened_word_before_choice', 'maybe) (a)', 'a'),
)
def test_extracts_choice(self, sample, expected):
self.assertEqual(sampling.extract_choice_response(sample), expected)
Expand All @@ -38,6 +44,7 @@ def test_extracts_choice(self, sample, expected):
('empty_string', ''),
('no_parenthesized_choice', 'no choice here'),
('word_without_closing_paren', '(abc'),
('parenthesized_word_only', '(yes) a'),
)
def test_returns_none_when_no_choice(self, sample):
self.assertIsNone(sampling.extract_choice_response(sample))
Expand Down
Loading