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.
import TextRank
let textRank = TextRank(text: myParagraph)
let rankedResults = textRank.runPageRank()
The rankedResults
is a TextGraph.PageRankResult
struct with three properties:
didConverge
: whether or not the PR algorithm convergediterations
: the number of iterations required for the PR algorithm to convergeresults
: aTextGraph.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 }
This code base is based off of the original Python implementation. These is another Swift package, 'SwiftTextRank' that implements this algorithm.