-
Notifications
You must be signed in to change notification settings - Fork 22
/
doc.go
445 lines (347 loc) · 11.6 KB
/
doc.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
Package textrank is an implementation of Text Rank algorithm in Go with
extendable features (automatic summarization, phrase extraction). It supports
multithreading by goroutines. The package is under The MIT Licence.
MOTIVATION
If there was a program what could rank book size text's words, phrases and
sentences continuously on multiple threads and it would be opened to modifing by
objects, written in a simple, secure, static language and if it would be very
well documented... Now, here it is.
FEATURES
- Find the most important phrases.
- Find the most important words.
- Find the most important N sentences.
- Importance by phrase weights.
- Importance by word occurrence.
- Find the first N sentences, start from Xth sentence.
- Find sentences by phrase chains ordered by position in text.
- Access to the whole ranked data.
- Support more languages.
- Algorithm for weighting can be modified by interface implementation.
- Parser can be modified by interface implementation.
- Multi thread support.
EXAMPLES
Find the most important phrases:
This is the most basic and simplest usage of textrank.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmDef)
// Get all phrases by weight.
rankedPhrases := textrank.FindPhrases(tr)
// Most important phrase.
fmt.Println(rankedPhrases[0])
// Second important phrase.
fmt.Println(rankedPhrases[1])
}
All possible pre-defined finder queries:
After ranking, the graph contains a lot of valuable data. There are functions in
textrank package what contains logic to retrieve those data from the graph.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmDef)
// Get all phrases order by weight.
rankedPhrases := textrank.FindPhrases(tr)
// Most important phrase.
fmt.Println(rankedPhrases[0])
// Get all words order by weight.
words := textrank.FindSingleWords(tr)
// Most important word.
fmt.Println(words[0])
// Get the most important 10 sentences. Importance by phrase weights.
sentences := textrank.FindSentencesByRelationWeight(tr, 10)
// Found sentences
fmt.Println(sentences)
// Get the most important 10 sentences. Importance by word occurrence.
sentences = textrank.FindSentencesByWordQtyWeight(tr, 10)
// Found sentences
fmt.Println(sentences)
// Get the first 10 sentences, start from 5th sentence.
sentences = textrank.FindSentencesFrom(tr, 5, 10)
// Found sentences
fmt.Println(sentences)
// Get sentences by phrase/word chains order by position in text.
sentencesPh := textrank.FindSentencesByPhraseChain(tr, []string{"gnome", "shell", "extension"})
// Found sentence.
fmt.Println(sentencesPh[0])
}
Access to everything
After ranking, the graph contains a lot of valuable data. The GetRank function
allows access to the graph and every data can be retrieved from this structure.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmDef)
// Get the rank graph.
rankData := tr.GetRankData()
// Get word ID by token/word.
wordId := rankData.WordValID["gnome"]
// Word's weight.
fmt.Println(rankData.Words[wordId].Weight)
// Word's quantity/occurrence.
fmt.Println(rankData.Words[wordId].Qty)
// All sentences what contain the this word.
fmt.Println(rankData.Words[wordId].SentenceIDs)
// All other words what are related to this word on left side.
fmt.Println(rankData.Words[wordId].ConnectionLeft)
// All other words what are related to this word on right side.
fmt.Println(rankData.Words[wordId].ConnectionRight)
// The node of this word, it contains the related words and the
// relation weight.
fmt.Println(rankData.Relation.Node[wordId])
}
Adding text continuously:
It is possibe to add more text after another texts already have been added. The
Ranking function can merge these multiple texts and it can recalculate the
weights and all related data.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmDef)
rawText2 := "Another book or article..."
rawText3 := "Third another book or article..."
// Add text to the previously added text.
tr.Populate(rawText2, language, rule)
// Add text to the previously added text.
tr.Populate(rawText3, language, rule)
// Run the ranking to the whole composed text.
tr.Ranking(algorithmDef)
// Get all phrases by weight.
rankedPhrases := textrank.FindPhrases(tr)
// Most important phrase.
fmt.Println(rankedPhrases[0])
// Second important phrase.
fmt.Println(rankedPhrases[1])
}
Using different algorithm to ranking text:
There are two algorithm has implemented, it is possible to write custom
algorithm by Algorithm interface and use it instead of defaults.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Using a little bit more complex algorithm to ranking text.
algorithmChain := textrank.NewChainAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmChain)
// Get all phrases by weight.
rankedPhrases := textrank.FindPhrases(tr)
// Most important phrase.
fmt.Println(rankedPhrases[0])
// Second important phrase.
fmt.Println(rankedPhrases[1])
}
Using multiple graphs:
Graph ID exists because it is possible run multiple independent text ranking
processes.
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// 1th TextRank object
tr1 := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr1.Populate(rawText, language, rule)
// Run the ranking.
tr1.Ranking(algorithmDef)
// 2nd TextRank object
tr2 := textrank.NewTextRank()
// Using a little bit more complex algorithm to ranking text.
algorithmChain := textrank.NewChainAlgorithm()
// Add text to the second graph.
tr2.Populate(rawText, language, rule)
// Run the ranking on the second graph.
tr2.Ranking(algorithmChain)
// Get all phrases by weight from first graph.
rankedPhrases := textrank.FindPhrases(tr1)
// Most important phrase from first graph.
fmt.Println(rankedPhrases[0])
// Second important phrase from first graph.
fmt.Println(rankedPhrases[1])
// Get all phrases by weight from second graph.
rankedPhrases2 := textrank.FindPhrases(tr2)
// Most important phrase from second graph.
fmt.Println(rankedPhrases2[0])
// Second important phrase from second graph.
fmt.Println(rankedPhrases2[1])
}
Using different non-English languages:
Engish is used by default but it is possible to add any language. To use other
languages a stop word list is required what you can find here:
https://github.com/stopwords-iso
package main
import (
"fmt"
"github.com/DavidBelicza/TextRank"
)
func main() {
rawText := "Your long raw text, it could be a book. Lorem ipsum..."
// TextRank object
tr := textrank.NewTextRank()
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Add Spanish stop words (just some example).
language.SetWords("es", []string{"uno", "dos", "tres", "yo", "es", "eres"})
// Active the Spanish.
language.SetActiveLanguage("es")
// Default algorithm for ranking text.
algorithmDef := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithmDef)
// Get all phrases by weight.
rankedPhrases := textrank.FindPhrases(tr)
// Most important phrase.
fmt.Println(rankedPhrases[0])
// Second important phrase.
fmt.Println(rankedPhrases[1])
}
Asynchronous usage by goroutines:
It is thread safe. Independent graphs can receive texts in the same time and can
be extended by more text also in the same time.
package main
import (
"fmt"
"time"
"github.com/DavidBelicza/TextRank"
)
func main() {
// A flag when program has to stop.
stopProgram := false
// Channel.
stream := make(chan string)
// TextRank object.
tr := textrank.NewTextRank()
// Open new thread/routine
go func(tr *textrank.TextRank) {
// 3 texts.
rawTexts := []string{
"Very long text...",
"Another very long text...",
"Second another very long text...",
}
// Add 3 texts to the stream channel, one by one.
for _, rawText := range rawTexts {
stream <- rawText
}
}(tr)
// Open new thread/routine
go func() {
// Counter how many times texts added to the ranking.
i := 1
for {
// Get text from stream channel when it got a new one.
rawText := <-stream
// Default Rule for parsing.
rule := textrank.NewDefaultRule()
// Default Language for filtering stop words.
language := textrank.NewDefaultLanguage()
// Default algorithm for ranking text.
algorithm := textrank.NewDefaultAlgorithm()
// Add text.
tr.Populate(rawText, language, rule)
// Run the ranking.
tr.Ranking(algorithm)
// Set stopProgram flag to true when all 3 text have been added.
if i == 3 {
stopProgram = true
}
i++
}
}()
// The main thread has to run while go-routines run. When stopProgram is
// true then the loop has finish.
for !stopProgram {
time.Sleep(time.Second * 1)
}
// Most important phrase.
phrases := textrank.FindPhrases(tr)
// Second important phrase.
fmt.Println(phrases[0])
}
*/
package textrank