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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
%module sentencepiece
%include exception.i
%{
#include <cmath>
#include <sentencepiece_processor.h>
#include <sentencepiece_trainer.h>
namespace {
PyObject* kUnicodeInput = reinterpret_cast<PyObject* >(0x1);
PyObject* kByteInput = reinterpret_cast<PyObject* >(0x2);
inline void ReleaseResultObject(PyObject *obj) {
if (obj != nullptr && obj != kUnicodeInput && obj != kByteInput) {
Py_XDECREF(obj);
}
}
class PyInputString {
public:
explicit PyInputString(PyObject* obj) {
#if PY_VERSION_HEX >= 0x03000000
if (PyUnicode_Check(obj)) {
// Python3, Unicode
str_ = const_cast<char *>(PyUnicode_AsUTF8AndSize(obj, &size_));
input_type_ = kUnicodeInput;
} else if (PyBytes_Check(obj)) {
// Python3, Bytes
PyBytes_AsStringAndSize(obj, &str_, &size_);
input_type_ = kByteInput;
}
#else
if (PyUnicode_Check(obj)) {
// Python2, Unicode
PyObject *utf8_obj = PyUnicode_AsUTF8String(obj);
PyString_AsStringAndSize(utf8_obj, &str_, &size_);
input_type_ = utf8_obj;
} else if (PyString_Check(obj)) {
// Python2, Bytes,
PyString_AsStringAndSize(obj, &str_, &size_);
input_type_ = kByteInput;
}
#endif
else {
str_ = nullptr;
}
}
const char* data() const { return str_; }
Py_ssize_t size() const { return size_; }
bool IsAvalable() const { return str_ != nullptr; }
PyObject *input_type() const { return input_type_; }
static bool IsUnicode(PyObject *resultobj) {
#if PY_VERSION_HEX >= 0x03000000
return (resultobj == nullptr || resultobj == kUnicodeInput);
#else
return (resultobj != nullptr && resultobj != kByteInput);
#endif
}
private:
PyObject* input_type_ = nullptr;
char* str_ = nullptr;
Py_ssize_t size_ = 0;
};
PyObject* MakePyOutputString(const std::string& output,
PyObject *resultobj) {
if (PyInputString::IsUnicode(resultobj)) {
return PyUnicode_FromStringAndSize(output.data(), output.size());
}
#if PY_VERSION_HEX >= 0x03000000
return PyBytes_FromStringAndSize(output.data(), output.size());
#else
return PyString_FromStringAndSize(output.data(), output.size());
#endif
}
PyObject* MakePyOutputBytes(const std::string& output) {
#if PY_VERSION_HEX >= 0x03000000
return PyBytes_FromStringAndSize(output.data(), output.size());
#else
return PyString_FromStringAndSize(output.data(), output.size());
#endif
}
int ToSwigError(sentencepiece::util::StatusCode code) {
switch (code) {
case sentencepiece::util::StatusCode::kNotFound:
return SWIG_IOError;
case sentencepiece::util::StatusCode::kOutOfRange:
return SWIG_IndexError;
case sentencepiece::util::StatusCode::kInvalidArgument:
return SWIG_SyntaxError;
default:
return SWIG_RuntimeError;
}
return SWIG_RuntimeError;
}
class PySentenceIterator : public sentencepiece::SentenceIterator {
public:
PySentenceIterator(PyObject *iter) : iter_(iter) {
item_ = PyIter_Next(iter_);
CopyValue();
}
~PySentenceIterator() {
// Py_XDECREF(iter_);
}
bool done() const override {
return item_ == nullptr;
}
void Next() override {
item_ = PyIter_Next(iter_);
CopyValue();
}
const std::string &value() const override {
return value_;
}
sentencepiece::util::Status status() const override {
return status_;
}
private:
void CopyValue() {
if (item_ == nullptr) return;
const PyInputString ustring(item_);
if (ustring.IsAvalable()) {
const char *data = ustring.data();
size_t size = ustring.size();
while (size > 0) {
if (data[size - 1] == '\r' || data[size - 1] == '\n')
--size;
else
break;
}
value_.assign(data, size);
} else {
status_ = sentencepiece::util::Status(sentencepiece::util::StatusCode::kInternal,
"Not a string.");
}
Py_XDECREF(item_);
}
PyObject *iter_ = nullptr;
PyObject *item_ = nullptr;
std::string value_;
sentencepiece::util::Status status_;
};
}
%}
%exception {
try {
$action
ReleaseResultObject(resultobj);
}
catch (const sentencepiece::util::Status &status) {
SWIG_exception(ToSwigError(status.code()), status.ToString().c_str());
}
}
%ignore sentencepiece::util::Status;
%ignore sentencepiece::util::StatusCode;
%ignore absl::string_view;
%ignore sentencepiece::SentencePieceText;
%ignore sentencepiece::NormalizerSpec;
%ignore sentencepiece::TrainerSpec;
%ignore sentencepiece::SentencePieceProcessor::status;
%ignore sentencepiece::SentencePieceProcessor::Encode;
%ignore sentencepiece::SentencePieceProcessor::SampleEncode;
%ignore sentencepiece::SentencePieceProcessor::NBestEncode;
%ignore sentencepiece::SentencePieceProcessor::Decode;
%ignore sentencepiece::SentencePieceProcessor::model_proto;
%ignore sentencepiece::SentencePieceProcessor::Load;
%ignore sentencepiece::SentencePieceProcessor::LoadOrDie;
%ignore sentencepiece::pretokenizer::PretokenizerForTrainingInterface;
%ignore sentencepiece::SentenceIterator;
%ignore sentencepiece::SentencePieceTrainer::Train;
%ignore sentencepiece::SentencePieceTrainer::GetNormalizerSpec;
%ignore sentencepiece::SentencePieceTrainer::PopulateNormalizerSpec;
%ignore sentencepiece::SentencePieceTrainer::MergeSpecsFromArgs;
%ignore sentencepiece::SentencePieceTrainer::SetProtoField;
%ignore sentencepiece::SentencePieceTrainer::PopulateModelTypeFromString;
%ignore sentencepiece::SentencePieceTrainer::PieceProcecssor;
%ignore sentencepiece::SentencePieceTrainer::SetPretokenizerForTraining;
%ignore sentencepiece::SentencePieceTrainer::GetPretokenizerForTraining;
%extend sentencepiece::SentencePieceProcessor {
sentencepiece::util::Status LoadFromFile(absl::string_view arg) {
return $self->Load(arg);
}
%pythoncode {
def Init(self,
model_file=None,
model_proto=None,
out_type=int,
add_bos=False,
add_eos=False,
reverse=False,
enable_sampling=False,
nbest_size=-1,
alpha=0.1):
"""Initialzie sentencepieceProcessor.
Args:
model_file: The sentencepiece model file path.
model_proto: The sentencepiece model serialized proto.
out_type: output type. int or str.
add_bos: Add <s> to the result (Default = false)
add_eos: Add </s> to the result (Default = false) <s>/</s> is added after
reversing (if enabled).
reverse: Reverses the tokenized sequence (Default = false)
nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
nbest_size = {0,1}: No sampling is performed.
nbest_size > 1: samples from the nbest_size results.
nbest_size < 0: assuming that nbest_size is infinite and samples
from the all hypothesis (lattice) using
forward-filtering-and-backward-sampling algorithm.
alpha: Soothing parameter for unigram sampling, and merge probability for
BPE-dropout.
"""
_sentencepiece_processor_init_native(self)
self._out_type = out_type
self._add_bos = add_bos
self._add_eos = add_eos
self._reverse = reverse
self._enable_sampling = enable_sampling
self._nbest_size = nbest_size
self._alpha = alpha
if model_file or model_proto:
self.Load(model_file=model_file, model_proto=model_proto)
def Encode(self,
input,
out_type=None,
add_bos=None,
add_eos=None,
reverse=None,
enable_sampling=None,
nbest_size=None,
alpha=None):
"""Encode text input to segmented ids or tokens.
Args:
input: input string. accepsts list of string.
out_type: output type. int or str.
add_bos: Add <s> to the result (Default = false)
add_eos: Add </s> to the result (Default = false) <s>/</s> is added after
reversing (if enabled).
reverse: Reverses the tokenized sequence (Default = false)
nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
nbest_size = {0,1}: No sampling is performed.
nbest_size > 1: samples from the nbest_size results.
nbest_size < 0: assuming that nbest_size is infinite and samples
from the all hypothesis (lattice) using
forward-filtering-and-backward-sampling algorithm.
alpha: Soothing parameter for unigram sampling, and merge probability for
BPE-dropout.
"""
if out_type is None:
out_type = self._out_type
if add_bos is None:
add_bos = self._add_bos
if add_eos is None:
add_eos = self._add_eos
if reverse is None:
reverse = self._reverse
if enable_sampling is None:
enable_sampling = self._enable_sampling
if nbest_size is None:
nbest_size = self._nbest_size
if alpha is None:
alpha = self._alpha
if enable_sampling == True and (nbest_size is None or nbest_size == 0 or
nbest_size == 1 or alpha is None or
alpha <= 0.0 or alpha > 1.0):
raise RuntimeError(
'When enable_sampling is True, We must specify "nbest_size > 1" or "nbest_size = -1", '
'and "0.0 < alpha < 1.0". "nbest_size = -1" is enabled only on unigram mode and '
'samples from all candidates on the lattice instead of nbest segmentations. '
)
def _encode(text):
if out_type is int:
if enable_sampling:
result = self.SampleEncodeAsIds(text, nbest_size, alpha)
else:
result = self.EncodeAsIds(text)
else:
if enable_sampling:
result = self.SampleEncodeAsPieces(text, nbest_size, alpha)
else:
result = self.EncodeAsPieces(text)
if reverse:
result.reverse()
if add_bos:
if out_type is int:
result = [self.bos_id()] + result
else:
result = [self.IdToPiece(self.bos_id())] + result
if add_eos:
if out_type is int:
result = result + [self.eos_id()]
else:
result = result + [self.IdToPiece(self.eos_id())]
return result
if type(input) is list:
return [_encode(n) for n in input]
return _encode(input)
def Decode(self, input):
"""Decode processed id or token sequences."""
if not input:
return self.DecodeIds([])
elif type(input) is int:
return self.DecodeIds([input])
elif type(input) is str:
return self.DecodePieces([input])
def _decode(input):
if not input:
return self.DecodeIds([])
if type(input[0]) is int:
return self.DecodeIds(input)
return self.DecodePieces(input)
if type(input[0]) is list:
return [_decode(n) for n in input]
return _decode(input)
def piece_size(self):
return self.GetPieceSize()
def vocab_size(self):
return self.GetPieceSize()
def __getstate__(self):
return self.serialized_model_proto()
def __setstate__(self, serialized_model_proto):
self.__init__()
self.LoadFromSerializedProto(serialized_model_proto)
def __len__(self):
return self.GetPieceSize()
def __getitem__(self, piece):
return self.PieceToId(piece)
def Load(self, model_file=None, model_proto=None):
"""Overwride SentencePieceProcessor.Load to support both model_file and model_proto.
Args:
model_file: The sentencepiece model file path.
model_proto: The sentencepiece model serialized proto. Either `model_file`
or `model_proto` must be set.
"""
if model_file and model_proto:
raise RuntimeError('model_file and model_proto must be exclusive.')
if model_proto:
return self.LoadFromSerializedProto(model_proto)
return self.LoadFromFile(model_file)
}
}
%extend sentencepiece::SentencePieceTrainer {
static void _TrainFromString(absl::string_view arg) {
const auto _status = sentencepiece::SentencePieceTrainer::Train(arg);
if (!_status.ok()) throw _status;
return;
}
static void _TrainFromMap(const std::map<std::string, std::string> &args) {
const auto _status = sentencepiece::SentencePieceTrainer::Train(args);
if (!_status.ok()) throw _status;
return;
}
static void _TrainFromMap2(const std::map<std::string, std::string> &args,
SentenceIterator *iter) {
const auto _status = sentencepiece::SentencePieceTrainer::Train(args, iter);
if (!_status.ok()) throw _status;
return;
}
static sentencepiece::util::bytes _TrainFromMap3(const std::map<std::string, std::string> &args) {
sentencepiece::util::bytes model_proto;
const auto _status = sentencepiece::SentencePieceTrainer::Train(args, nullptr, &model_proto);
if (!_status.ok()) throw _status;
return model_proto;
}
static sentencepiece::util::bytes _TrainFromMap4(const std::map<std::string, std::string> &args,
SentenceIterator *iter) {
sentencepiece::util::bytes model_proto;
const auto _status = sentencepiece::SentencePieceTrainer::Train(args, iter, &model_proto);
if (!_status.ok()) throw _status;
return model_proto;
}
%pythoncode {
@staticmethod
def Train(arg=None, **kwargs):
"""Train Sentencepiece model. Accept both kwargs and legacy string arg."""
if arg is not None and type(arg) is str:
return SentencePieceTrainer._TrainFromString(arg)
def _encode(value):
"""Encode value to CSV.."""
if type(value) is list:
if sys.version_info[0] == 3:
f = StringIO()
else:
f = BytesIO()
writer = csv.writer(f, lineterminator='')
writer.writerow([str(v) for v in value])
return f.getvalue()
else:
return str(value)
sentence_iterator = None
model_writer = None
new_kwargs = {}
for key, value in kwargs.items():
if key in ['sentence_iterator', 'sentence_reader']:
sentence_iterator = value
elif key in ['model_writer']:
model_writer = value
else:
new_kwargs[key] = _encode(value)
if model_writer:
if sentence_iterator:
model_proto = SentencePieceTrainer._TrainFromMap4(new_kwargs,
sentence_iterator)
else:
model_proto = SentencePieceTrainer._TrainFromMap3(new_kwargs)
model_writer.write(model_proto)
else:
if sentence_iterator:
return SentencePieceTrainer._TrainFromMap2(new_kwargs, sentence_iterator)
else:
return SentencePieceTrainer._TrainFromMap(new_kwargs)
return None
}
}
%typemap(out) std::vector<int> {
$result = PyList_New($1.size());
for (size_t i = 0; i < $1.size(); ++i) {
PyList_SetItem($result, i, PyInt_FromLong(static_cast<long>($1[i])));
}
}
%typemap(out) std::vector<std::vector<int>> {
$result = PyList_New($1.size());
for (size_t i = 0; i < $1.size(); ++i) {
PyObject *obj = PyList_New($1[i].size());
for (size_t j = 0; j < $1[i].size(); ++j) {
PyList_SetItem(obj, j, PyInt_FromLong(static_cast<long>($1[i][j])));
}
PyList_SetItem($result, i, obj);
}
}
%typemap(out) std::vector<std::string> {
PyObject *input_type = resultobj;
$result = PyList_New($1.size());
for (size_t i = 0; i < $1.size(); ++i) {
PyList_SetItem($result, i, MakePyOutputString($1[i], input_type));
}
}
%typemap(out) std::vector<std::vector<std::string>> {
PyObject *input_type = resultobj;
$result = PyList_New($1.size());
for (size_t i = 0; i < $1.size(); ++i) {
PyObject *obj = PyList_New($1[i].size());
for (size_t j = 0; j < $1[i].size(); ++j) {
PyList_SetItem(obj, j, MakePyOutputString($1[i][j], input_type));
}
PyList_SetItem($result, i, obj);
}
}
%typemap(out) std::string {
PyObject *input_type = resultobj;
$result = MakePyOutputString($1, input_type);
}
%typemap(out) const std::string& {
PyObject *input_type = resultobj;
$result = MakePyOutputString(*$1, input_type);
}
%typemap(out) sentencepiece::util::bytes {
$result = MakePyOutputBytes($1);
}
%typemap(out) sentencepiece::util::Status {
if (!$1.ok()) {
SWIG_exception(ToSwigError($1.code()), $1.ToString().c_str());
}
$result = SWIG_From_bool($1.ok());
}
%typemap(in) const std::string & {
const PyInputString ustring($input);
if (!ustring.IsAvalable()) {
PyErr_SetString(PyExc_TypeError, "not a string");
SWIG_fail;
}
resultobj = ustring.input_type();
$1 = new std::string(ustring.data(), ustring.size());
}
%typemap(typecheck) absl::string_view = char *;
%typemap(in) absl::string_view {
const PyInputString ustring($input);
if (!ustring.IsAvalable()) {
PyErr_SetString(PyExc_TypeError, "not a string");
SWIG_fail;
}
resultobj = ustring.input_type();
$1 = absl::string_view(ustring.data(), ustring.size());
}
%typemap(in) const std::vector<std::string>& {
std::vector<std::string> *out = nullptr;
if (PyList_Check($input)) {
const size_t size = PyList_Size($input);
out = new std::vector<std::string>(size);
for (size_t i = 0; i < size; ++i) {
const PyInputString ustring(PyList_GetItem($input, i));
if (ustring.IsAvalable()) {
(*out)[i] = std::string(ustring.data(), ustring.size());
} else {
PyErr_SetString(PyExc_TypeError, "list must contain strings");
SWIG_fail;
}
resultobj = ustring.input_type();
}
} else {
PyErr_SetString(PyExc_TypeError, "not a list");
SWIG_fail;
}
$1 = out;
}
%typemap(in) const std::vector<int>& {
std::vector<int> *out = nullptr;
if (PyList_Check($input)) {
const size_t size = PyList_Size($input);
out = new std::vector<int>(size);
for (size_t i = 0; i < size; ++i) {
PyObject *o = PyList_GetItem($input, i);
if (PyInt_Check(o)) {
(*out)[i] = static_cast<int>(PyInt_AsLong(o));
} else {
PyErr_SetString(PyExc_TypeError,"list must contain integers");
SWIG_fail;
}
}
} else {
PyErr_SetString(PyExc_TypeError,"not a list");
SWIG_fail;
}
$1 = out;
}
%typemap(in) const std::map<std::string, std::string> & {
std::map<std::string, std::string> *out = nullptr;
if (PyDict_Check($input)) {
PyObject *key, *value;
Py_ssize_t pos = 0;
out = new std::map<std::string, std::string>;
while (PyDict_Next($input, &pos, &key, &value)) {
const PyInputString key_ustring(key);
const PyInputString value_ustring(value);
if (key_ustring.IsAvalable() && value_ustring.IsAvalable()) {
out->emplace(std::string(key_ustring.data(), key_ustring.size()),
std::string(value_ustring.data(), value_ustring.size()));
} else {
PyErr_SetString(PyExc_TypeError, "map must contain strings.");
SWIG_fail;
}
resultobj = key_ustring.input_type();
}
} else {
PyErr_SetString(PyExc_TypeError, "not a dictionary");
SWIG_fail;
}
$1 = out;
}
%typemap(in) sentencepiece::SentenceIterator * {
sentencepiece::SentenceIterator *out = nullptr;
if (PyIter_Check($input)) {
out = new PySentenceIterator($input);
} else {
PyErr_SetString(PyExc_TypeError, "not a iterator");
SWIG_fail;
}
$1 = out;
}
%typemap(freearg) const std::string& {
delete $1;
}
%typemap(freearg) const std::vector<std::string>& {
delete $1;
}
%typemap(freearg) const std::vector<std::vector<std::string>>& {
delete $1;
}
%typemap(freearg) const std::vector<int>& {
delete $1;
}
%typemap(freearg) const std::vector<std::vector<int>>& {
delete $1;
}
%typemap(freearg) const std::map<std::string, std::string> & {
delete $1;
}
%typemap(freearg) sentencepiece::SentenceIterator * {
delete $1;
}
%include <sentencepiece_processor.h>
%include <sentencepiece_trainer.h>
%pythoncode %{
import re
import csv
import sys
from io import StringIO
from io import BytesIO
def _add_snake_case(classname):
"""Added snake_cased method from CammelCased method."""
snake_map = {}
for k, v in classname.__dict__.items():
if re.match(r'^[A-Z]+', k):
snake = re.sub(r'(?<!^)(?=[A-Z])', '_',
k).lower().replace('n_best', 'nbest')
snake_map[snake] = v
for k, v in snake_map.items():
setattr(classname, k, v)
def _batchnize(classname, name):
"""Enables batch request for the method classname.name."""
func = getattr(classname, name, None)
def _batched_func(self, arg):
if type(arg) is list:
return [func(self, n) for n in arg]
else:
return func(self, arg)
setattr(classname, name, _batched_func)
_sentencepiece_processor_init_native = SentencePieceProcessor.__init__
setattr(SentencePieceProcessor, '__init__', SentencePieceProcessor.Init)
SentencePieceProcessor.Tokenize = SentencePieceProcessor.Encode
SentencePieceProcessor.Detokenize = SentencePieceProcessor.Decode
for m in [
'PieceToId', 'IdToPiece', 'GetScore', 'IsUnknown', 'IsControl', 'IsUnused',
'IsByte'
]:
_batchnize(SentencePieceProcessor, m)
_add_snake_case(SentencePieceProcessor)
_add_snake_case(SentencePieceTrainer)
%}
|