-
Notifications
You must be signed in to change notification settings - Fork 0
/
excerpt_window.go
226 lines (205 loc) · 4.71 KB
/
excerpt_window.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
218
219
220
221
222
223
224
225
226
package excerpt
import (
"bytes"
"fmt"
"io"
"strings"
"unicode"
"unicode/utf8"
)
type TermScore struct {
Score float64
ByteLength uint32
}
func (t *TermScore) String() string {
return fmt.Sprintf("<TermScore: %v Length: %v>", t.Score, t.ByteLength)
}
type Match struct {
Start uint32
ByteLength uint32
Score float64
}
func (m *Match) String() string {
return fmt.Sprintf("<Match at: %v, Score: %v>", m.Start, m.Score)
}
type ExcerptWindowBM struct {
Start uint32
ByteLength uint32
CharLength uint32
Score float64
Text string
Matches []*Match
}
func (e *ExcerptWindowBM) String() string {
return fmt.Sprintf("<ExcerptWindowBM(charlength: %v|bytes: %v): starts at: %v score: %v>:\n%s\n", e.CharLength, e.ByteLength, e.Start, e.Score, e.Text)
}
func (e *ExcerptWindowBM) AddMatch(m *Match) bool {
if len(e.Matches) == 0 {
e.Start = m.Start
} else {
if m.Start > e.Start+e.ByteLength {
return false
}
}
e.Matches = append(e.Matches, m)
e.Score += m.Score
return true
}
func (e *ExcerptWindowBM) RemoveFirstMatch() {
if len(e.Matches) > 1 {
e.Score -= e.Matches[0].Score
e.Matches = e.Matches[1:]
e.Start = e.Matches[0].Start
} else {
e.Matches = []*Match{}
e.Start = 0
e.Score = 0
}
return
}
func (e *ExcerptWindowBM) AdjustWindow(body *strings.Reader, prependChars uint32,
prependFullwords bool) {
var bufSize int
body.Seek(int64(e.Matches[0].Start), 0)
// i am not sure why but i get positions that were not rune starts in
// multibyte character texts. until the reason is understood i will have to
// adjust the start position here...
var moveOffsets uint32 = 0
for {
b, err := body.ReadByte()
if err == io.EOF {
break
}
if utf8.RuneStart(b) {
break
}
moveOffsets += 1
body.Seek(-2, 1)
}
if moveOffsets > 0 {
e.Start -= moveOffsets
e.ByteLength += moveOffsets
for n, _ := range e.Matches {
e.Matches[n].Start -= moveOffsets
}
}
body.Seek(int64(e.Matches[0].Start), 0)
var rc uint32 = 0
for rc < e.CharLength {
_, size, err := body.ReadRune()
if err == io.EOF {
// bufSize -= 1
break
}
bufSize += size
rc += 1
}
e.ByteLength = uint32(bufSize)
for i := len(e.Matches) - 1; i > 1; i-- {
if e.Matches[i].Start > e.Start+e.ByteLength {
e.Score -= e.Matches[i].Score
e.Matches = e.Matches[:i]
} else {
break
}
}
if e.Matches[len(e.Matches)-1].Start+e.Matches[len(e.Matches)-1].ByteLength > e.Start+e.ByteLength {
e.ByteLength = (e.Matches[len(e.Matches)-1].Start + e.Matches[len(e.Matches)-1].ByteLength) - e.Start
}
if prependChars == 0 {
return
}
// extend window to show more context at the beginning of the excerpt
if e.Start == 0 {
return
}
if e.Start < prependChars {
e.ByteLength += e.Start
e.Start = 0
return
}
var moveStart uint32 = 0
var lastWhiteSpace uint32 = 0
var lastWhiteSpaceLength int = 0
body.Seek(int64(e.Start)-1, 0)
for {
b, err := body.ReadByte()
if err == io.EOF {
break
}
if !utf8.RuneStart(b) {
body.Seek(-2, 1)
continue
}
body.Seek(-1, 1)
r, rs, err := body.ReadRune()
if err == io.EOF {
break
}
moveStart += uint32(rs)
// log.Println("read rune:", string(r), "rs:", rs, "moveStart", moveStart)
if unicode.IsSpace(r) {
lastWhiteSpace = moveStart
lastWhiteSpaceLength = rs
}
if strings.Index(END_PUNCT_CHAR, string(r)) != -1 {
// log.Println("found END_PUNCT_CHAR")
moveStart -= uint32(rs)
if moveStart == lastWhiteSpace {
moveStart -= uint32(lastWhiteSpaceLength)
}
break
}
if moveStart >= prependChars {
// log.Println("moveStart >= prependChars")
if !unicode.IsSpace(r) && prependFullwords {
moveStart = lastWhiteSpace - uint32(lastWhiteSpaceLength)
}
break
}
spos := int64(-(rs + 1))
body.Seek(spos, 1)
}
e.Start -= moveStart
e.ByteLength += moveStart
}
func (e *ExcerptWindowBM) MaterializeWindow(body *strings.Reader) {
var buffer bytes.Buffer
var bc int = 0
// check that the last rune is not cut off
body.Seek(int64(e.Start+e.ByteLength-1), 0)
var lastRune int = 1
for {
b, err := body.ReadByte()
if err == io.EOF {
break
}
if !utf8.RuneStart(b) {
body.Seek(-2, 1)
lastRune += 1
continue
} else {
break
}
}
body.Seek(-1, 1)
_, rs, _ := body.ReadRune()
// log.Println("lastRune", lastRune, "rs", rs)
if lastRune != rs {
// log.Println("lastRune != rs", lastRune, rs, "add", rs-lastRune, "bytes")
e.ByteLength += uint32(rs - lastRune)
}
// body.Seek(int64(e.Matches[0].Start), 0)
body.Seek(int64(e.Start), 0)
for bc < int(e.ByteLength) {
b, err := body.ReadByte()
if err == io.EOF {
break
}
buffer.WriteByte(b)
bc += 1
}
e.Text = strings.TrimSpace(buffer.String())
e.ByteLength = uint32(len(e.Text))
// log.Println(e.Text)
}