{"id":16760946,"url":"https://github.com/davidbelicza/textrank","last_synced_at":"2025-10-12T00:40:55.112Z","repository":{"id":48849050,"uuid":"116861351","full_name":"DavidBelicza/TextRank","owner":"DavidBelicza","description":":wink: :cyclone: :strawberry: TextRank implementation in Golang with extendable features (summarization, phrase extraction) and multithreading (goroutine).","archived":false,"fork":false,"pushed_at":"2021-07-08T17:29:28.000Z","size":1376,"stargazers_count":214,"open_issues_count":5,"forks_count":22,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-07T16:13:29.722Z","etag":null,"topics":["go","golang","pagerank","phrase-extraction","sentence-classification","summarization","textrank","token"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DavidBelicza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-09T19:36:17.000Z","updated_at":"2025-05-26T02:00:28.000Z","dependencies_parsed_at":"2022-08-31T16:31:21.308Z","dependency_job_id":null,"html_url":"https://github.com/DavidBelicza/TextRank","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/DavidBelicza/TextRank","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidBelicza%2FTextRank","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidBelicza%2FTextRank/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidBelicza%2FTextRank/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidBelicza%2FTextRank/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DavidBelicza","download_url":"https://codeload.github.com/DavidBelicza/TextRank/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavidBelicza%2FTextRank/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279009509,"owners_count":26084609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","pagerank","phrase-extraction","sentence-classification","summarization","textrank","token"],"created_at":"2024-10-13T04:24:59.562Z","updated_at":"2025-10-12T00:40:55.105Z","avatar_url":"https://github.com/DavidBelicza.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\nTextRank on Go\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://godoc.org/github.com/DavidBelicza/TextRank\"\u003e\n\t\t\u003cimg src=\"https://godoc.org/github.com/DavidBelicza/TextRank?status.svg\" alt=\"GoDoc\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://github.com/DavidBelicza/TextRank/blob/master/LICENSE\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/License-MIT-ee00ee.svg\" alt=\"License: MIT\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://travis-ci.org/DavidBelicza/TextRank\"\u003e\n\t\t\u003cimg src=\"https://travis-ci.org/DavidBelicza/TextRank.svg?branch=master\" alt=\"Build Status\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://goreportcard.com/report/github.com/DavidBelicza/TextRank\"\u003e\n\t\t\u003cimg src=\"https://goreportcard.com/badge/github.com/DavidBelicza/TextRank\" alt=\"Go Report Card\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://coveralls.io/github/DavidBelicza/TextRank?branch=master\"\u003e\n\t\t\u003cimg src=\"https://coveralls.io/repos/github/DavidBelicza/TextRank/badge.svg?branch=master\" alt=\"Coverage Status\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://github.com/DavidBelicza/TextRank/releases/latest\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/github/release/DavidBelicza/TextRank.svg?colorB=269aca\" alt=\"Release\" /\u003e\n\t\u003c/a\u003e\n\t\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nThis source code is an implementation of the TextRank algorithm under the MIT license.\n\u003cbr /\u003eThe minimum required Go version is 1.8.\n\u003cp align=\"center\"\u003e\n\u003cbr /\u003e\t\n\t\n## MOTIVATION\n\nImagine a program that can continuously rank the words, phrases, and sentences of a book-sized text across multiple threads, can be modified through objects, is written in a simple, secure, static language, and is very well documented... Now, here it is.\n\n## FEATURES\n\n* Find the most important phrases.\n* Find the most important words.\n* Find the most important N sentences. \n\t* Importance by phrase weights.\n\t* Importance by word occurrence.\n* Find the first N sentences, start from Xth sentence.\n* Find sentences by phrase chains ordered by position in text.\n* Access to the whole ranked data.\n* Support more languages.\n* Algorithm for weighting can be modified by interface implementation.\n* Parser can be modified by interface implementation.\n* Multi-thread support.\n\n## INSTALL\n\nYou can install TextRank using `go get`:\n\n```go get github.com/DavidBelicza/TextRank/v2```\n\nTextRank uses Go modules for vendoring, so you can install the dependencies with this command:\n\n```go mod vendor```\n\n## DOCKER\n\nUsing Docker with TextRank isn't necessary; it's just an option.\n\nBuild image from the repository's root directory:\n\n```docker build -t go_text_rank_image .```\n\nCreate container from the image:\n\n```docker run -dit --name textrank go_text_rank_image:latest```\n\nRun the **go test -v .** code inside the container:\n\n```docker exec -i -t textrank go test -v .```\n\nStop, start or remove the container:\n\n* ```docker stop textrank```\n* ```docker start textrank```\n* ```docker rm textrank```\n\n## HOW DOES IT WORK\n\nTo see how it works, the easiest way is to use the sample text. You can find it in the [textrank_test.go file at this line](https://github.com/DavidBelicza/TextRank/blob/master/textrank_test.go#L12). It's a short text about Gnome Shell.\n\n* TextRank reads the text,\n    * parses it,\n    * removes the unnecessary stop words,\n    * tokenizes it\n* and counts the occurrences of the words and phrases\n* then it starts weighting\n    * by the occurrence of words and phrases and their relations\n* After weights are done, TextRank normalizes them between 1 and 0\n* Then the different finder methods can find the most important words, phrases, or sentences\n\nThe most important phrases from the sample text are:\n\nPhrase | Occurrence | Weight\n--- | --- | ---\ngnome - shell | 5 | 1\nextension - gnome | 3 | 0.50859946\nicons - tray | 3 | 0.49631447\ngnome - caffeine | 2 | 0.27027023\n\nThe word **gnome** appears most often in this text and **shell** is also used multiple times. Together they form a phrase used five times. This is the highest occurrence in the text, so it is the most important phrase.\n\nThe next two important phrases each occur three times, but they are not equal. The phrase **extension gnome** contains the word **gnome**, the most popular word in the text, which increases its weight. This increase also affects related words but not enough to outrank other important phrases that don't contain **gnome**.\n\nThe exact algorithm can be found in the [algorithm.go file at this line](https://github.com/DavidBelicza/TextRank/blob/master/rank/algorithm.go#L65).\n\n## TEXTRANK OR AUTOMATIC SUMMARIZATION\n\u003e Automatic summarization is the process of reducing a text document with a computer program in order to create a summary that retains the most important points of the original document. Technologies that can make a coherent summary take into account variables such as length, writing style and syntax. Automatic data summarization is part of machine learning and data mining. The main idea of summarization is to find a representative subset of the data, which contains the information of the entire set. Summarization technologies are used in a large number of sectors in industry today. - Wikipedia\n\n## EXAMPLES\n\n### Find the most important phrases\n\nThis is the most basic and simplest use of TextRank.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmDef)\n\n\t// Get all phrases by weight.\n\trankedPhrases := textrank.FindPhrases(tr)\n\n\t// Most important phrase.\n\tfmt.Println(rankedPhrases[0])\n\t// Second important phrase.\n\tfmt.Println(rankedPhrases[1])\n}\n```\n\n### All possible pre-defined finder queries\n\nAfter ranking, the graph contains a lot of valuable data. The TextRank package provides functions that retrieve this data from the graph.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmDef)\n\n\t// Get all phrases order by weight.\n\trankedPhrases := textrank.FindPhrases(tr)\n\t// Most important phrase.\n\tfmt.Println(rankedPhrases[0])\n\n\t// Get all words order by weight.\n\twords := textrank.FindSingleWords(tr)\n\t// Most important word.\n\tfmt.Println(words[0])\n\n\t// Get the most important 10 sentences. Importance by phrase weights.\n\tsentences := textrank.FindSentencesByRelationWeight(tr, 10)\n\t// Found sentences\n\tfmt.Println(sentences)\n\n\t// Get the most important 10 sentences. Importance by word occurrence.\n\tsentences = textrank.FindSentencesByWordQtyWeight(tr, 10)\n\t// Found sentences\n\tfmt.Println(sentences)\n\n\t// Get the first 10 sentences, start from 5th sentence.\n\tsentences = textrank.FindSentencesFrom(tr, 5, 10)\n\t// Found sentences\n\tfmt.Println(sentences)\n\n\t// Get sentences by phrase/word chains order by position in text.\n\tsentencesPh := textrank.FindSentencesByPhraseChain(tr, []string{\"gnome\", \"shell\", \"extension\"})\n\t// Found sentence.\n\tfmt.Println(sentencesPh[0])\n}\n```\n\n### Access to everything\n\nAfter ranking, the graph contains a lot of valuable data. The GetRank function provides access to the graph so every piece of data can be retrieved from this structure.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmDef)\n\n\t// Get the rank graph.\n\trankData := tr.GetRankData()\n\n\t// Get word ID by token/word.\n\twordId := rankData.WordValID[\"gnome\"]\n\n\t// Word's weight.\n\tfmt.Println(rankData.Words[wordId].Weight)\n\t// Word's quantity/occurrence.\n\tfmt.Println(rankData.Words[wordId].Qty)\n\t// All sentences what contain the this word.\n\tfmt.Println(rankData.Words[wordId].SentenceIDs)\n\t// All other words what are related to this word on left side.\n\tfmt.Println(rankData.Words[wordId].ConnectionLeft)\n\t// All other words what are related to this word on right side.\n\tfmt.Println(rankData.Words[wordId].ConnectionRight)\n\t// The node of this word, it contains the related words and the relation weight.\n\tfmt.Println(rankData.Relation.Node[wordId])\n}\n```\n\n### Adding text continuously\n\nIt is possible to add more text after other texts have already been added. The Ranking function can merge these texts and recalculate the weights and related data.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmDef)\n\n\trawText2 := \"Another book or article...\"\n\trawText3 := \"Third another book or article...\"\n\n\t// Add text to the previously added text.\n\ttr.Populate(rawText2, language, rule)\n\t// Add text to the previously added text.\n\ttr.Populate(rawText3, language, rule)\n\t// Run the ranking to the whole composed text.\n\ttr.Ranking(algorithmDef)\n\n\t// Get all phrases by weight.\n\trankedPhrases := textrank.FindPhrases(tr)\n\n\t// Most important phrase.\n\tfmt.Println(rankedPhrases[0])\n\t// Second important phrase.\n\tfmt.Println(rankedPhrases[1])\n}\n```\n\n### Using a different algorithm to rank text\n\nTwo algorithms are implemented, but you can write your own by implementing the Algorithm interface and use it instead of the defaults.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Using a little bit more complex algorithm to ranking text.\n\talgorithmChain := textrank.NewChainAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmChain)\n\n\t// Get all phrases by weight.\n\trankedPhrases := textrank.FindPhrases(tr)\n\n\t// Most important phrase.\n\tfmt.Println(rankedPhrases[0])\n\t// Second important phrase.\n\tfmt.Println(rankedPhrases[1])\n}\n```\n\n### Using multiple graphs\n\nGraph IDs exist because it is possible to run multiple independent text ranking processes.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// 1th TextRank object\n\ttr1 := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr1.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr1.Ranking(algorithmDef)\n\n\t// 2nd TextRank object\n\ttr2 := textrank.NewTextRank()\n\n\t// Using a little bit more complex algorithm to ranking text.\n\talgorithmChain := textrank.NewChainAlgorithm()\n\n\t// Add text to the second graph.\n\ttr2.Populate(rawText, language, rule)\n\t// Run the ranking on the second graph.\n\ttr2.Ranking(algorithmChain)\n\n\t// Get all phrases by weight from first graph.\n\trankedPhrases := textrank.FindPhrases(tr1)\n\n\t// Most important phrase from first graph.\n\tfmt.Println(rankedPhrases[0])\n\t// Second important phrase from first graph.\n\tfmt.Println(rankedPhrases[1])\n\n\t// Get all phrases by weight from second graph.\n\trankedPhrases2 := textrank.FindPhrases(tr2)\n\n\t// Most important phrase from second graph.\n\tfmt.Println(rankedPhrases2[0])\n\t// Second important phrase from second graph.\n\tfmt.Println(rankedPhrases2[1])\n}\n```\n\n### Using different non-English languages\n\nEnglish is used by default, but you can add any language. To use other languages, a stop word list is required, which you can find here: https://github.com/stopwords-iso\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\trawText := \"Your long raw text, it could be a book. Lorem ipsum...\"\n\t// TextRank object\n\ttr := textrank.NewTextRank()\n\t// Default Rule for parsing.\n\trule := textrank.NewDefaultRule()\n\t// Default Language for filtering stop words.\n\tlanguage := textrank.NewDefaultLanguage()\n\n\t// Add Spanish stop words (just some example).\n\tlanguage.SetWords(\"es\", []string{\"uno\", \"dos\", \"tres\", \"yo\", \"es\", \"eres\"})\n\t// Active the Spanish.\n\tlanguage.SetActiveLanguage(\"es\")\n\n\t// Default algorithm for ranking text.\n\talgorithmDef := textrank.NewDefaultAlgorithm()\n\n\t// Add text.\n\ttr.Populate(rawText, language, rule)\n\t// Run the ranking.\n\ttr.Ranking(algorithmDef)\n\n\t// Get all phrases by weight.\n\trankedPhrases := textrank.FindPhrases(tr)\n\n\t// Most important phrase.\n\tfmt.Println(rankedPhrases[0])\n\t// Second important phrase.\n\tfmt.Println(rankedPhrases[1])\n}\n```\n\n### Asynchronous usage by goroutines\n\nIt is thread-safe. Independent graphs can receive text at the same time and can be extended with more text concurrently.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\n\t\"github.com/DavidBelicza/TextRank/v2\"\n)\n\nfunc main() {\n\t// A flag when program has to stop.\n\tstopProgram := false\n\t// Channel.\n\tstream := make(chan string)\n\t// TextRank object.\n\ttr := textrank.NewTextRank()\n\n\t// Open new thread/routine\n\tgo func(tr *textrank.TextRank) {\n\t\t// 3 texts.\n\t\trawTexts := []string{\n\t\t\t\"Very long text...\",\n\t\t\t\"Another very long text...\",\n\t\t\t\"Second another very long text...\",\n\t\t}\n\n\t\t// Add 3 texts to the stream channel, one by one.\n\t\tfor _, rawText := range rawTexts {\n\t\t\tstream \u003c- rawText\n\t\t}\n\t}(tr)\n\n\t// Open new thread/routine\n\tgo func() {\n\t\t// Counter how many times texts added to the ranking.\n\t\ti := 1\n\n\t\tfor {\n\t\t\t// Get text from stream channel when it got a new one.\n\t\t\trawText := \u003c-stream\n\n\t\t\t// Default Rule for parsing.\n\t\t\trule := textrank.NewDefaultRule()\n\t\t\t// Default Language for filtering stop words.\n\t\t\tlanguage := textrank.NewDefaultLanguage()\n\t\t\t// Default algorithm for ranking text.\n\t\t\talgorithm := textrank.NewDefaultAlgorithm()\n\n\t\t\t// Add text.\n\t\t\ttr.Populate(rawText, language, rule)\n\t\t\t// Run the ranking.\n\t\t\ttr.Ranking(algorithm)\n\n\t\t\t// Set stopProgram flag to true when all 3 text have been added.\n\t\t\tif i == 3 {\n\t\t\t\tstopProgram = true\n\t\t\t}\n\n\t\t\ti++\n\t\t}\n\t}()\n\n\t// The main thread has to run while go-routines run. When stopProgram is\n\t// true then the loop has finish.\n\tfor !stopProgram {\n\t\ttime.Sleep(time.Second * 1)\n\t}\n\n\t// Most important phrase.\n\tphrases := textrank.FindPhrases(tr)\n\t// Second important phrase.\n\tfmt.Println(phrases[0])\n}\n```\n\n## A SIMPLE VISUAL REPRESENTATION\n\nThe image below illustrates how the simplest text ranking algorithm works. You can replace this algorithm by injecting a different Algorithm interface implementation.\n\n\u003cimg src=\"https://i.imgur.com/RUdDfBz.jpg\" /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidbelicza%2Ftextrank","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidbelicza%2Ftextrank","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidbelicza%2Ftextrank/lists"}