-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocabulary.py
157 lines (136 loc) · 5.12 KB
/
vocabulary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
77
78
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import re
import unittest
from typing import List, Optional
class Vocabulary:
BOS = "BOS"
EOS = "EOS"
PAD = "PAD"
def __init__(self, list_of_sentences: Optional[List[str]]):
self.token2index = {self.BOS: 0, self.EOS: 1, self.PAD: 2}
self.index2token = {v: k for k, v in self.token2index.items()}
if not list_of_sentences:
return
for sentence in list_of_sentences:
self.add_tokens(self.tokenize(sentence))
def add_tokens(self, tokens: List[str]) -> None:
"""
Adds tokens the vocabulary
:param tokens:
:return: None
"""
for token in tokens:
if token not in self.token2index:
i = len(self.token2index.items())
self.token2index[token] = i
self.index2token[i] = token
def tokenize(self, sentence: str, add_special_tokens: bool = True) -> List[str]:
"""
Split on all tokens and punctuation. Optionally adds BOS and EOS tokens.
:param sentence:
:param add_special_tokens:
:return: List of string tokens
"""
tokens = re.findall(r"\w+|[^\s\w]+", sentence)
if add_special_tokens:
tokens = [self.BOS] + tokens + [self.EOS]
return tokens
def encode(self, sentence: str, add_special_tokens: bool = True) -> List[int]:
"""
Converts a string to a list of token indices given the vocabulary
:param sentence: a string representing a sentence
:param add_special_tokens: whether or not to add a bos and eos token
:return: list of token indices
"""
tokens = self.tokenize(sentence, add_special_tokens)
return [self.token2index[token] for token in tokens]
def batch_encode(
self, sentences: List[str], padding=True, add_special_tokens: bool = False
) -> List[List[int]]:
"""
Convert a list of string sentences to nested list of token indices. Optionally adds padding & bos+eos tokens
:param sentences: A list of sentences to be encoded into a batch
:param padding: Boolean that allows for padding up to the longest sequence in the batch
:param add_special_tokens: Boolean that allows for adding a BOS and EOS token to each sentence in the batch
:return: nested list of tokenized sequences
"""
tokenized_sentences = [
self.encode(sentence, add_special_tokens) for sentence in sentences
]
if padding:
max_length = max([len(tokens) for tokens in tokenized_sentences])
tokenized_sentences = [
s + ((max_length - len(s)) * [self.token2index[self.PAD]])
for s in tokenized_sentences
]
return tokenized_sentences
class TestVocabulary(unittest.TestCase):
maxDiff = None
def test_tokenize(self):
input_sequence = "Hello my name is Joris and I was born with the name Joris."
output = Vocabulary([]).tokenize(input_sequence)
self.assertEqual(
[
"BOS",
"Hello",
"my",
"name",
"is",
"Joris",
"and",
"I",
"was",
"born",
"with",
"the",
"name",
"Joris",
".",
"EOS",
],
output,
)
def test_init_vocab(self):
input_sentences = ["Hello my name is Joris and I was born with the name Joris."]
vocab = Vocabulary(input_sentences)
expected = {
"BOS": 0,
"EOS": 1,
"PAD": 2,
"Hello": 3,
"my": 4,
"name": 5,
"is": 6,
"Joris": 7,
"and": 8,
"I": 9,
"was": 10,
"born": 11,
"with": 12,
"the": 13,
".": 14,
}
self.assertEqual(vocab.token2index, expected)
def test_encode(self):
input_sentences = ["Hello my name is Joris and I was born with the name Joris."]
vocab = Vocabulary(input_sentences)
output = vocab.encode(input_sentences[0])
self.assertEqual(output, [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 7, 14, 1])
def test_encode_no_special_tokens(self):
input_sentences = ["Hello my name is Joris and I was born with the name Joris."]
vocab = Vocabulary(input_sentences)
output = vocab.encode(input_sentences[0], add_special_tokens=False)
self.assertEqual(output, [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 7, 14])
def test_batch_encode(self):
input_sentences = [
"This is one sentence",
"This is another, much longer sentence",
"Short sentence",
]
vocab = Vocabulary(input_sentences)
output = vocab.batch_encode(input_sentences, add_special_tokens=False)
self.assertEqual(
output,
[[3, 4, 5, 6, 2, 2, 2], [3, 4, 7, 8, 9, 10, 6], [11, 6, 2, 2, 2, 2, 2]],
)
if __name__ == "__main__":
unittest.main()