Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix not-contraction offsets + add test (resolves #15) #17

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion syntok/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _produce_separator_split_token(
yield Token(prefix, word[remainder:mo.start() - 1], offset + remainder)
prefix = ""

yield Token(prefix, "not" if self.replace_not_contraction else 'n' + mo.group(0), offset + mo.start())
yield Token(prefix, "not" if self.replace_not_contraction else 'n' + mo.group(0), offset + mo.start() - 1)
return ""

yield Token(prefix, word[remainder:mo.start()], offset + remainder)
Expand Down
17 changes: 17 additions & 0 deletions syntok/tokenizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ def test_nonword_high_prefix(self):
self.assertListEqual(s(result), ["\U0001F64C", ".", "A"])
self.assertListEqual([t.offset for t in result], [0, 1, 2]) # requires Py3.3+

def test_apostrophe_offset_without_replace_not_contraction(self):
# NOTE: in this case nothing is replaced, so the offsets should remain identical
# to those in the original text
text = "don't"
self.tokenizer = Tokenizer(replace_not_contraction=False)
result = self.tokenizer.split(text)
self.assertListEqual([t.offset for t in result], [0, 2])

def test_apostrophe_offset_with_replace_not_contraction(self):
# NOTE: in this case, "n't" is replaced with "not", so a space is introduced.
# e.g. "don't" -> "do not", "can't" -> "can not"
text = "don't"
self.tokenizer = Tokenizer(replace_not_contraction=True)
result = self.tokenizer.split(text)
self.assertListEqual([t.offset for t in result], [0, 2])
self.assertListEqual([t.value for t in result], ["do", "not"])


class TestToText(TestCase):

Expand Down