-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
220 lines (189 loc) · 4.78 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
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
package habari
import (
"bytes"
"github.com/samber/lo"
"slices"
)
var brackets = [][]rune{
{'(', ')'},
{'[', ']'},
{'{', '}'},
{'\u300C', '\u300D'},
{'\u300E', '\u300F'},
{'\u3010', '\u3011'},
{'\uFF08', '\uFF09'},
}
var separators = []rune{'-', '+', '~', '&', '\u2010', '\u2011', '\u2012', '\u2013', '\u2014', '\u2015'}
var delimiters = []rune{' ', '_', '.', '|', ','}
// tokenize
//
// "[hello] world" -> "[", "hello", "]", " ", "world"
func tokenize(filename string) []*token {
type runeVal struct {
openingBracket rune
closingBracket rune
separator rune
delimiter rune
unknown rune
}
// Get all runes
runes := []rune(filename)
runeVars := make([]*runeVal, 0)
for _, r := range runes {
// Identify brackets
foundBracket := false
inner:
for _, bracketDuo := range brackets {
if slices.Contains(bracketDuo, r) {
if bracketDuo[0] == r {
runeVars = append(runeVars, &runeVal{openingBracket: r})
} else {
runeVars = append(runeVars, &runeVal{closingBracket: r})
}
foundBracket = true
break inner
}
}
if foundBracket {
continue
}
idx := lo.IndexOf(separators, r)
if idx != -1 {
runeVars = append(runeVars, &runeVal{separator: r})
continue
}
idx = lo.IndexOf(delimiters, r)
if idx != -1 {
runeVars = append(runeVars, &runeVal{delimiter: r})
continue
}
runeVars = append(runeVars, &runeVal{unknown: r})
}
rawTkns := make([]*token, 0)
// Merge unknown runeVal
unknownBuff := bytes.NewBufferString("")
isUnknownSeq := false
for _, r := range runeVars {
if r.unknown != 0 {
if isUnknownSeq {
unknownBuff.WriteString(string(r.unknown))
} else {
unknownBuff.Reset()
unknownBuff.WriteString(string(r.unknown))
isUnknownSeq = true
}
} else {
// End the unknown sequence
if isUnknownSeq {
isUnknownSeq = false
rawTkns = append(rawTkns, newToken(unknownBuff.String()))
}
if r.separator != 0 {
tkn := newToken(string(r.separator))
tkn.Category = tokenCatSeparator
rawTkns = append(rawTkns, tkn)
}
if r.delimiter != 0 {
tkn := newToken(string(r.delimiter))
tkn.Category = tokenCatDelimiter
rawTkns = append(rawTkns, tkn)
}
if r.openingBracket != 0 {
tkn := newToken(string(r.openingBracket))
tkn.Category = tokenCatOpeningBracket
rawTkns = append(rawTkns, tkn)
}
if r.closingBracket != 0 {
tkn := newToken(string(r.closingBracket))
tkn.Category = tokenCatClosingBracket
rawTkns = append(rawTkns, tkn)
}
}
}
if isUnknownSeq {
rawTkns = append(rawTkns, newToken(unknownBuff.String()))
}
enclosedTknsBuff := make([]*token, 0)
openingBracket := ""
// Find enclosed tokens
for _, tkn := range rawTkns {
if tkn.isOpeningBracket() {
if openingBracket != "" {
} else {
openingBracket = tkn.getValue()
}
continue
}
// Count parenthesis as enclosed tokens if we are already in an enclosed token sequence
// and if the opening bracket is not a parenthesis
tknIsParenthesis := (tkn.getValue() == "(" || tkn.getValue() == ")") && openingBracket != "("
if (tkn.isUnknown() || tknIsParenthesis) && openingBracket != "" {
enclosedTknsBuff = append(enclosedTknsBuff, tkn)
continue
}
// we encounter a closing bracket
if tkn.isClosingBracket() && openingBracket != "" {
// confirm that is it a matching closing bracket
if isMatchingClosingBracket(openingBracket, tkn.getValue()) {
openingBracket = ""
// Update tokens
for _, enclosedTkn := range enclosedTknsBuff {
enclosedTkn.Enclosed = true
}
enclosedTknsBuff = make([]*token, 0)
continue
}
}
}
identifyTokenKinds(rawTkns)
return rawTkns
}
func identifyTokenKinds(tkns []*token) {
for _, tkn := range tkns {
if isCRC32(tkn.getValue()) {
tkn.setKind(tokenKindCrc32)
continue
}
if isResolution(tkn.getValue()) {
tkn.setKind(tokenKindPossibleVideoRes)
continue
}
if isNumber(tkn.getValue()) {
if isYearNumber(tkn.getValue()) {
tkn.setKind(tokenKindYear)
continue
}
tkn.setKind(tokenKindNumber)
continue
}
if isNumberLike(tkn.getValue()) {
tkn.setKind(tokenKindNumberLike)
continue
}
if isOrdinalNumber(tkn.getValue()) {
tkn.setKind(tokenKindOrdinalNumber)
continue
}
if len([]rune(tkn.getValue())) > 1 {
tkn.setKind(tokenKindWord)
} else {
tkn.setKind(tokenKindCharacter)
}
}
}
func isMatchingClosingBracket(openingBracket string, bracket string) bool {
for _, bracketDuo := range brackets {
if string(bracketDuo[0]) == openingBracket {
return string(bracketDuo[1]) == bracket
}
}
return false
}
func isMatchingOpeningBracket(closingBracket string, bracket string) bool {
for _, bracketDuo := range brackets {
if string(bracketDuo[1]) == closingBracket {
return string(bracketDuo[0]) == bracket
}
}
return false
}