Giter VIP home page Giter VIP logo

escape-unk's Introduction

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.

escape-unk's People

Contributors

zjaume avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.