Skip to content

Commit 84a97b3

Browse files
committed
docstrings, address reviewer comments
1 parent 275ed4d commit 84a97b3

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

model2vec/distill/distillation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def distill_from_model(
8080

8181
# Clean the vocabulary by removing duplicate tokens and tokens that are in the internal vocabulary.
8282
# Copy the original tokenizer model.
83-
tokenizer_model = original_tokenizer_model._deep_copy()
83+
tokenizer_model = original_tokenizer_model.model_copy(deep=True)
8484
if tokenizer_model.adds_prefix_space is not None:
8585
tokenizer_model.adds_prefix_space = True
8686

@@ -184,7 +184,7 @@ def _validate_parameters(
184184
try:
185185
token_remove_regex = re.compile(token_remove_pattern)
186186
except re.error as e:
187-
raise ValueError(f"Couldn't compile the regex pattern: {e}")
187+
raise ValueError(f"Couldn't compile the regex pattern: {e}") from e
188188

189189
return sif_coefficient, token_remove_regex
190190

model2vec/tokenizer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
importable("transformers", "tokenizer")
44

5-
from model2vec.tokenizer.tokenizer import (
5+
from model2vec.tokenizer.tokenizer import ( # noqa: E402
66
clean_and_create_vocabulary,
77
turn_tokens_into_ids,
88
)

model2vec/tokenizer/tokenizer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def clean_and_create_vocabulary(
1313
vocabulary_to_add: list[str],
1414
token_remove_regex: re.Pattern[str] | None,
1515
) -> TokenizerModel:
16-
"""Cleans a vocabulary by removing duplicates and tokens that were already in the vocabulary."""
16+
"""Clean a vocabulary by removing duplicates and tokens that were already in the vocabulary."""
1717
seen_tokens = set()
1818

1919
n_duplicate = 0
@@ -22,10 +22,9 @@ def clean_and_create_vocabulary(
2222

2323
internal_tokens: list[str] = model.sorted_vocabulary
2424
if token_remove_regex:
25-
len_before = len(internal_tokens)
2625
tokens_to_remove = [token for token in internal_tokens if token_remove_regex.match(token)]
2726
model = model.remove_tokens_from_vocabulary(tokens_to_remove)
28-
n_regex_removed = len_before - len(internal_tokens)
27+
n_regex_removed = len(tokens_to_remove)
2928
preprocessor = model.preprocessor
3029

3130
seen_tokens = set(internal_tokens)
@@ -38,7 +37,7 @@ def clean_and_create_vocabulary(
3837
n_empty += 1
3938
continue
4039
if len(preprocessed) > 1:
41-
tokens_as_str = [f"'{subword}'" for subword in token]
40+
tokens_as_str = [f"'{subword}'" for subword in preprocessed]
4241
split_into = ",".join(tokens_as_str)
4342
logger.warning(f"Token '{token}' was split into multiple tokens after preprocessing: [{split_into}]")
4443
added_tokens_to_add.append(token)
@@ -65,7 +64,7 @@ def clean_and_create_vocabulary(
6564

6665

6766
def _report_statistics(n_multiword: int, n_duplicate: int, n_regex_removed: int, n_empty: int) -> None:
68-
"""Helper function to avoid increasing complexity in main function."""
67+
"""Report statistics on the various types of issues we found."""
6968
if n_multiword:
7069
logger.info(f"Added {n_multiword} multi-word tokens to the vocabulary.")
7170
if n_duplicate:

0 commit comments

Comments
 (0)