tokenization_herbert.py 2.84 KB
Newer Older
rmroczkowski's avatar
rmroczkowski committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding=utf-8
# Copyright 2020 The Google AI Language Team Authors, Allegro.pl, Facebook Inc. and the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Sylvain Gugger's avatar
Sylvain Gugger committed
16
17
18
from ...utils import logging
from ..bert.tokenization_bert import BasicTokenizer
from ..xlm.tokenization_xlm import XLMTokenizer
rmroczkowski's avatar
rmroczkowski committed
19
20
21
22
23
24
25
26
27


logger = logging.get_logger(__name__)

VOCAB_FILES_NAMES = {
    "vocab_file": "vocab.json",
    "merges_file": "merges.txt",
}

28
PRETRAINED_VOCAB_FILES_MAP = {
29
30
    "vocab_file": {"allegro/herbert-base-cased": "https://huggingface.co/allegro/herbert-base-cased/vocab.json"},
    "merges_file": {"allegro/herbert-base-cased": "https://huggingface.co/allegro/herbert-base-cased/merges.txt"},
31
32
33
34
35
}

PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {"allegro/herbert-base-cased": 514}
PRETRAINED_INIT_CONFIGURATION = {}

rmroczkowski's avatar
rmroczkowski committed
36
37
38
39
40
41
42

class HerbertTokenizer(XLMTokenizer):
    """
    Construct a BPE tokenizer for HerBERT.

    Peculiarities:

43
    - uses BERT's pre-tokenizer: BaseTokenizer splits tokens on spaces, and also on punctuation. Each occurrence of a
Sylvain Gugger's avatar
Sylvain Gugger committed
44
      punctuation character will be treated separately.
rmroczkowski's avatar
rmroczkowski committed
45
46
47

    - Such pretokenized input is BPE subtokenized

Sylvain Gugger's avatar
Sylvain Gugger committed
48
49
    This tokenizer inherits from :class:`~transformers.XLMTokenizer` which contains most of the methods. Users should
    refer to the superclass for more information regarding methods.
rmroczkowski's avatar
rmroczkowski committed
50
51
52
    """

    vocab_files_names = VOCAB_FILES_NAMES
53
54
55
    pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
    pretrained_init_configuration = PRETRAINED_INIT_CONFIGURATION
    max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
rmroczkowski's avatar
rmroczkowski committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    def __init__(self, **kwargs):

        kwargs["cls_token"] = "<s>"
        kwargs["unk_token"] = "<unk>"
        kwargs["pad_token"] = "<pad>"
        kwargs["mask_token"] = "<mask>"
        kwargs["sep_token"] = "</s>"
        kwargs["do_lowercase_and_remove_accent"] = False
        kwargs["additional_special_tokens"] = []

        super().__init__(**kwargs)
        self.bert_pre_tokenizer = BasicTokenizer(
            do_lower_case=False, never_split=self.all_special_tokens, tokenize_chinese_chars=False, strip_accents=False
        )

    def _tokenize(self, text):

        pre_tokens = self.bert_pre_tokenizer.tokenize(text)

        split_tokens = []
        for token in pre_tokens:
            if token:
                split_tokens.extend([t for t in self.bpe(token).split(" ")])

        return split_tokens