Skip to content

Commit 2b01ec0

Browse files
committed
Polish Tokenize
1 parent 6182b48 commit 2b01ec0

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ ENV/
8989
# Rope project settings
9090
.ropeproject
9191
.idea
92-
stopwords.txt
92+
stopwords.txt
93+
/test

trie.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -239,26 +239,22 @@ func Tokenize(emits []*Emit, source string) []*Token {
239239
if el == 0 {
240240
return []*Token{{source, nil}}
241241
}
242-
count := 0
243242
index := 0
244243
runes := []rune(source)
245-
tokens := make([]*Token, el*2+1)
244+
tokens := make([]*Token, 0, el*2+1)
246245
for i := 0; i < el; i++ {
247246
emit := emits[i]
248247
if index < emit.Begin {
249-
tokens[count] = &Token{string(runes[index:emit.Begin]), nil}
250-
count++
248+
tokens = append(tokens, &Token{string(runes[index:emit.Begin]), nil})
251249
}
252-
tokens[count] = &Token{string(runes[emit.Begin:emit.End]), emit}
253-
count++
250+
tokens = append(tokens, &Token{string(runes[emit.Begin:emit.End]), emit})
254251
index = emit.End
255252
}
256253
last := emits[el-1]
257254
if last.End < utf8.RuneCountInString(source) {
258-
tokens[count] = &Token{string(runes[last.End:]), nil}
259-
count++
255+
tokens = append(tokens, &Token{string(runes[last.End:]), nil})
260256
}
261-
return tokens[:count]
257+
return tokens
262258
}
263259

264260
func Replace(emits []*Emit, source string, replacement string) string {
@@ -306,19 +302,17 @@ func removeEmits(emits []*Emit, predicate func(a, b *Emit) bool) []*Emit {
306302
replica := make([]*Emit, el)
307303
copy(replica, emits)
308304
sortEmits(replica)
309-
index := 1
310305
emit := replica[0]
311-
sorted := make([]*Emit, el)
312-
sorted[0] = emit
306+
sorted := make([]*Emit, 0, el)
307+
sorted = append(sorted, emit)
313308
for i := 1; i < el; i++ {
314309
next := replica[i]
315310
if !predicate(emit, next) {
316-
sorted[index] = next
317-
index++
311+
sorted = append(sorted, next)
318312
emit = next
319313
}
320314
}
321-
return sorted[:index]
315+
return sorted
322316
}
323317

324318
func sortEmits(emits []*Emit) {

0 commit comments

Comments
 (0)