Documentation ¶
Overview ¶
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]) }
Index ¶
- func FindPhrases(textRank *TextRank) []rank.Phrase
- func FindSentencesByPhraseChain(textRank *TextRank, phrases []string) []rank.Sentence
- func FindSentencesByRelationWeight(textRank *TextRank, limit int) []rank.Sentence
- func FindSentencesByWordQtyWeight(textRank *TextRank, limit int) []rank.Sentence
- func FindSentencesFrom(textRank *TextRank, sentenceID int, limit int) []rank.Sentence
- func FindSingleWords(textRank *TextRank) []rank.SingleWord
- func NewChainAlgorithm() *rank.AlgorithmChain
- func NewDefaultAlgorithm() *rank.AlgorithmDefault
- func NewDefaultLanguage() *convert.LanguageDefault
- func NewDefaultRule() *parse.RuleDefault
- type TextRank
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindPhrases ¶
FindPhrases function retrieves a slice of Phrase structures by TextRank object. The return value contains the sorted phrases with IDs, words, weights and quantities by weight from 1 to 0. Weight is calculated from quantities of relation between two words. A single phrase is from two words - not less and more. (But it's possible to find chain of phrases by FindSentencesByPhraseChain function.)
func FindSentencesByPhraseChain ¶
FindSentencesByPhraseChain function retrieves a slice of Sentence structures by TextRank object and slice of phrases. The return value contains the ID of the sentence and the sentence text itself. The slice is sorted by weight of word quantities from 1 to 0.
textRank TextRank is the object of the TextRank.
phrases []string is a slice of phrases. A single phrase is from two words, so when the slice contains 3 words the inner method will search for two phrases. The search algorithm seeks for "len(phrases)!". In case of three item the possible combination is 3 factorial (3!) = 3 * 2 * 1.
rawText := "Long raw text, lorem ipsum..." rule := NewDefaultRule() language := NewDefaultLanguage() algorithm := NewDefaultAlgorithm() Append(rawText, language, rule, 1) Ranking(1, algorithm) FindSentencesByPhraseChain(1, []string{ "captain", "james", "kirk", })
The above code searches for captain james kirk, captain kirk james, james kirk captain, james captain kirk, kirk james captain and james kirk captain combinations in the graph. The 3 of words have to be related to each other in the same sentence but the search algorithm ignores the stop words. So if there is a sentence "James Kirk is the Captain of the Enterprise." the sentence will be returned because the words "is" and "the" are stop words.
func FindSentencesByRelationWeight ¶
FindSentencesByRelationWeight function retrieves a slice of Sentence structures by TextRank object. The return value contains the ID of the sentence and the sentence text itself. The slice is sorted by weight of phrases from 1 to 0.
func FindSentencesByWordQtyWeight ¶
FindSentencesByWordQtyWeight function retrieves a slice of Sentence structures by TextRank object. The return value contains the ID of the sentence and the sentence text itself. The slice is sorted by weight of word quantities from 1 to 0.
func FindSentencesFrom ¶
FindSentencesFrom function retrieves a slice of Sentence structures by TextRank object and by ID of the sentence. The return value contains the sentence text itself. The returned slice contains sentences sorted by their IDs started from the given sentence ID in ascending sort.
func FindSingleWords ¶
func FindSingleWords(textRank *TextRank) []rank.SingleWord
FindSingleWords function retrieves a slice of SingleWord structures by TextRank object. The return value contains the sorted words with IDs, words, weights and quantities by weight from 1 to 0. Weight is calculated from quantities of word.
func NewChainAlgorithm ¶
func NewChainAlgorithm() *rank.AlgorithmChain
NewChainAlgorithm function retrieves an Algorithm object. It defines how should work the text ranking algorithm, the weighting. This is an alternative way to ranking words by weighting the number of the words. Because Algorithm is an interface it's possible to modify the ranking algorithm by inject different implementation. This is the 4th step to use TextRank.
func NewDefaultAlgorithm ¶
func NewDefaultAlgorithm() *rank.AlgorithmDefault
NewDefaultAlgorithm function retrieves an Algorithm object. It defines how should work the text ranking algorithm, the weighting. This is the general text rank by weighting the connection between the words to find the strongest phrases. Because Algorithm is an interface it's possible to modify the ranking algorithm by inject different implementation. This is the 4th step to use TextRank.
func NewDefaultLanguage ¶
func NewDefaultLanguage() *convert.LanguageDefault
NewDefaultLanguage function retrieves a default Language object. It defines what words are real and what words are just Stop Words or useless Junk Words. It uses the default English Stop Words, but it's possible to set different Stop Words in English or any other languages. Because Language is an interface it's possible to modify the ranking by inject different Language implementation. This is the 3rd step to use TextRank.
func NewDefaultRule ¶
func NewDefaultRule() *parse.RuleDefault
NewDefaultRule function retrieves a default Rule object what works in the most cases in English or similar Latin languages like French or Spanish. The Rule defines raw text how should be split to sentences and words. Because Rule is an interface it's possible modify the ranking by inject different Rule implementation. This is the 2nd step to use TextRank.
Types ¶
type TextRank ¶
type TextRank struct {
// contains filtered or unexported fields
}
TextRank structure contains the Rank data object. This structure is a wrapper around the whole text ranking functionality.
func NewTextRank ¶
func NewTextRank() *TextRank
NewTextRank constructor retrieves a TextRank pointer. This is the 1th step to use TextRank.
func (*TextRank) GetRankData ¶
GetRankData method retrieves the Rank data to that case if the developer want access to the whole graph and sentences, words, weights and all of the data to analyze it or just implement a new search logic or finder method.
func (*TextRank) Populate ¶
Populate method adds a raw text to the text-ranking graph. It parses, tokenize the raw text and prepares it to weighting and scoring. It's possible to append a new raw text to an existing one even if the previously text is already ranked. This is 5th step to use TextRank.
text string must be a plain text from TXT or PDF or any document, it can contain new lines, break lines or any unnecessary text parts, but it should not contain HTML tags or codes.
lang Language object can be loaded from NewDefaultLanguage function.
rule Rule object can be loaded from NewDefaultRule function.