Skip to main content

escape-unk

Escape unknown symbols in SentecePiece vocabularies. This is particulary useful for MarianNMT toolkit which does not support replacing unknown tokens with most attentive word in the source (see here, thanks to @emjotde for the idea).

IMPORTANT NOTE: this solution is far from ideal, as the model, especially if it has not been trained with escaped chars, may fail to copy the escaped unknown characters. Ideally, you should train your SentencePiece vocabulary with --byte_fallback option. This is just a workaround for scenarios where model does not have byte fallback or can not be re-trained.

Install

Just install it from PyPi

pip install escape-unk

Background

There are some scenarios where your machine translation model has to translate sentencences containing characters unknown for the SentencePiece vocabulary. Neural models usually start to hallucinate, throw out garbage or just don't know hot to translate when an unknown character comes to the input. In the cases where those characters simply need to be copied, escaping them to their hexadecimal representation, can be useful if the model manages to copy the escaped symbols.

Escape Chinese characters in an English-German vocabulary is just like:

echo "Beijing (Chinese: 北京) is the capital of the People's Republic of China" | escape-unk -m vocab.deen.spm
Beijing (Chinese: [[e58c97e4baac]]) is the capital of the People's Republic of China

or escaping emojis

echo "I ❤️ you" | escape-unk -m vocab.deen.spm
I [[e29da4efb88f]] you

So instead of:

echo "Beijing (Chinese: 北京) is the capital of the People's Republic of China" | marian-decoder -c model.config.yml
Peking (chinesisch: ) ist die Hauptstadt der Volksrepublik China

we will have:

echo "Beijing (Chinese: 北京) is the capital of the People's Republic of China" | escape-unk -m vocab.deen.spm | marian-decoder -c model.config.yml
Beijing (chinesisch: [[e58c97e4baac]]) ist die Hauptstadt der Volksrepublik China

and the full pipeline with unescape-unk:

echo "Beijing ..." | escape-unk -m vocab.deen.spm | marian-decode -c config.yml | unescape-unk
Beijing (chinesisch: 北京) ist die Hauptstadt der Volksrepublik China

WARNING: if an escaped sequence is not correctly copied by the translator and generates an invalid sequence, the character is omitted and substituted by an empty string. If you want it to fail when this happens, use --strict/-s mode with unescape-unk command.

Metadata

Release files for escape-unk 1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for escape-unk 1.6
File Size Uploaded Provenance
escape_unk-1.6.tar.gz 5.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for escape-unk 1.6
File Interpreter ABI Platform
escape_unk-1.6-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / escape_unk-1.6.tar.gz

Download URL escape_unk-1.6.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5e61f10d09ecaeafb82ceea40f1e87a1c81f3234502a50e8b76df6fdad7d236e
BLAKE2b-256 checksum
How to use checksums
783c601a59a8984e226211c245bff88b0796b061645f4d292381b114a928f553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / escape_unk-1.6-py3-none-any.whl

Download URL escape_unk-1.6-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9edad673385f3cce25491b16b32818563428b898fbf76608fc21e9d671ee3149
BLAKE2b-256 checksum
How to use checksums
47e4c5bf05519c75a450866e53a3972cd7bf0afb49437131662fd64e38c6f911
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Security for escape-unk 1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page