forked from advancedlogic/go-freeling
/
splitter.go
217 lines (195 loc) · 5.49 KB
/
splitter.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
package nlp
import (
"container/list"
set "gopkg.in/fatih/set.v0"
"strconv"
"strings"
)
const SAME = 100
const VERY_LONG = 1000
const (
SPLITTER_GENERAL = 1 + iota
SPLITTER_MARKERS
SPLITTER_SENT_END
SPLITTER_SENT_START
)
type Splitter struct {
SPLIT_AllowBetweenMarkers bool
SPLIT_MaxWords int64
starters *set.Set
enders map[string]bool
markers map[string]int
}
func NewSplitter(splitterFile string) *Splitter {
this := Splitter{
starters: set.New(),
enders: make(map[string]bool),
markers: make(map[string]int),
}
cfg := NewConfigFile(false, "##")
cfg.AddSection("General", SPLITTER_GENERAL)
cfg.AddSection("Markers", SPLITTER_MARKERS)
cfg.AddSection("SentenceEnd", SPLITTER_SENT_END)
cfg.AddSection("SentenceStart", SPLITTER_SENT_START)
if !cfg.Open(splitterFile) {
CRASH("Error opening file "+splitterFile, MOD_SPLITTER)
}
this.SPLIT_AllowBetweenMarkers = true
this.SPLIT_MaxWords = 0
nmk := 1
line := ""
for cfg.GetContentLine(&line) {
items := Split(line, " ")
switch cfg.GetSection() {
case SPLITTER_GENERAL:
{
name := items[0]
if name == "AllowBetweenMarkers" {
this.SPLIT_AllowBetweenMarkers, _ = strconv.ParseBool(items[1])
} else if name == "MaxWords" {
this.SPLIT_MaxWords, _ = strconv.ParseInt(items[1], 10, 64)
} else {
LOG.Panic("Unexpected splitter option " + name)
}
break
}
case SPLITTER_MARKERS:
{
open := items[0]
close := items[1]
if open != close {
this.markers[open] = nmk
this.markers[close] = -nmk
} else {
this.markers[open] = SAME + nmk
this.markers[close] = SAME + nmk
}
nmk++
break
}
case SPLITTER_SENT_END:
{
name := items[0]
value, _ := strconv.ParseBool(items[1])
this.enders[name] = !value
break
}
case SPLITTER_SENT_START:
{
this.starters.Add(line)
break
}
default:
break
}
}
LOG.Trace("Analyzer succesfully created")
return &this
}
type SplitterStatus struct {
BetweenMark bool
NoSplitCount int
MarkType *list.List
MarkForm *list.List
buffer *Sentence
nsentence int
}
func (this *Splitter) OpenSession() *SplitterStatus {
LOG.Trace("Opening new session")
return &SplitterStatus{
BetweenMark: false,
NoSplitCount: 0,
MarkType: list.New(),
MarkForm: list.New(),
buffer: NewSentence(),
nsentence: 0,
}
}
func (this *Splitter) CloseSession(ses *SplitterStatus) {
LOG.Trace("Closing session")
ses.MarkType = ses.MarkType.Init()
ses.MarkForm = ses.MarkForm.Init()
ses = nil
}
func (this *Splitter) Split(st *SplitterStatus, v *list.List, flush bool, ls *list.List) {
ls = ls.Init()
LOG.Trace("Looking for a sentence marker. Max no split is " + strconv.Itoa(int(this.SPLIT_MaxWords)))
for w := v.Front(); w != nil; w = w.Next() {
m := this.markers[w.Value.(*Word).getForm()]
checkSplit := true
if st.BetweenMark && !this.SPLIT_AllowBetweenMarkers && m != 0 && m == If(m > SAME, 1, -1).(int)*st.MarkType.Front().Value.(int) {
LOG.Trace("End no split period. marker " + w.Value.(*Word).getForm() + " code: " + strconv.Itoa(m))
st.MarkType.Remove(st.MarkType.Front())
st.MarkForm.Remove(st.MarkForm.Front())
if st.MarkForm.Len() == 0 {
st.BetweenMark = false
st.NoSplitCount = 0
} else {
st.NoSplitCount++
}
st.buffer.PushBack(w.Value.(*Word))
checkSplit = false
} else if m > 0 && !this.SPLIT_AllowBetweenMarkers {
st.MarkForm.PushFront(w.Value.(*Word).getForm())
st.MarkType.PushFront(m)
LOG.Trace("Start no split periood, marker " + w.Value.(*Word).getForm() + " code:" + strconv.Itoa(m))
st.BetweenMark = true
st.NoSplitCount++
st.buffer.PushBack(w.Value.(*Word))
checkSplit = false
} else if st.BetweenMark {
LOG.Trace("no-split flag continues set. word " + w.Value.(*Word).getForm() + " expecting code " + strconv.Itoa(st.MarkType.Front().Value.(int)) + " (closing " + st.MarkForm.Front().Value.(string))
st.NoSplitCount++
if this.SPLIT_MaxWords == 0 || st.NoSplitCount <= int(this.SPLIT_MaxWords) {
checkSplit = false
st.buffer.PushBack(w.Value.(*Word))
}
if st.NoSplitCount == VERY_LONG {
LOG.Warn("Sentence is very long")
}
}
if checkSplit {
e := this.enders[w.Value.(*Word).getForm()]
if e {
if e || this.endOfSentence(w, v) {
LOG.Trace("Sentence marker [" + w.Value.(*Word).getForm() + "] found")
st.buffer.PushBack(w.Value.(*Word))
st.nsentence++
st.buffer.sentID = strconv.Itoa(st.nsentence)
ls.PushBack(st.buffer)
LOG.Trace("Sentence lenght " + strconv.Itoa(st.buffer.Len()))
nsentence := st.nsentence
this.CloseSession(st)
st = this.OpenSession()
st.nsentence = nsentence
} else {
LOG.Trace(w.Value.(*Word).getForm() + " is not a sentence marker here")
st.buffer.PushBack(w.Value.(*Word))
}
} else {
LOG.Trace(w.Value.(*Word).getForm() + " is not a sentence marker here")
st.buffer.PushBack(w.Value.(*Word))
}
}
}
if flush && st.buffer.Len() > 0 {
LOG.Trace("Flushing the remaining words into a sentence")
st.nsentence++
st.buffer.sentID = strconv.Itoa(st.nsentence)
ls.PushBack(st.buffer)
nsentence := st.nsentence
this.CloseSession(st)
st = this.OpenSession()
st.nsentence = nsentence
}
}
func (this *Splitter) endOfSentence(w *list.Element, v *list.List) bool {
if w == v.Back() {
return true
} else {
r := w
r = r.Next()
f := r.Value.(*Word).getForm()
return strings.Title(f) == f || this.starters.Has(f)
}
}