summaryrefslogtreecommitdiff
path: root/tokenizer/bpe/split/fsa.go
blob: 885aa5ed3d6913b6c8158d71d0b6755f05658329 (plain)
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
package split

import (
	"strings"
	"unicode"
	"unicode/utf8"
)

const (
	RuneUnicode32 = iota
	RuneWhitespaceNotUnicode32
	RuneLetter
	RuneNumber
	RuneOther
	StateInitial
	StateU32
	StateWhitespaceNotUnicode32
	StateLetter
	StateNumber
	StateOther
	StateWhitespaceLookAhead
)

type FSA struct {
	state  int
	static []string
}

func NewFSA() *FSA {
	return &FSA{
		state:  StateInitial,
		static: []string{"'s", "'t", "'re", "'ve", "'m", "'ll", "'d"},
	}
}

func (f *FSA) Reset() {
	f.state = StateInitial
}

func (f *FSA) Read(next rune) bool {
	var r int

	if next == 32 {
		r = RuneUnicode32
	} else if unicode.IsSpace(next) {
		r = RuneWhitespaceNotUnicode32
	} else if unicode.IsLetter(next) {
		r = RuneLetter
	} else if unicode.IsNumber(next) {
		r = RuneNumber
	} else {
		r = RuneOther
	}

	if f.state == StateInitial {
		switch r {
		case RuneUnicode32:
			f.state = StateU32

			break
		case RuneWhitespaceNotUnicode32:
			f.state = StateWhitespaceNotUnicode32

			break
		case RuneLetter:
			f.state = StateLetter

			break
		case RuneNumber:
			f.state = StateNumber

			break
		default:
			f.state = StateOther
		}
	} else if f.state == StateU32 {
		switch r {
		case RuneUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		case RuneWhitespaceNotUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		case RuneLetter:
			f.state = StateLetter

			break
		case RuneNumber:
			f.state = StateNumber

			break
		default:
			f.state = StateOther
		}
	} else if f.state == StateWhitespaceNotUnicode32 {
		switch r {
		case RuneUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		case RuneWhitespaceNotUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		default:
			return false
		}
	} else if f.state == StateNumber {
		switch r {
		case RuneNumber:
			f.state = StateNumber

			break
		default:
			return false
		}
	} else if f.state == StateLetter {
		switch r {
		case RuneLetter:
			f.state = StateLetter

			break
		default:
			return false
		}
	} else if f.state == StateOther {
		switch r {
		case RuneOther:
			f.state = StateOther

			break
		default:
			return false
		}
	} else if f.state == StateWhitespaceLookAhead {
		switch r {
		case RuneUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		case RuneWhitespaceNotUnicode32:
			f.state = StateWhitespaceLookAhead

			break
		default:
			return false
		}
	} else {
		panic("invalid state")
	}

	return true
}

func (f *FSA) FindAll(s string) []string {
	if !utf8.ValidString(s) {
		// s = strings.ToValidUTF8(s, string(utf8.RuneError)) replaces with a single utf8.RuneError
		// s = strings.Map(func(r rune) rune { return r }, s) should be identical to s = string([]rune(s))

		s = string([]rune(s)) // replaces each invalid byte with utf8.RuneError during decoding
	}

	var findAll func(start int, matches []string) []string

	findAll = func(start int, matches []string) []string {
		if start >= len(s) {
			return matches
		}

		for _, v := range f.static {
			if strings.HasPrefix(s[start:], v) {
				matches = append(matches, v)

				next := start + len(v)

				if next >= len(s) {
					return matches
				}

				return findAll(next, matches)
			}
		}

		prev, stop := start, start

		for stop < len(s) {
			r, size := utf8.DecodeRuneInString(s[stop:])

			ok := f.Read(r)

			if ok {
				prev = stop
				stop += size
			} else {
				if f.state == StateInitial {
					return matches
				}

				if f.state == StateWhitespaceLookAhead {
					matches = append(matches, s[start:prev])

					f.Reset()

					return findAll(prev, matches)
				}

				matches = append(matches, s[start:stop])

				f.Reset()

				return findAll(stop, matches)
			}
		}

		if start < len(s) {
			matches = append(matches, s[start:])
		}

		return matches
	}

	defer f.Reset()

	return findAll(0, make([]string, 0))
}