TextRank in Swift to swiftly test extractive summarization

Gary Bartos
4 min readMar 2, 2024
The Litany of Fear, taken from https://dune.fandom.com/wiki/Litany_Against_Fear. Short texts like the Litany of Fear process faster.

TextRank is an algorithm for extractive summarization of text. This short post is meant for folks who want to start tinkering with TextRank in Swift rather than in Python. Maybe you can figure out in 30 minutes whether TextRank will summarize text the way you need. And maybe I can help.

Here’s an implementation of TextRank you can get running with relatively little effort:

Credit to Eugene Honcharenko for the code. Maybe he’s busy with other stuff.

Documentation of the project and the code is sparse, but the code can be made to run in relatively little time:

  1. Use Swift Package Manager to get the code using the link https://github.com/goncharik/SwiftTextRank.git.
  2. Set up an Xcode project so that you can use SwiftTextRank in a Playground. If you have trouble with that, check out this StackOverflow post: https://stackoverflow.com/questions/60352804/how-to-import-swift-package-in-playground. Or just download the code and paste it into a Playground.
  3. Choose some text to process. It doesn’t have to be a Dune quote. Maybe it’s an article about that guy who keeps trying and failing to cross large bodies of water in a bubble.
  4. Copy and paste your text into the Playground text below.
  5. Run the code.
  6. Try some other text.
  7. Compare results from TextRank, BERT, ChatGPT, Gemini, and so on — what’ll work for you?

Here’s the sample code to try the SwiftTextRank project linked above:

//sample: Litany Against Fear from Dune by Frank Herbert
let text =
"""
I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past, I will turn the inner eye to see its path.
Where the fear has gone there will be nothing. Only I will remain.
"""

let summary = Summary(text)

let build = summary.build()

for item in build
{
print(item)
}

As user Accessibilly I posted that code in an Issue for the SwiftTextRank project. Here’s the issue page:

That’s it. The rest of this post is a mini retrospective. There are a few links. I don’t mention Dune again.

Got What I Needed, Off I Go

The extra newlines in the output might be the result of my copy & paste of sample text into the String for the variable text without cleaning the text first. I’m not going to fix that.

The code ran slowly for larger texts. A code comment mentions performance. It’s all good, man, just more than I need now.

The project worked well enough for my purposes, especially when I compared the results to output from BERT. I figured out what I needed to know this week about extractive summarization tools. Gotta move on.

Apple has a sample project for BERT. (Click the link, then scroll down the page). BERT didn’t do what I needed, but hey, I figured that out quickly enough.

Sometimes you need to test some algorithms before the weekend. Maybe you’re like me and you love writing code from scratch, but you also acknowledge the brevity of life. Download code, test it, credit the developer, and move on.

Others TextRank Projects You Might Get Running

Of the three “TextRank in Swift” projects in Github, the project above is the one I managed to get to work most quickly. It took a little effort to get running.

Here are two other projects in Github for TextRank in Swift.

What is TextRank?

This post isn’t a recipe post with a 10,000-word preamble about the history of text summarization, nor is it a substitute for the many fine descriptions of TextRank. If you’re here at all, you just want a quick fix.

But if you’re a completist and you made it this far, here are some links about TextRank.

Have fun!

--

--

Gary Bartos

Founder of Echobatix, engineer, inventor of assistive technology for people with disabilities. Keen on accessible gaming. echobatix@gmail.com