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

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

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

type FSA struct {
	static []string
}

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

func (f *FSA) Read(state int, next rune) (int, 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 state == StateInitial {
		switch r {
		case RuneUnicode32:
			return StateU32, true
		case RuneWhitespaceNotUnicode32:
			return StateWhitespaceNotUnicode32, true
		case RuneLetter:
			return StateLetter, true
		case RuneNumber:
			return StateNumber, true
		default:
			return StateOther, true
		}
	} else if state == StateU32 {
		switch r {
		case RuneUnicode32:
			return StateWhitespaceLookAhead, true
		case RuneWhitespaceNotUnicode32:
			return StateWhitespaceLookAhead, true
		case RuneLetter:
			return StateLetter, true
		case RuneNumber:
			return StateNumber, true
		default:
			return StateOther, true
		}
	} else if state == StateWhitespaceNotUnicode32 {
		switch r {
		case RuneUnicode32:
			return StateWhitespaceLookAhead, true
		case RuneWhitespaceNotUnicode32:
			return StateWhitespaceLookAhead, true
		default:
			return state, false
		}
	} else if state == StateNumber {
		switch r {
		case RuneNumber:
			return StateNumber, true
		default:
			return state, false
		}
	} else if state == StateLetter {
		switch r {
		case RuneLetter:
			return StateLetter, true
		default:
			return state, false
		}
	} else if state == StateOther {
		switch r {
		case RuneOther:
			return StateOther, true
		default:
			return state, false
		}
	} else if state == StateWhitespaceLookAhead {
		switch r {
		case RuneUnicode32:
			return StateWhitespaceLookAhead, true
		case RuneWhitespaceNotUnicode32:
			return StateWhitespaceLookAhead, true
		default:
			return state, false
		}
	} else {
		panic("invalid state")
	}
}

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

		state := StateInitial

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

			next, ok := f.Read(state, r)

			if ok {
				prev = stop
				stop += size

				state = next
			} else {
				if state == StateInitial {
					return matches
				}

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

					return findAll(prev, matches)
				}

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

				return findAll(stop, matches)
			}
		}

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

		return matches
	}

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