-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentencemode.go
39 lines (31 loc) · 886 Bytes
/
sentencemode.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
package babble
import (
"regexp"
"strings"
)
// SentenceMode will process key material by splitting on typical
// sentence-ending punctuation. It uses SentenceTokens.
type SentenceMode struct {
skip int
}
// Divider returns the divider to use between sentences.
func (m *SentenceMode) Divider() string {
return " "
}
// Skip will cause Split() to skip the first n tokens.
func (m *SentenceMode) Skip(n int) {
m.skip = n
}
// Tokenize will split on any typical sentence-ending punctuation.
func (m *SentenceMode) Tokenize(b []byte) []Token {
var out []Token
var r *regexp.Regexp = regexp.MustCompile(
`([A-Z][^!"'.?]*(("[^"]+"|'[^']+')?[^!"'.?]*)*([!.?])+)`,
)
var s string = whiteSpace.ReplaceAllString(string(b), " ")
s = strings.TrimSpace(s)
for _, sentence := range r.FindAllString(s, -1)[m.skip:] {
out = append(out, SentenceToken{sentence})
}
return out
}