### Install Sacremoses Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Install the Sacremoses library using pip. Ensure you are using Python 3 for versions 0.0.41 and above. ```shell pip install -U sacremoses ``` -------------------------------- ### Sacremoses CLI Pipeline Example Source: https://github.com/hplt-project/sacremoses/blob/master/README.md An example of chaining multiple Sacremoses CLI commands (normalize, tokenize, truecase) to process a text file and save the output. It includes options for language, processes, and model training/loading. ```shell cat big.txt | sacremoses -l en -j 4 \ normalize -c tokenize -a truecase -a -m big.truemodel \ > big.txt.norm.tok.true ``` -------------------------------- ### Train and Use Truecaser (Python) Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows how to train a new truecaser model from a text file, save it, and then use it to truecase strings. It also demonstrates loading a pre-trained model and using different casing options. ```python from sacremoses import MosesTruecaser, MosesTokenizer # Train a new truecaser from a 'big.txt' file. mtr = MosesTruecaser() mtok = MosesTokenizer(lang='en') # Save the truecase model to 'big.truecasemodel' using `save_to` >> tokenized_docs = [mtok.tokenize(line) for line in open('big.txt')] # noqa mtr.train(tokenized_docs, save_to='big.truecasemodel') # Save the truecase model to 'big.truecasemodel' after training # (just in case you forgot to use `save_to`) mtr = MosesTruecaser() mtr.train('big.txt') mtr.save_model('big.truecasemodel') # Truecase a string after training a model. mtr = MosesTruecaser() mtr.train('big.txt') mtr.truecase("THE ADVENTURES OF SHERLOCK HOLMES") ['the', 'adventures', 'of', 'Sherlock', 'Holmes'] # Loads a model and truecase a string using trained model. mtr = MosesTruecaser('big.truecasemodel') mtr.truecase("THE ADVENTURES OF SHERLOCK HOLMES") ['the', 'adventures', 'of', 'Sherlock', 'Holmes'] mtr.truecase("THE ADVENTURES OF SHERLOCK HOLMES", use_known=True) ['the', 'ADVENTURES', 'OF', 'SHERLOCK', 'HOLMES'] mtr.truecase("THE ADVENTURES OF SHERLOCK HOLMES", return_str=True) 'the adventures of Sherlock Holmes' ``` -------------------------------- ### Display Normalize Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows the available options for the sacremoses normalize command. This helps in understanding various text normalization techniques. ```shell $ 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. ``` -------------------------------- ### Display Truecase Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows the available options for the sacremoses truecase command. This helps in understanding how to train and apply truecasing models. ```shell $ 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. ``` -------------------------------- ### Display Tokenizer Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows the available options for the sacremoses tokenize command. Use this to understand different tokenization strategies. ```shell $ 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. ``` -------------------------------- ### Sacremoses CLI Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Displays the help message for the Sacremoses command-line interface, showing available options and commands. ```shell $ sacremoses --help Usage: sacremoses [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]... Options: -l, --language TEXT Use language specific rules when tokenizing -j, --processes INTEGER No. of processes. -e, --encoding TEXT Specify encoding of file. -q, --quiet Disable progress bar. --version Show the version and exit. -h, --help Show this message and exit. Commands: detokenize detruecase normalize tokenize train-truecase truecase ``` -------------------------------- ### Display Detokenizer Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows the available options for the sacremoses detokenize command. Use this to understand how to revert tokenized text back to its original form. ```shell $ sacremoses detokenize --help Usage: sacremoses detokenize [OPTIONS] Options: -x, --xml-unescape Unescape special characters for XML. -h, --help Show this message and exit. ``` -------------------------------- ### Tokenize and Detokenize Text (Python) Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Demonstrates tokenizing text with special symbols and verifying the output. Also shows tokenizing and detokenizing a sentence with various punctuation and contractions. ```python from sacremoses import MosesTokenizer, MosesDetokenizer mt = MosesTokenizer(lang='en') text = 'This, is a sentence with weird\xbb symbols\u2026 appearing everywhere\xbf' expected_tokenized = 'This , is a sentence with weird \xbb symbols \u2026 appearing everywhere \xbf' tokenized_text = mt.tokenize(text, return_str=True) tokenized_text == expected_tokenized True ``` ```python mt, md = MosesTokenizer(lang='en'), MosesDetokenizer(lang='en') sent = "This ain't funny. It's actually hillarious, yet double Ls. | [] < > [ ] & You're gonna shake it off? Don't?" expected_tokens = ['This', 'ain', ''t', 'funny', '.', 'It', ''s', 'actually', 'hillarious', ',', 'yet', 'double', 'Ls', '.', '|', '[', ']', '<', '>', '[', ']', '&', 'You', ''re', 'gonna', 'shake', 'it', 'off', '?', 'Don', ''t', '?'] expected_detokens = "This ain't funny. It's actually hillarious, yet double Ls. | [] < > [] & You're gonna shake it off? Don't?" mt.tokenize(sent) == expected_tokens True md.detokenize(tokens) == expected_detokens True ``` -------------------------------- ### Display Detruecase Help Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Shows the available options for the sacremoses detruecase command. This helps in understanding how to revert truecasing. ```shell $ 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. ``` -------------------------------- ### Normalize Punctuation (Python) Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Demonstrates using the MosesPunctNormalizer to normalize punctuation in a given string. This is useful for standardizing text before further processing. ```python from sacremoses import MosesPunctNormalizer mpn = MosesPunctNormalizer() mpn.normalize('THIS EBOOK IS OTHERWISE PROVIDED TO YOU "AS-IS."') 'THIS EBOOK IS OTHERWISE PROVIDED TO YOU "AS-IS."' ``` -------------------------------- ### Apply Truecasing to Text Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Applies truecasing to a tokenized text file using a specified model file and displays progress. This is used to restore original casing to text. ```shell $ sacremoses -j 4 truecase -m big.model < big.txt.tok > big.txt.tok.true 100%|██████████████████████████████████| 128457/128457 [00:09<00:00, 14257.27it/s] ``` -------------------------------- ### Normalize Text Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Normalizes text, including quotations, commas, and numbers, and displays progress. This command is useful for standardizing text format. ```shell $ sacremoses -j 4 normalize < big.txt > big.txt.norm 100%|██████████████████████████████████| 128457/128457 [00:09<00:00, 13096.23it/s] ``` -------------------------------- ### Tokenize Text Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Tokenizes a large text file using default settings and displays progress. This is a common use case for preparing text for downstream NLP tasks. ```shell $ sacremoses -l en -j 4 tokenize < big.txt > big.txt.tok 100%|██████████████████████████████████| 128457/128457 [00:05<00:00, 24363.39it/s ``` -------------------------------- ### Tokenize Text with Protected Patterns Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Tokenizes text while protecting specific patterns defined in a file. This is useful for preserving entities like URLs or email addresses during tokenization. ```shell $ wget https://raw.githubusercontent.com/moses-smt/mosesdecoder/master/scripts/tokenizer/basic-protected-patterns $ sacremoses -l en -j 4 tokenize -p basic-protected-patterns < big.txt > big.txt.tok 100%|██████████████████████████████████| 128457/128457 [00:05<00:00, 22183.94it/s ``` -------------------------------- ### Detokenize Text Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Detokenizes a tokenized text file and displays progress. This command is used to reconstruct sentences or documents from tokenized input. ```shell $ sacremoses -l en -j 4 detokenize < big.txt.tok > big.txt.tok.detok 100%|██████████████████████████████████| 128457/128457 [00:16<00:00, 7931.26it/s] ``` -------------------------------- ### Detruecase Text Source: https://github.com/hplt-project/sacremoses/blob/master/README.md Removes truecasing from a text file and displays progress. This command is used to revert text that has undergone truecasing. ```shell $ sacremoses -j 4 detruecase < big.txt.tok.true > big.txt.tok.true.detrue 100%|█████████████████████████████████| 128457/128457 [00:04<00:00, 26945.16it/s] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.