Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Start, End index calculations fix for unicode characters. (#1171)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1171

The existing GPT2BPETokenizer incorrectly calculates the start and end indices for unicode characters.
This is because for multi-byte characters, we need to additionally use the byte decoder on the decoded bytes to get back the original token that was encoded.

Reviewed By: chenyangyu1988

Differential Revision: D18697646

fbshipit-source-id: 8f4d32a1caa40d8d06e7be31dfd4a6846692531a
  • Loading branch information
Debojeet Chatterjee authored and facebook-github-bot committed Nov 27, 2019
1 parent e261466 commit fb64c42
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pytext/data/tokenizers/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ def __init__(self, bpe: GPT2BPEEncoder):
def tokenize(self, input_str: str) -> List[Token]:
bpe_ids = self.bpe.encode(input_str)
char_tokens = [self.bpe.decoder[id].lstrip(u"\u0120") for id in bpe_ids]
# fix for incorrect decoding of utf-8 chars
char_tokens = [
bytearray([self.bpe.byte_decoder[char] for char in char_token]).decode(
"utf-8"
)
for char_token in char_tokens
]
lengths = [len(token) for token in char_tokens]
tokens = []
end = 0
Expand Down

0 comments on commit fb64c42

Please sign in to comment.