Skip to content

A Swift package that implements the 'TextRank' algorithm for text summarization.

Notifications You must be signed in to change notification settings

jhrcook/TextRank

Repository files navigation

TextRank

Swift pre-commit SwiftFormat swift test

A Swift package that implements the 'TextRank' algorithm for summarization. This algorithm uses the PageRank (PR) algorithm to rank nodes by centrality in a weighted, undirected network where the nodes are sentences and edges indicate the degree of similarity of two sentences.

This package is functional, but young. Please open an issue if you find any bugs or have any feature requests.

Stop words were acquired from Ranks NL. Please open an issue to request additional language support.

Example

import TextRank

let textRank = TextRank(text: myParagraph)
let rankedResults = textRank.runPageRank()

The rankedResults is a TextGraph.PageRankResult struct with three properties:

  1. didConverge: whether or not the PR algorithm converged
  2. iterations: the number of iterations required for the PR algorithm to converge
  3. results: a TextGraph.NodeList which is a type alias for [Sentence: Float] that holds the final rankings from the PR algorithm

The PageRankResult.results object holds the final node list after running PR. Under the hood, it is a dictionary mapping each sentence to a rank. The keys are of type Sentence which has properties for the original sentence text and the set of words in the sentence words. Below is an example of how to obtain an array of sentences sorted by their rankings in decreasing order.

let sortedSentences: [String] = rankedResults
	.results
	.sorted { $0.value < $1.value }
	.map { $0.key }

Similar projects

This code base is based off of the original Python implementation. These is another Swift package, 'SwiftTextRank' that implements this algorithm.