Skip to content
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
13 changes: 1 addition & 12 deletions src/transformers/models/luke/tokenization_luke.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,7 @@ def _decode(
else self.clean_up_tokenization_spaces
)
if clean_up_tokenization_spaces:
text = (
text.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
text = self.clean_up_tokenization(text)

return text

Expand Down
24 changes: 0 additions & 24 deletions src/transformers/models/plbart/tokenization_plbart.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,30 +334,6 @@ def _convert_lang_code_special_format(self, lang: str) -> str:
lang = FAIRSEQ_LANGUAGE_CODES_MAP.get(lang, lang)
return lang

def clean_up_tokenization(self, out_string: str) -> str:
"""
Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms.

Args:
out_string (`str`): The text to clean up.

Returns:
`str`: The cleaned-up string.
"""
out_string = (
out_string.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
return out_string

def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=None, **kwargs):
"""Override to use self.clean_up_tokenization_spaces as default for batched input."""
return super().decode(
Expand Down
25 changes: 0 additions & 25 deletions src/transformers/models/wav2vec2/tokenization_wav2vec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,31 +356,6 @@ def convert_tokens_to_string(

return {"text": string, "char_offsets": char_offsets, "word_offsets": word_offsets}

@staticmethod
def clean_up_tokenization(out_string: str) -> str:
"""
Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms.

Args:
out_string (`str`): The text to clean up.

Returns:
`str`: The cleaned-up string.
"""
out_string = (
out_string.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
return out_string

@staticmethod
def _compute_offsets(char_repetitions: list[int], chars: list[str], ctc_token: int) -> list[dict[str, str | int]]:
end_indices = np.asarray(char_repetitions).cumsum()
Expand Down
18 changes: 1 addition & 17 deletions src/transformers/tokenization_mistral_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,23 +455,7 @@ def _decode(
else self.clean_up_tokenization_spaces
)
if clean_up_tokenization_spaces:
# Call custom cleanup method if it exists (e.g., for CLVP's [SPACE] token replacement)
if hasattr(self, "clean_up_tokenization") and callable(self.clean_up_tokenization):
text = self.clean_up_tokenization(text)
else:
# Otherwise apply standard cleanup
text = (
text.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
text = self.clean_up_tokenization(text)

return _maybe_remove_lang(text=text, skip_special_tokens=skip_special_tokens)

Expand Down
18 changes: 1 addition & 17 deletions src/transformers/tokenization_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,23 +1108,7 @@ def _decode(
else self.clean_up_tokenization_spaces
)
if clean_up_tokenization_spaces:
# Call custom cleanup method if it exists (e.g., for CLVP's [SPACE] token replacement)
if hasattr(self, "clean_up_tokenization") and callable(self.clean_up_tokenization):
text = self.clean_up_tokenization(text)
else:
# Otherwise apply standard cleanup
text = (
text.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
text = self.clean_up_tokenization(text)

return text

Expand Down
21 changes: 21 additions & 0 deletions src/transformers/tokenization_utils_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,27 @@ def _save_pretrained(

return file_names + vocab_files + (added_tokens_file,)

def clean_up_tokenization(self, text: str) -> str:
"""
Clean up tokenization spaces in a given text.
This method is mostly for remote code support.

"""

text = (
text.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
return text

def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str, ...]:
"""
Save only the vocabulary of the tokenizer (vocabulary + added tokens).
Expand Down
18 changes: 1 addition & 17 deletions src/transformers/tokenization_utils_tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,23 +939,7 @@ def _decode(
else self.clean_up_tokenization_spaces
)
if clean_up_tokenization_spaces:
# Call custom cleanup method if it exists
if hasattr(self, "clean_up_tokenization") and callable(self.clean_up_tokenization):
text = self.clean_up_tokenization(text)
else:
# Apply standard cleanup
text = (
text.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
)
text = self.clean_up_tokenization(text)

return text

Expand Down