-
Notifications
You must be signed in to change notification settings - Fork 22
/
tokenizer.go
56 lines (42 loc) · 1.06 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
package parse
import (
"strings"
)
// TokenizeText function use the given raw text and parses by a Rule object and
// retrieves the parsed text in a Text struct object.
func TokenizeText(rawText string, rule Rule) Text {
return findSentences(rawText, rule)
}
func findSentences(rawText string, rule Rule) Text {
text := Text{}
var sentence string
for _, chr := range rawText {
if !rule.IsSentenceSeparator(chr) {
sentence = sentence + string(chr)
} else if len(sentence) > 0 {
sentence = sentence + string(chr)
text.Append(sentence, findWords(sentence, rule))
sentence = ""
}
}
if len(sentence) > 0 {
text.Append(sentence, findWords(sentence, rule))
}
return text
}
func findWords(rawSentence string, rule Rule) (words []string) {
words = []string{}
var word string
for _, chr := range rawSentence {
if !rule.IsWordSeparator(chr) {
word = word + string(chr)
} else if len(word) > 0 {
words = append(words, strings.ToLower(word))
word = ""
}
}
if len(word) > 0 {
words = append(words, strings.ToLower(word))
}
return
}