Install sacremoses via pip
masterInstall the latest version of sacremoses using pip. Note that sacremoses requires Python 3 (version 0.0.41 or higher). If you are using Python 2, you must use version 0.0.40.
pip install -U sacremosesrepository·master·Indexed 19 days ago
https://github.com/hplt-project/sacremosesA multilingual NLP library providing tools for text preprocessing, including tokenization, detokenization, truecasing, and punctuation normalization. It features a Python API with classes like MosesTokenizer, MosesDetokenizer, MosesTruecaser, and MosesPunctNormalizer, as well as a CLI that supports processing pipelines for tasks such as tokenize, detokenize, truecase, detruecase, and normalize.
Install the latest version of sacremoses using pip. Note that sacremoses requires Python 3 (version 0.0.41 or higher). If you are using Python 2, you must use version 0.0.40.
pip install -U sacremosesThe Sacremoses CLI allows you to perform tokenization, detokenization, normalization, and truecasing via command-line pipes. The CLI accepts input from stdin and outputs the processed text to stdout.
Global options apply to all commands:
--language, -l: Specify the language (default: en).--processes, -j: Number of processes for parallel execution (default: 1).--encoding, -e: Specify file encoding (default: utf8).--quiet, -q: Disable the progress bar.# Example: Tokenize an English file using 4 processes
cat input.txt | sacremoses -l en -j 4 tokenize > output.txtMosesPunctNormalizer can be used to normalize punctuation within a string.
from sacremoses import MosesPunctNormalizer
mpn = MosesPunctNormalizer()
normalized = mpn.normalize('TEXT TO NORMALIZE')from sacremoses import MosesPunctNormalizer
mpn = MosesPunctNormalizer()
mpn.normalize('THIS EBOOK IS OTHERWISE PROVIDED TO YOU "AS-IS."')Use MosesTokenizer to split text into tokens and MosesDetokenizer to reconstruct text from tokens. You can specify the language using the lang parameter.
MosesTokenizer.tokenize(text, return_str=True) returns the tokenized text as a string. Without return_str=True, it returns a list of tokens.
MosesDetokenizer.detokenize(tokens) takes a list of tokens and returns the reconstructed string.
from sacremoses import MosesTokenizer, MosesDetokenizer
# Tokenization
mt = MosesTokenizer(lang='en')
text = 'This, is a sentence with weird\xbb symbols\u2026 appearing everywhere\xbf'
tokenized_text = mt.tokenize(text, return_str=True)
# Detokenization
md = MosesDetokenizer(lang='en')
tokens = ['This', 'is', 'a', 'sentence']
detokenized_text = md.detokenize(tokens)MosesTruecaser allows you to train a model to correctly capitalize text (truecasing) and then apply that model to strings.
You can train a model by passing tokenized documents to .train(). You can save the model during training using the save_to argument, or save it after training using .save_model(path).
MosesTruecaser('path/to/model')..truecase(text): Returns a list of truecased tokens..truecase(text, return_str=True): Returns the truecased text as a string..truecase(text, use_known=True): Uses known words to assist truecasing.from sacremoses import MosesTruecaser, MosesTokenizer
# Training and saving
mtr = MosesTruecaser()
mtok = MosesTokenizer(lang='en')
tokenized_docs = [mtok.tokenize(line) for line in open('big.txt')]
mtr.train(tokenized_docs, save_to='big.truecasemodel')
# Using a trained model
mtr = MosesTruecaser('big.truecasemodel')
result = mtr.truecase("THE ADVENTURES OF SHERLOCK HOLMES", return_str=True)
# Output: 'the adventures of Sherlock Holmes'Options for the truecase command:
-m, --modelfile TEXT: Filename to save/load the modelfile. [required]-a, --is-asr: A flag to indicate that model is for ASR.-p, --possibly-use-first-token: Use the first token as part of truecase training.-h, --help: Show this message and exit.$ sacremoses truecase --help
Usage: sacremoses truecase [OPTIONS]
Options:
-m, --modelfile TEXT Filename to save/load the modelfile.
[required]
-a, --is-asr A flag to indicate that model is for ASR.
-p, --possibly-use-first-token Use the first token as part of truecase
training.
-h, --help Show this message and exit.Options for the tokenize command:
-a, --aggressive-dash-splits: Triggers dash split rules.-x, --xml-escape: Escape special characters for XML.-p, --protected-patterns TEXT: Specify file with patterns to be protected in tokenisation.-c, --custom-nb-prefixes TEXT: Specify a custom non-breaking prefixes file.-h, --help: Show this message and exit.$ sacremoses tokenize --help
Usage: sacremoses tokenize [OPTIONS]
Options:
-a, --aggressive-dash-splits Triggers dash split rules.
-x, --xml-escape Escape special characters for XML.
-p, --protected-patterns TEXT Specify file with patters to be protected in
tokenisation.
-c, --custom-nb-prefixes TEXT Specify a custom non-breaking prefixes file,
add prefixes to the default ones from the
specified language.
-h, --help Show this message and exit.Options for the detokenize command:
-x, --xml-unescape: Unescape special characters for XML.-h, --help: Show this message and exit.$ sacremoses detokenize --help
Usage: sacremoses detokenize [OPTIONS]
Options:
-x, --xml-unescape Unescape special characters for XML.
-h, --help Show this message and exit.Options for the detruecase command:
-j, --processes INTEGER: Number of processes.-a, --is-headline: Whether the file are headlines.-e, --encoding TEXT: Specify encoding of file.-h, --help: Show this message and exit.$ sacremoses detruecase --help
Usage: sacremoses detruecase [OPTIONS]
Options:
-j, --processes INTEGER No. of processes.
-a, --is-headline Whether the file are headlines.
-e, --encoding TEXT Specify encoding of file.
-h, --help Show this message and exit.Options for the normalize command:
-q, --normalize-quote-commas: Normalize quotations and commas.-d, --normalize-numbers: Normalize number.-p, --replace-unicode-puncts: Replace unicode punctuations BEFORE normalization.-c, --remove-control-chars: Remove control characters AFTER normalization.-h, --help: Show this message and exit.$ sacremoses normalize --help
Usage: sacremoses normalize [OPTIONS]
Options:
-q, --normalize-quote-commas Normalize quotations and commas.
-d, --normalize-numbers Normalize number.
-p, --replace-unicode-puncts Replace unicode punctuations BEFORE
normalization.
-c, --remove-control-chars Remove control characters AFTER normalization.
-h, --help Show this message and exit.The sacremoses CLI allows you to run NLP tasks via the command line. Since version 0.0.42, the CLI supports a pipeline feature where global options are set before the command.
-l, --language TEXT: Use language specific rules.-j, --processes INTEGER: Number of processes.-e, --encoding TEXT: Specify file encoding.-q, --quiet: Disable progress bar.tokenize: Tokenize text.detokenize: Detokenize tokens.truecase: Apply truecasing.detruecase: Reverse truecasing.normalize: Normalize text.train-truecase: Train a truecase model.You can chain commands together in a single execution:
cat big.txt | sacremoses -l en -j 4 \
normalize -c tokenize -a truecase -a -m big.truemodel \
> big.txt.norm.tok.trueThe detokenize command reconstructs text from tokens. It expects input where tokens are space-separated.
Options:
--xml-unescape, -x: Unescape special characters for XML (default: True).cat tokens.txt | sacremoses detokenize > output.txt