Skip to content

Commit

Permalink
Do not use aggressive dash splitting in tokenization (mozilla#718)
Browse files Browse the repository at this point in the history
* Do not use aggressive dash splitting in tokenization

* Add another use case with dash

* Add more test cases
  • Loading branch information
eu9ene committed Aug 29, 2024
1 parent 78408b8 commit ffe461f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pipeline/alignments/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ def _tokenize_lines(params) -> List[str]:
from mosestokenizer import MosesTokenizer

try:
# Use aggressive dash splitting to reduce vocabulary size
tokenizer = MosesTokenizer(lang, aggressive_dash_splits=True)
tokenizer = MosesTokenizer(lang)
except RuntimeError as err:
msg = str(err)
if "No known abbreviations for language" in msg:
# Fall-back to English if the language is not found
tokenizer = MosesTokenizer("en", aggressive_dash_splits=True)
tokenizer = MosesTokenizer("en")
else:
raise err

Expand Down
4 changes: 4 additions & 0 deletions tests/test_aln_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
("Hi", {0: 0}),
("Hello, world!", {0: 0, 1: 0, 2: 1, 3: 1}),
("Hello, world!", {0: 0, 1: 0, 2: 1, 3: 1}),
("Hello, half-world and welcome!", {0: 0, 1: 0, 2: 1, 3: 2, 4: 3, 5: 3}),
("Hello - world!", {0: 0, 1: 1, 2: 2, 3: 2}),
("Hello,- world!", {0: 0, 1: 0, 2: 0, 3: 1, 4: 1}),
(
"“I will not,” retorted the Witch, “for it is now my shoe, and not yours.”",
{
Expand Down Expand Up @@ -48,6 +51,7 @@ def test_remap_indices(orig, expected_idx_map):
"""
tokenized = tokenizer.tokenize(orig)
tokenized_str = " ".join(tokenized)
print(tokenized_str)

idx_map = map_indices(tokenized_str, orig)

Expand Down

0 comments on commit ffe461f

Please sign in to comment.