test_tokenization_transfo_xl.py 4 KB
Newer Older
1
# coding=utf-8
Sylvain Gugger's avatar
Sylvain Gugger committed
2
# Copyright 2020 The HuggingFace Team. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
#
# 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

16
17

import os
18
import unittest
19

Sylvain Gugger's avatar
Sylvain Gugger committed
20
from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer
21

22
from .test_tokenization_common import TokenizerTesterMixin
Aymeric Augustin's avatar
Aymeric Augustin committed
23
24


25
class TransfoXLTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
26

27
28
    tokenizer_class = TransfoXLTokenizer
    test_rust_tokenizer = False
29
30

    def setUp(self):
Julien Chaumond's avatar
Julien Chaumond committed
31
        super().setUp()
32
33

        vocab_tokens = [
34
35
36
37
38
39
40
41
42
43
44
            "<unk>",
            "[CLS]",
            "[SEP]",
            "want",
            "unwanted",
            "wa",
            "un",
            "running",
            ",",
            "low",
            "l",
45
        ]
46
47
        self.vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"])
        with open(self.vocab_file, "w", encoding="utf-8") as vocab_writer:
48
            vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))
49

50
    def get_tokenizer(self, **kwargs):
51
        kwargs["lower_case"] = True
52
        return TransfoXLTokenizer.from_pretrained(self.tmpdirname, **kwargs)
53

54
    def get_input_output_texts(self, tokenizer):
55
56
        input_text = "<unk> UNwanted , running"
        output_text = "<unk> unwanted, running"
57
        return input_text, output_text
58

59
60
    def test_full_tokenizer(self):
        tokenizer = TransfoXLTokenizer(vocab_file=self.vocab_file, lower_case=True)
61

62
        tokens = tokenizer.tokenize("<unk> UNwanted , running")
63
        self.assertListEqual(tokens, ["<unk>", "unwanted", ",", "running"])
64

65
        self.assertListEqual(tokenizer.convert_tokens_to_ids(tokens), [0, 4, 8, 7])
66

67
68
69
70
    def test_full_tokenizer_lower(self):
        tokenizer = TransfoXLTokenizer(lower_case=True)

        self.assertListEqual(
71
72
            tokenizer.tokenize(" \tHeLLo ! how  \n Are yoU ?  "), ["hello", "!", "how", "are", "you", "?"]
        )
73
74
75
76
77

    def test_full_tokenizer_no_lower(self):
        tokenizer = TransfoXLTokenizer(lower_case=False)

        self.assertListEqual(
78
79
            tokenizer.tokenize(" \tHeLLo ! how  \n Are yoU ?  "), ["HeLLo", "!", "how", "Are", "yoU", "?"]
        )
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    def test_full_tokenizer_moses_numbers(self):
        tokenizer = TransfoXLTokenizer(lower_case=False)
        text_in = "Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"
        tokens_out = [
            "Hello",
            "(",
            "bracket",
            ")",
            "and",
            "side",
            "@-@",
            "scrolled",
            "[",
            "and",
            "]",
            "Henry",
            "'s",
            "$",
            "5",
            "@,@",
            "000",
            "with",
            "3",
            "@.@",
            "34",
            "m",
            ".",
            "What",
            "'s",
            "up",
            "!",
            "?",
        ]

        self.assertListEqual(tokenizer.tokenize(text_in), tokens_out)

        self.assertEqual(tokenizer.convert_tokens_to_string(tokens_out), text_in)

119
120
121
122
123
124
125
126
127
128
129
130
    def test_move_added_token(self):
        tokenizer = self.get_tokenizer()
        original_len = len(tokenizer)

        tokenizer.add_tokens(["new1", "new2"])
        tokenizer.move_added_token("new1", 1)

        # Check that moved token is not copied (duplicate)
        self.assertEqual(len(tokenizer), original_len + 2)
        # Check that token is moved to specified id
        self.assertEqual(tokenizer.encode("new1"), [1])
        self.assertEqual(tokenizer.decode([1]), "new1")