Repository navigation
Expand file tree
/
Copy pathsentencepiece_test.py
More file actions
executable file
·383 lines (339 loc) · 14.4 KB
/
Copy pathsentencepiece_test.py
File metadata and controls
executable file
·383 lines (339 loc) · 14.4 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2018 Google Inc.
#
# 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.!
import codecs
import io
import sentencepiece as spm
import unittest
import sys
import os
import pickle
from collections import defaultdict
data_dir = 'test'
if sys.platform == 'win32':
data_dir = os.path.join('..', 'data')
class TestSentencepieceProcessor(unittest.TestCase):
"""Test case for SentencePieceProcessor"""
def setUp(self):
self.sp_ = spm.SentencePieceProcessor()
self.jasp_ = spm.SentencePieceProcessor()
self.assertTrue(self.sp_.Load(os.path.join('test', 'test_model.model')))
self.assertTrue(
self.jasp_.Load(os.path.join('test', 'test_ja_model.model')))
with open(os.path.join('test', 'test_model.model'), 'rb') as f:
self.assertTrue(self.sp_.LoadFromSerializedProto(f.read()))
with open(os.path.join('test', 'test_ja_model.model'), 'rb') as f:
self.assertTrue(self.jasp_.LoadFromSerializedProto(f.read()))
def test_load(self):
self.assertEqual(1000, self.sp_.GetPieceSize())
self.assertEqual(0, self.sp_.PieceToId('<unk>'))
self.assertEqual(1, self.sp_.PieceToId('<s>'))
self.assertEqual(2, self.sp_.PieceToId('</s>'))
self.assertEqual('<unk>', self.sp_.IdToPiece(0))
self.assertEqual('<s>', self.sp_.IdToPiece(1))
self.assertEqual('</s>', self.sp_.IdToPiece(2))
self.assertEqual(0, self.sp_.unk_id())
self.assertEqual(1, self.sp_.bos_id())
self.assertEqual(2, self.sp_.eos_id())
self.assertEqual(-1, self.sp_.pad_id())
for i in range(self.sp_.GetPieceSize()):
piece = self.sp_.IdToPiece(i)
self.assertEqual(i, self.sp_.PieceToId(piece))
def test_roundtrip(self):
text = 'I saw a girl with a telescope.'
ids = self.sp_.EncodeAsIds(text)
pieces1 = self.sp_.EncodeAsPieces(text)
pieces2 = self.sp_.NBestEncodeAsPieces(text, 10)[0]
self.assertEqual(pieces1, pieces2)
self.assertEqual(text, self.sp_.DecodePieces(pieces1))
self.assertEqual(text, self.sp_.DecodeIds(ids))
for n in range(100):
self.assertEqual(
text,
self.sp_.DecodePieces(self.sp_.SampleEncodeAsPieces(text, 64, 0.5)))
self.assertEqual(
text,
self.sp_.DecodePieces(self.sp_.SampleEncodeAsPieces(text, -1, 0.5)))
self.assertEqual(
text, self.sp_.DecodeIds(self.sp_.SampleEncodeAsIds(text, 64, 0.5)))
self.assertEqual(
text, self.sp_.DecodeIds(self.sp_.SampleEncodeAsIds(text, -1, 0.5)))
def test_ja_load(self):
self.assertEqual(8000, self.jasp_.GetPieceSize())
self.assertEqual(0, self.jasp_.PieceToId('<unk>'))
self.assertEqual(1, self.jasp_.PieceToId('<s>'))
self.assertEqual(2, self.jasp_.PieceToId('</s>'))
self.assertEqual('<unk>', self.jasp_.IdToPiece(0))
self.assertEqual('<s>', self.jasp_.IdToPiece(1))
self.assertEqual('</s>', self.jasp_.IdToPiece(2))
for i in range(self.jasp_.GetPieceSize()):
piece = self.jasp_.IdToPiece(i)
self.assertEqual(i, self.jasp_.PieceToId(piece))
def test_ja_roundtrip(self):
text = '清水寺は京都にある。'
ids = self.jasp_.EncodeAsIds(text)
pieces1 = self.jasp_.EncodeAsPieces(text)
pieces2 = self.jasp_.NBestEncodeAsPieces(text, 10)[0]
self.assertEqual(pieces1, pieces2)
self.assertEqual(text, self.jasp_.DecodePieces(pieces1))
self.assertEqual(text, self.jasp_.DecodeIds(ids))
for n in range(100):
self.assertEqual(
text,
self.jasp_.DecodePieces(
self.jasp_.SampleEncodeAsPieces(text, 64, 0.5)))
self.assertEqual(
text,
self.jasp_.DecodePieces(
self.jasp_.SampleEncodeAsPieces(text, -1, 0.5)))
def test_unicode_roundtrip(self):
text = u'I saw a girl with a telescope.'
ids = self.sp_.EncodeAsIds(text)
pieces = self.sp_.EncodeAsPieces(text)
self.assertEqual(text, self.sp_.DecodePieces(pieces))
self.assertEqual(text, self.sp_.DecodeIds(ids))
# python2 returns `str`.
if sys.version_info < (3, 0, 0):
text = text.encode('utf-8')
self.assertEqual(text, self.sp_.DecodeIds(ids))
self.assertEqual(text, self.sp_.DecodePieces(pieces))
def test_unicode_ja_roundtrip(self):
text = u'清水寺は京都にある。'
ids = self.jasp_.EncodeAsIds(text)
pieces = self.jasp_.EncodeAsPieces(text)
self.assertEqual(text, self.jasp_.DecodePieces(pieces))
# python2 returns `str`.
if sys.version_info < (3, 0, 0):
text = text.encode('utf-8')
self.assertEqual(text, self.jasp_.DecodeIds(ids))
def test_pickle(self):
with open('sp.pickle', 'wb') as f:
pickle.dump(self.sp_, f)
id1 = self.sp_.encode('hello world.', out_type=int)
with open('sp.pickle', 'rb') as f:
sp = pickle.load(f)
id2 = sp.encode('hello world.', out_type=int)
self.assertEqual(id1, id2)
def test_train(self):
spm.SentencePieceTrainer.Train('--input=' +
os.path.join(data_dir, 'botchan.txt') +
' --model_prefix=m --vocab_size=1000')
sp = spm.SentencePieceProcessor()
sp.Load('m.model')
with codecs.open(
os.path.join(data_dir, 'botchan.txt'), 'r', encoding='utf-8') as file:
for line in file:
sp.DecodePieces(sp.EncodeAsPieces(line))
sp.DecodeIds(sp.EncodeAsIds(line))
def test_train(self):
spm.SentencePieceTrainer.Train('--input=' +
os.path.join(data_dir, 'botchan.txt') +
' --model_prefix=m --vocab_size=1000')
# Load as 'rb' for Python3.5/2.7.
is1 = open(os.path.join(data_dir, 'botchan.txt'), 'rb')
is2 = open(os.path.join(data_dir, 'botchan.txt'), 'rb')
os1 = io.BytesIO()
os2 = io.BytesIO()
spm.SentencePieceTrainer.train(
input=os.path.join(data_dir, 'botchan.txt'),
model_prefix='m',
vocab_size=1000)
spm.SentencePieceTrainer.train(
sentence_iterator=is1, model_prefix='m', vocab_size=1000)
spm.SentencePieceTrainer.train(
input=os.path.join(data_dir, 'botchan.txt'),
model_writer=os1,
vocab_size=1000)
spm.SentencePieceTrainer.train(
sentence_iterator=is2, model_writer=os2, vocab_size=1000)
sp1 = spm.SentencePieceProcessor(model_proto=os1.getvalue())
sp2 = spm.SentencePieceProcessor(model_proto=os2.getvalue())
self.assertEqual([sp1.id_to_piece(i) for i in range(sp1.get_piece_size())],
[sp2.id_to_piece(i) for i in range(sp2.get_piece_size())])
def test_train_kwargs(self):
spm.SentencePieceTrainer.train(
input=[os.path.join(data_dir, 'botchan.txt')],
model_prefix='m',
vocab_size=1002,
user_defined_symbols=['foo', 'bar', ','])
sp = spm.SentencePieceProcessor()
sp.Load('m.model')
with codecs.open(
os.path.join(data_dir, 'botchan.txt'), 'r', encoding='utf-8') as file:
for line in file:
sp.DecodePieces(sp.EncodeAsPieces(line))
sp.DecodeIds(sp.EncodeAsIds(line))
# snake case API.
def test_load_snake(self):
self.assertEqual(1000, self.sp_.get_piece_size())
self.assertEqual(0, self.sp_.piece_to_id('<unk>'))
self.assertEqual(1, self.sp_.piece_to_id('<s>'))
self.assertEqual(2, self.sp_.piece_to_id('</s>'))
self.assertEqual('<unk>', self.sp_.id_to_piece(0))
self.assertEqual('<s>', self.sp_.id_to_piece(1))
self.assertEqual('</s>', self.sp_.id_to_piece(2))
for i in range(self.sp_.get_piece_size()):
piece = self.sp_.id_to_piece(i)
self.assertEqual(i, self.sp_.piece_to_id(piece))
def test_roundtrip_snake(self):
text = 'I saw a girl with a telescope.'
ids = self.sp_.encode_as_ids(text)
pieces1 = self.sp_.encode_as_pieces(text)
pieces2 = self.sp_.nbest_encode_as_pieces(text, 10)[0]
self.assertEqual(pieces1, pieces2)
self.assertEqual(text, self.sp_.decode_pieces(pieces1))
self.assertEqual(text, self.sp_.decode_ids(ids))
for n in range(100):
self.assertEqual(
text,
self.sp_.decode_pieces(
self.sp_.sample_encode_as_pieces(text, 64, 0.5)))
self.assertEqual(
text,
self.sp_.decode_pieces(
self.sp_.sample_encode_as_pieces(text, -1, 0.5)))
self.assertEqual(
text,
self.sp_.decode_ids(self.sp_.sample_encode_as_ids(text, 64, 0.5)))
self.assertEqual(
text,
self.sp_.decode_ids(self.sp_.sample_encode_as_ids(text, -1, 0.5)))
def test_ja_load_snake(self):
self.assertEqual(8000, self.jasp_.get_piece_size())
self.assertEqual(0, self.jasp_.piece_to_id('<unk>'))
self.assertEqual(1, self.jasp_.piece_to_id('<s>'))
self.assertEqual(2, self.jasp_.piece_to_id('</s>'))
self.assertEqual('<unk>', self.jasp_.id_to_piece(0))
self.assertEqual('<s>', self.jasp_.id_to_piece(1))
self.assertEqual('</s>', self.jasp_.id_to_piece(2))
for i in range(self.jasp_.get_piece_size()):
piece = self.jasp_.id_to_piece(i)
self.assertEqual(i, self.jasp_.piece_to_id(piece))
def test_ja_roundtrip_snake(self):
text = '清水寺は京都にある。'
ids = self.jasp_.encode_as_ids(text)
pieces1 = self.jasp_.encode_as_pieces(text)
pieces2 = self.jasp_.nbest_encode_as_pieces(text, 10)[0]
self.assertEqual(pieces1, pieces2)
self.assertEqual(text, self.jasp_.decode_pieces(pieces1))
self.assertEqual(text, self.jasp_.decode_ids(ids))
for n in range(100):
self.assertEqual(
text,
self.jasp_.decode_pieces(
self.jasp_.sample_encode_as_pieces(text, 64, 0.5)))
self.assertEqual(
text,
self.jasp_.decode_pieces(
self.jasp_.sample_encode_as_pieces(text, -1, 0.5)))
def test_unicode_roundtrip_snake(self):
text = u'I saw a girl with a telescope.'
ids = self.sp_.encode_as_ids(text)
pieces = self.sp_.encode_as_pieces(text)
self.assertEqual(text, self.sp_.decode_pieces(pieces))
# python2 returns `str`.
if sys.version_info < (3, 0, 0):
text = text.encode('utf-8')
self.assertEqual(text, self.sp_.decode_ids(ids))
def test_unicode_ja_roundtrip_snake(self):
text = u'清水寺は京都にある。'
ids = self.jasp_.encode_as_ids(text)
pieces = self.jasp_.encode_as_pieces(text)
self.assertEqual(text, self.jasp_.decode_pieces(pieces))
# python2 returns `str`.
if sys.version_info < (3, 0, 0):
text = text.encode('utf-8')
self.assertEqual(text, self.jasp_.decode_ids(ids))
def test_train_snake(self):
spm.SentencePieceTrainer.train('--input=' +
os.path.join(data_dir, 'botchan.txt') +
' --model_prefix=m --vocab_size=1000')
sp = spm.SentencePieceProcessor()
sp.load('m.model')
with codecs.open(
os.path.join(data_dir, 'botchan.txt'), 'r', encoding='utf-8') as file:
for line in file:
sp.decode_pieces(sp.encode_as_pieces(line))
sp.decode_ids(sp.encode_as_ids(line))
def test_serialized_proto(self):
text = u'I saw a girl with a telescope.'
self.assertNotEqual('', self.sp_.EncodeAsSerializedProto(text))
self.assertNotEqual('',
self.sp_.SampleEncodeAsSerializedProto(text, 10, 0.2))
self.assertNotEqual('', self.sp_.NBestEncodeAsSerializedProto(text, 10))
self.assertNotEqual('',
self.sp_.DecodePiecesAsSerializedProto(['foo', 'bar']))
self.assertNotEqual('', self.sp_.DecodeIdsAsSerializedProto([20, 30]))
self.assertNotEqual('', self.sp_.encode_as_serialized_proto(text))
self.assertNotEqual(
'', self.sp_.sample_encode_as_serialized_proto(text, 10, 0.2))
self.assertNotEqual('', self.sp_.nbest_encode_as_serialized_proto(text, 10))
self.assertNotEqual(
'', self.sp_.decode_pieces_as_serialized_proto(['foo', 'bar']))
self.assertNotEqual('', self.sp_.decode_ids_as_serialized_proto([20, 30]))
def test_new_api(self):
sp = spm.SentencePieceProcessor(
model_file=os.path.join('test', 'test_model.model'))
text = 'hello world'
text2 = 'Tokyo'
ids = self.sp_.EncodeAsIds(text)
ids2 = self.sp_.EncodeAsIds(text2)
pieces = self.sp_.EncodeAsPieces(text)
pieces2 = self.sp_.EncodeAsPieces(text2)
self.assertEqual(sp.encode(text), ids)
self.assertEqual(sp.encode(text, out_type=str), pieces)
detok_ids = self.sp_.DecodeIds(ids)
detok_pieces = self.sp_.DecodePieces(pieces)
self.assertEqual(sp.decode(ids), detok_ids)
self.assertEqual(sp.decode(pieces), detok_pieces)
# add_bos, add_eos, reverse
self.assertEqual([sp.bos_id()] + ids, sp.encode(text, add_bos=True))
self.assertEqual(ids + [sp.eos_id()], sp.encode(text, add_eos=True))
rids = ids[:]
rids.reverse()
self.assertEqual(rids, sp.encode(text, reverse=True))
# different shape.
self.assertEqual([ids, ids2], sp.encode([text, text2]))
self.assertEqual([pieces, pieces2], sp.encode([text, text2], out_type=str))
self.assertEqual([text, text2], sp.decode([ids, ids2]))
self.assertEqual([text, text2], sp.decode([pieces, pieces2]))
def test_new_api_init(self):
sp = spm.SentencePieceProcessor(
model_file=os.path.join('test', 'test_model.model'),
add_bos=True,
add_eos=True,
out_type=str)
text = 'hello world'
pieces = ['<s>'] + self.sp_.EncodeAsPieces(text) + ['</s>']
self.assertEqual(pieces, sp.encode(text))
def test_new_api_sampling(self):
sp = spm.SentencePieceProcessor(
model_file=os.path.join('test', 'test_model.model'),
out_type=str,
enable_sampling=True)
ids = defaultdict(int)
for n in range(100):
++ids[' '.join(sp.encode('hello world'))]
self.assertGreater(len(ids), 1)
ids2 = defaultdict(int)
for n in range(100):
++ids2[' '.join(sp.encode('hello world', enable_sampling=False))]
self.assertEqual(len(ids2), 1)
def suite():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(TestSentencepieceProcessor))
return suite
if __name__ == '__main__':
unittest.main()