File size: 601 Bytes
cc15101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""GPT-2 style pre-tokenization. Splits text into chunks (words with a

leading space, number runs, punctuation runs, whitespace) so BPE only

ever merges *within* a chunk, never across word boundaries."""

import regex as re

# The GPT-2 pattern. Order matters: contractions, spaced letters,
# spaced numbers, spaced punctuation, the whitespace.
GPT2_PATTERN = (
    r"""'s|'t|'re|'ve|'m|'ll|'d"""
    r"""| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
)


_PAT = re.compile(GPT2_PATTERN)

def split(text):
    """Return list of chunk strings."""
    return _PAT.findall(text)