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

Extend Tokenizer to Support Single Strings and Lists of Strings #258

Merged
merged 4 commits into from
Nov 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions laser_encoders/laser_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def tokenize_file(self, inp_fname: Path, out_fname: Path) -> None:
tokens = self.tokenize(line.strip())
file_out.write(tokens + "\n")

def __call__(self, text_or_batch, batch=False):
if not batch:
def __call__(self, text_or_batch):
if isinstance(text_or_batch, str):
return self.tokenize(text_or_batch)
else:
return self.tokenize_batch(text_or_batch)
Expand Down
8 changes: 5 additions & 3 deletions laser_encoders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ def __init__(
self.encoder.eval()
self.sort_kind = sort_kind

def __call__(self, sentences):
def __call__(self, text_or_batch):
if self.spm_model:
sentences = self.tokenizer(sentences)
return self.encode_sentences(sentences)
text_or_batch = self.tokenizer(text_or_batch)
if isinstance(text_or_batch, str):
text_or_batch = [text_or_batch]
return self.encode_sentences(text_or_batch)
else:
raise ValueError(
"Either initialize the encoder with an spm_model or pre-tokenize and use the encode_sentences method."
Expand Down
13 changes: 13 additions & 0 deletions laser_encoders/test_laser_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def test_tokenize(tokenizer, input_text: str):
assert tokenizer.tokenize(input_text) == expected_output


def test_tokenizer_call_method(tokenizer, input_text: str):
single_string = "This is a test sentence."
expected_output = "▁this ▁is ▁a ▁test ▁sent ence ."
assert tokenizer(single_string) == expected_output

list_of_strings = ["This is a test sentence.", "This is another test sentence."]
expected_output = [
"▁this ▁is ▁a ▁test ▁sent ence .",
"▁this ▁is ▁another ▁test ▁sent ence .",
]
assert tokenizer(list_of_strings) == expected_output


def test_normalization(tokenizer):
test_data = "Hello!!! How are you??? I'm doing great."
expected_output = "▁hel lo !!! ▁how ▁are ▁you ??? ▁i ' m ▁do ing ▁great ."
Expand Down
Loading