forked from buckhx/gobert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
64 lines (56 loc) · 1.79 KB
/
tokenizer.go
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
// Package tokenize supplies tokenization operations for BERT.
// Ports the tokenizer.py capability from the core BERT repo
//
// NOTE: All definitions are related to BERT and may vary from unicode definitions,
// for example, BERT considers '$' punctuation, but unicode does not.
package tokenize
import (
"github.com/sunhailin-Leo/gobert/tokenize/vocab"
"github.com/valyala/bytebufferpool"
)
// Tokenizer is an interface for chunking a string into it's tokens as per the BERT implementation
type Tokenizer interface {
Tokenize(text string) (tokens []string)
}
// VocabTokenizer comprises of a Tokenizer and VocabProvider
type VocabTokenizer interface {
Tokenizer
vocab.Provider
}
// NewTokenizer returns a new FullTokenizer
// Use Option array to modify default behavior
func NewTokenizer(voc vocab.Dict, buf *bytebufferpool.ByteBuffer, opts ...Option) VocabTokenizer {
tkz := Full{
Basic: NewBasic(),
Wordpiece: NewWordpiece(voc, buf),
}
for _, opt := range opts {
tkz = opt(tkz)
}
return tkz
}
// Option alter the behavior of the tokenizer
// TODO add tests for these behavior changes
type Option func(tkz Full) Full
// WithLower will lowercase all input if set to true, or skip lowering if false
// NOTE: kink from reference implementation is that lowering also strips accents
func WithLower(lower bool) Option {
return func(tkz Full) Full {
tkz.Basic.Lower = lower
return tkz
}
}
// WithUnknownToken will alter the unknown token from default [UNK]
func WithUnknownToken(unk string) Option {
return func(tkz Full) Full {
tkz.Wordpiece.unknownToken = unk
return tkz
}
}
// WithMaxChars sets the maximum len of a token to be tokenized, if longer will be labeled as unknown
func WithMaxChars(wc int) Option {
return func(tkz Full) Full {
tkz.Wordpiece.maxWordChars = wc
return tkz
}
}