-
Notifications
You must be signed in to change notification settings - Fork 20
/
tldr.go
executable file
·371 lines (331 loc) · 9.43 KB
/
tldr.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Dependencies:
go get github.com/alixaxel/pagerank
WARNING: This package is not thread safe, so you cannot use *Bag from many goroutines.
*/
package tldr
import (
"encoding/json"
"sort"
"strings"
"unicode"
"github.com/alixaxel/pagerank"
)
type Bag struct {
BagOfWordsPerSentence [][]string
OriginalSentences []string
Dict map[string]int
Nodes []*Node
Edges []*Edge
Ranks []int
MaxCharacters int
Algorithm string // "centrality" or "pagerank" or "custom"
Weighing string // "hamming" or "jaccard" or "custom"
Damping float64
Tolerance float64
Threshold float64
SentencesDistanceThreshold float64
customAlgorithm func(e []*Edge) []int
customWeighing func(src, dst []int) float64
wordTokenizer func(sentence string) []string
vectorLength int
}
func (b *Bag) String() string {
r, _ := json.MarshalIndent(b, "", " ")
return string(r)
}
// The default values of each settings
const (
VERSION = "0.6.0"
DEFAULT_ALGORITHM = "pagerank"
DEFAULT_WEIGHING = "hamming"
DEFAULT_DAMPING = 0.85
DEFAULT_TOLERANCE = 0.0001
DEFAULT_THRESHOLD = 0.001
DEFAULT_MAX_CHARACTERS = 0
DEFAULT_SENTENCES_DISTANCE_THRESHOLD = 0.95
)
func defaultWordTokenizer(sentence string) []string {
words := strings.Fields(sentence)
for i, word := range words {
words[i] = SanitizeWord(word)
}
return words
}
// New creates a new summarizer
func New() *Bag {
return &Bag{
MaxCharacters: DEFAULT_MAX_CHARACTERS,
Algorithm: DEFAULT_ALGORITHM,
Weighing: DEFAULT_WEIGHING,
Damping: DEFAULT_DAMPING,
Tolerance: DEFAULT_TOLERANCE,
Threshold: DEFAULT_THRESHOLD,
SentencesDistanceThreshold: DEFAULT_SENTENCES_DISTANCE_THRESHOLD,
wordTokenizer: defaultWordTokenizer,
}
}
// Set max characters, damping, tolerance, threshold, sentences distance threshold, algorithm, and weighing
func (bag *Bag) Set(m int, d, t, th, sth float64, alg, w string) {
bag.MaxCharacters = m
bag.Damping = d
bag.Tolerance = t
bag.Threshold = th
bag.Algorithm = alg
bag.Weighing = w
bag.SentencesDistanceThreshold = sth
}
// Useful if you already have your own dictionary (example: from your database)
// Dictionary is a map[string]int where the key is the word and int is the position in vector, starting from 1
func (bag *Bag) SetDictionary(dict map[string]int) {
bag.Dict = dict
}
func (bag *Bag) SetCustomAlgorithm(f func(e []*Edge) []int) {
bag.customAlgorithm = f
}
func (bag *Bag) SetCustomWeighing(f func(src, dst []int) float64) {
bag.customWeighing = f
}
func (bag *Bag) SetWordTokenizer(f func(string) []string) {
bag.wordTokenizer = f
}
// Summarize the text to num sentences
func (bag *Bag) Summarize(text string, num int) ([]string, error) {
text = strings.TrimSpace(text)
if len(text) < 1 && len(bag.OriginalSentences) == 0 {
return nil, nil
}
bag.createSentences(text) // only actually creates sentences if no OrignalSentences
// If user already provide dictionary, pass creating dictionary
if len(bag.Dict) < 1 {
if text == "" {
text = strings.TrimSpace(strings.Join(bag.OriginalSentences, " "))
}
bag.createDictionary(text)
}
bag.createNodes()
bag.createEdges()
switch bag.Algorithm {
case "centrality":
bag.centrality()
case "pagerank":
bag.pageRank()
case "custom":
bag.Ranks = bag.customAlgorithm(bag.Edges)
default:
bag.pageRank()
}
// if no ranks, return error
lenRanks := len(bag.Ranks)
if lenRanks == 0 {
return nil, nil
}
// guard so it won't crash but return only the highest rank sentence
// if num is invalid
if num > lenRanks || num < 1 {
num = 1
}
// get only top num of ranks
idx := bag.Ranks[:num]
// sort it ascending by how the sentences appeared on the original text
sort.Ints(idx)
return bag.concatResult(idx), nil
}
// concatenate sentences at idx to result string
func (bag *Bag) concatResult(idx []int) []string {
var res []string
if bag.MaxCharacters > 0 {
lenRes := 0
for i := range idx {
lenOrig := len([]rune(bag.OriginalSentences[idx[i]]))
if lenRes+lenOrig <= bag.MaxCharacters {
res = append(res, bag.OriginalSentences[idx[i]])
} else {
n := bag.MaxCharacters - lenRes
if n > lenOrig {
n = lenOrig
}
res = append(res, string([]rune(bag.OriginalSentences[idx[i]])[:n]))
break
}
lenRes += lenOrig
}
return res
}
for i := range idx {
res = append(res, bag.OriginalSentences[idx[i]])
}
return res
}
type Rank struct {
idx int
score float64
}
func (bag *Bag) centrality() {
// first remove edges under Threshold weight
var newEdges []*Edge
for _, edge := range bag.Edges {
if edge.weight > bag.Threshold {
newEdges = append(newEdges, edge)
}
}
// sort them by weight descending
sort.Sort(ByWeight(newEdges))
ReverseEdge(newEdges)
rankBySrc := make([]int, len(newEdges))
for i, v := range newEdges {
rankBySrc[i] = v.src
}
// uniq it without disturbing the order
m := make(map[int]bool)
var uniq []int
for _, v := range rankBySrc {
if m[v] {
continue
}
uniq = append(uniq, v)
m[v] = true
}
bag.Ranks = uniq
}
func (bag *Bag) pageRank() {
// first remove edges under Threshold weight
var newEdges []*Edge
for _, edge := range bag.Edges {
if edge.weight > bag.Threshold {
newEdges = append(newEdges, edge)
}
}
// then page rank them
graph := pagerank.NewGraph()
defer graph.Reset()
for _, edge := range newEdges {
graph.Link(uint32(edge.src), uint32(edge.dst), edge.weight)
}
var ranks []*Rank
graph.Rank(bag.Damping, bag.Tolerance, func(sentenceIndex uint32, rank float64) {
ranks = append(ranks, &Rank{int(sentenceIndex), rank})
})
// sort ranks into an array of sentence index, by score descending
sort.Sort(ByScore(ranks))
ReverseRank(ranks)
idx := make([]int, len(ranks))
for i, v := range ranks {
idx[i] = v.idx
}
bag.Ranks = idx
}
type Edge struct {
src int // index of node
dst int // index of node
weight float64 // weight of the similarity between two sentences
}
func (bag *Bag) createEdges() {
for i, src := range bag.Nodes {
for j, dst := range bag.Nodes {
// don't compare same node
if i != j {
var weight float64
switch bag.Weighing {
case "jaccard":
commonElements := Intersection(src.vector, dst.vector)
weight = 1.0 - float64(len(commonElements))/((float64(bag.vectorLength)*2)-float64(len(commonElements)))
case "hamming":
differentElements := SymmetricDifference(src.vector, dst.vector)
weight = float64(len(differentElements))
case "custom":
weight = bag.customWeighing(src.vector, dst.vector)
default:
differentElements := SymmetricDifference(src.vector, dst.vector)
weight = float64(len(differentElements))
}
edge := &Edge{i, j, weight}
bag.Edges = append(bag.Edges, edge)
}
}
}
}
type Node struct {
sentenceIndex int // index of sentence from the bag
vector []int // map of word count in respect with dict, should we use map instead of slice?
// for example :
/*
dict = {
i : 1
am : 2
the : 3
shit : 4
}
str = "I am not shit, you effin shit"
vector = [1, 1, 0, 2] => [1, 1, 0, 1] because should be binary
*/
}
func (bag *Bag) createNodes() {
bag.vectorLength = len(bag.Dict)
for i, sentence := range bag.BagOfWordsPerSentence {
// vector length is len(dict)
vector := make([]int, bag.vectorLength)
// word for word now
for _, word := range sentence {
// check word dict position, if doesn't exist, skip
if bag.Dict[word] == 0 {
continue
}
// minus 1, because array started from 0 and lowest dict is 1
pos := bag.Dict[word] - 1
// set 1 to the position
vector[pos] = 1
}
// vector is now created, put it into the node
node := &Node{i, vector}
// node is now completed, put into the bag
bag.Nodes = append(bag.Nodes, node)
}
}
func (bag *Bag) createSentences(text string) {
if len(bag.OriginalSentences) == 0 {
// trim all spaces
// done by calling func: text = strings.TrimSpace(text)
// tokenize text as sentences
// sentence is a group of words separated by whitespaces or punctuation other than !?.
bag.OriginalSentences = TokenizeSentences(text)
}
// from original sentences, explode each sentences into bag of words
bag.BagOfWordsPerSentence = [][]string{}
for _, sentence := range bag.OriginalSentences {
words := bag.wordTokenizer(sentence)
bag.BagOfWordsPerSentence = append(bag.BagOfWordsPerSentence, words)
}
// then uniq it
UniqSentences(bag.BagOfWordsPerSentence, bag.SentencesDistanceThreshold)
}
func (bag *Bag) createDictionary(text string) {
// trim all spaces
// this already done by calling func: text = strings.TrimSpace(text)
// lowercase the text
text = strings.ToLower(text)
// remove all non alphanumerics but spaces
var prev rune
text = strings.Map(func(r rune) rune {
if r == '-' && (unicode.IsDigit(prev) || unicode.IsLetter(prev)) {
return r
}
if !unicode.IsDigit(r) && !unicode.IsLetter(r) && !unicode.IsSpace(r) {
return -1
}
prev = r
return r
}, text)
// turn it into bag of words
words := strings.Fields(text)
// turn it into dictionary
dict := make(map[string]int)
i := 1
for _, word := range words {
if dict[word] == 0 {
dict[word] = i
i++
}
}
bag.Dict = dict
}