-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.go
159 lines (129 loc) · 3.29 KB
/
main.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
package english
import (
"regexp"
"strings"
"gopkg.in/neurosnap/sentences.v1"
"gopkg.in/neurosnap/sentences.v1/data"
)
type WordTokenizer struct {
sentences.DefaultWordTokenizer
}
var reAbbr = regexp.MustCompile(`((?:[\w]\.)+[\w]*\.)`)
// English customized sentence tokenizer.
func NewSentenceTokenizer(s *sentences.Storage) (*sentences.DefaultSentenceTokenizer, error) {
training := s
if training == nil {
b, err := data.Asset("data/english.json")
if err != nil {
return nil, err
}
training, err = sentences.LoadTraining(b)
if err != nil {
return nil, err
}
}
// supervisor abbreviations
abbrevs := []string{"sgt", "gov", "no"}
for _, abbr := range abbrevs {
training.AbbrevTypes.Add(abbr)
}
lang := sentences.NewPunctStrings()
word := NewWordTokenizer(lang)
annotations := sentences.NewAnnotations(training, lang, word)
ortho := &sentences.OrthoContext{
Storage: training,
PunctStrings: lang,
TokenType: word,
TokenFirst: word,
}
multiPunct := &MultiPunctWordAnnotation{
Storage: training,
TokenParser: word,
TokenGrouper: &sentences.DefaultTokenGrouper{},
Ortho: ortho,
}
annotations = append(annotations, multiPunct)
tokenizer := &sentences.DefaultSentenceTokenizer{
Storage: training,
PunctStrings: lang,
WordTokenizer: word,
Annotations: annotations,
}
return tokenizer, nil
}
func NewWordTokenizer(p sentences.PunctStrings) *WordTokenizer {
word := &WordTokenizer{}
word.PunctStrings = p
return word
}
// Find any punctuation excluding the period final
func (e *WordTokenizer) HasSentEndChars(t *sentences.Token) bool {
enders := []string{
`."`, `.'`, `.)`, `.’`, `.”`,
`?`, `?"`, `?'`, `?)`, `?’`, `?”`,
`!`, `!"`, `!'`, `!)`, `!’`, `!”`,
}
for _, ender := range enders {
if strings.HasSuffix(t.Tok, ender) {
return true
}
}
parens := []string{
`.[`, `.(`, `."`, `.'`,
`?[`, `?(`,
`![`, `!(`,
}
for _, paren := range parens {
if strings.Index(t.Tok, paren) != -1 {
return true
}
}
return false
}
// Attempts to tease out custom Abbreviations, e.g. F.B.I.
type MultiPunctWordAnnotation struct {
*sentences.Storage
sentences.TokenParser
sentences.TokenGrouper
sentences.Ortho
}
func (a *MultiPunctWordAnnotation) Annotate(tokens []*sentences.Token) []*sentences.Token {
for _, tokPair := range a.TokenGrouper.Group(tokens) {
if len(tokPair) < 2 || tokPair[1] == nil {
continue
}
a.tokenAnnotation(tokPair[0], tokPair[1])
}
return tokens
}
func (a *MultiPunctWordAnnotation) tokenAnnotation(tokOne, tokTwo *sentences.Token) {
if len(reAbbr.FindAllString(tokOne.Tok, 1)) == 0 {
return
}
if a.IsInitial(tokOne) {
return
}
tokOne.Abbr = true
tokOne.SentBreak = false
nextTyp := a.TokenParser.TypeNoSentPeriod(tokTwo)
/*
[4.1.1. Orthographic Heuristic] Check if there's
orthogrpahic evidence about whether the next word
starts a sentence or not.
*/
isSentStarter := a.Ortho.Heuristic(tokTwo)
if isSentStarter == 1 {
tokOne.SentBreak = true
return
}
/*
[4.1.3. Frequent Sentence Starter Heruistic] If the
next word is capitalized, and is a member of the
frequent-sentence-starters list, then label tok as a
sentence break.
*/
if a.TokenParser.FirstUpper(tokTwo) && a.SentStarters[nextTyp] != 0 {
tokOne.SentBreak = true
return
}
}