test_tokenization_t5.py 3.22 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2018 Google T5 Authors and 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.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

thomwolf's avatar
thomwolf committed
16
17

import os
18
import unittest
thomwolf's avatar
thomwolf committed
19

20
from transformers.tokenization_t5 import T5Tokenizer
21
from transformers.tokenization_xlnet import SPIECE_UNDERLINE
thomwolf's avatar
thomwolf committed
22

23
from .test_tokenization_common import TokenizerTesterMixin
thomwolf's avatar
thomwolf committed
24

Aymeric Augustin's avatar
Aymeric Augustin committed
25

26
27
SAMPLE_VOCAB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/test_sentencepiece.model")

thomwolf's avatar
thomwolf committed
28

29
class T5TokenizationTest(TokenizerTesterMixin, unittest.TestCase):
thomwolf's avatar
thomwolf committed
30
31
32
33

    tokenizer_class = T5Tokenizer

    def setUp(self):
Julien Chaumond's avatar
Julien Chaumond committed
34
        super().setUp()
thomwolf's avatar
thomwolf committed
35
36

        # We have a SentencePiece fixture for testing
37
        tokenizer = T5Tokenizer(SAMPLE_VOCAB)
thomwolf's avatar
thomwolf committed
38
39
40
        tokenizer.save_pretrained(self.tmpdirname)

    def test_full_tokenizer(self):
41
        tokenizer = T5Tokenizer(SAMPLE_VOCAB)
thomwolf's avatar
thomwolf committed
42

43
44
        tokens = tokenizer.tokenize("This is a test")
        self.assertListEqual(tokens, ["鈻乀his", "鈻乮s", "鈻乤", "鈻乼", "est"])
thomwolf's avatar
thomwolf committed
45

46
        self.assertListEqual(tokenizer.convert_tokens_to_ids(tokens), [285, 46, 10, 170, 382])
thomwolf's avatar
thomwolf committed
47

48
        tokens = tokenizer.tokenize("I was born in 92000, and this is fals茅.")
thomwolf's avatar
thomwolf committed
49
        self.assertListEqual(
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
            tokens,
            [
                SPIECE_UNDERLINE + "I",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "9",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "s",
                "茅",
                ".",
            ],
        )
        ids = tokenizer.convert_tokens_to_ids(tokens)
        self.assertListEqual(ids, [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4])
thomwolf's avatar
thomwolf committed
77
78

        back_tokens = tokenizer.convert_ids_to_tokens(ids)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        self.assertListEqual(
            back_tokens,
            [
                SPIECE_UNDERLINE + "I",
                SPIECE_UNDERLINE + "was",
                SPIECE_UNDERLINE + "b",
                "or",
                "n",
                SPIECE_UNDERLINE + "in",
                SPIECE_UNDERLINE + "",
                "<unk>",
                "2",
                "0",
                "0",
                "0",
                ",",
                SPIECE_UNDERLINE + "and",
                SPIECE_UNDERLINE + "this",
                SPIECE_UNDERLINE + "is",
                SPIECE_UNDERLINE + "f",
                "al",
                "s",
                "<unk>",
                ".",
            ],
        )