-
Notifications
You must be signed in to change notification settings - Fork 9
/
summarize.go
219 lines (174 loc) · 4.42 KB
/
summarize.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
package summarize
import (
"io"
"math"
"strings"
)
type Summarize struct {
Title string
Text string
Language string
StopWordsProvider StopWordsProvider
TextSplitter TextSplitter
EndSentenceRunes []rune
QuoteTuples [][]rune
IdealWordCount int
}
func New(title string, r io.Reader) Summarize {
buffer := getBuffer()
defer bufferPool.Put(buffer)
buffer.ReadFrom(r)
return NewFromString(title, buffer.String())
}
func NewFromString(title, text string) Summarize {
return Summarize{
Title: title,
Text: text,
Language: "en",
StopWordsProvider: DefaultStopWords{},
TextSplitter: DefaultTextSplitter{[]rune{'.', '!', '?'}},
IdealWordCount: 20,
}
}
func (s Summarize) KeyPoints() []string {
s.StopWordsProvider.SetLanguage(s.Language)
sentences := s.TextSplitter.Sentences(s.Text)
keywords := s.keywords(s.Text)
titleWords := toLower(s.TextSplitter.Words(s.Title))
if len(sentences) <= 5 {
return sentences
}
ranks := TextCounter{}
titleMap := map[string]bool{}
for _, t := range titleWords {
if !s.StopWordsProvider.IsStopWord(t) {
titleMap[t] = true
}
}
for i, sent := range sentences {
words := toLower(s.TextSplitter.Words(sent))
titleScore := s.titleScore(titleMap, words)
lengthScore := s.lengthScore(words)
positionScore := s.positionScore(i+1, len(sentences))
sbs := s.sbs(words, keywords)
dbs := s.dbs(words, keywords)
freq := (sbs + dbs) / 2 * 10
total := (titleScore*1.5 + freq*2 + lengthScore + positionScore) / 4
ranks.Add(sent, int(total*100))
}
var keyPoints []string
mostCommon := ranks.MostCommon(5)
commonMap := map[string]bool{}
for _, p := range mostCommon {
commonMap[p.Text] = true
}
for _, sent := range sentences {
if commonMap[sent] {
keyPoints = append(keyPoints, sent)
}
}
return keyPoints
}
func (s Summarize) keywords(text string) map[string]float64 {
allWords := toLower(s.TextSplitter.Words(text))
allLen := float64(len(allWords))
filteredWords := []string{}
for _, w := range allWords {
if !s.StopWordsProvider.IsStopWord(w) {
filteredWords = append(filteredWords, w)
}
}
freq := NewTextCounterFromSlice(filteredWords)
pairs := freq.MostCommon(10)
keyMap := map[string]float64{}
for _, p := range pairs {
score := float64(p.Count) / allLen
keyMap[p.Text] = score*1.5 + 1
}
return keyMap
}
func (s Summarize) titleScore(titleMap map[string]bool, words []string) float64 {
count := 0.0
for _, w := range words {
if _, ok := titleMap[w]; ok && !s.StopWordsProvider.IsStopWord(w) {
count += 1
}
}
return count / float64(len(titleMap))
}
func (s Summarize) lengthScore(words []string) float64 {
return 1 - math.Abs(float64(s.IdealWordCount-len(words)))/float64(s.IdealWordCount)
}
func (s Summarize) positionScore(pos, total int) float64 {
normalized := float64(pos) / float64(total)
if normalized < 0 {
normalized = 0
}
if normalized <= 0.1 {
return 0.17
} else if normalized <= 0.2 {
return 0.23
} else if normalized <= 0.3 {
return 0.14
} else if normalized <= 0.4 {
return 0.08
} else if normalized <= 0.5 {
return 0.05
} else if normalized <= 0.6 {
return 0.04
} else if normalized <= 0.7 {
return 0.06
} else if normalized <= 0.8 {
return 0.04
} else if normalized <= 0.9 {
return 0.04
} else if normalized <= 1.0 {
return 0.15
} else {
return 0
}
}
func (s Summarize) sbs(words []string, keywords map[string]float64) float64 {
score := 0.0
if len(words) == 0 {
return score
}
for _, w := range words {
if c, ok := keywords[w]; ok {
score += c
}
}
return (1 / float64(len(words)) * score) / 10
}
func (s Summarize) dbs(words []string, keywords map[string]float64) float64 {
score := 0.0
if len(words) == 0 {
return score
}
summ := 0.0
first := [2]float64{}
second := [2]float64{}
uniqueWords := map[string]bool{}
for i, w := range words {
if c, ok := keywords[w]; ok {
if len(first) == 0 {
first[0], first[1] = float64(i), c
} else {
second[0], second[1] = first[0], first[1]
first[0], first[1] = float64(i), c
diff := first[0] - second[0]
summ += first[1] * second[1] / math.Pow(diff, 2)
}
uniqueWords[w] = true
}
}
k := float64(len(uniqueWords) + 1)
return (1 / (k * (k + 1)) * summ)
}
func toLower(words []string) []string {
var lower []string
for _, w := range words {
lower = append(lower, strings.ToLower(w))
}
return lower
}