Text Summarization and Keyword Extraction for Swift
Features • Requirements • Installation • Usage • Documentation • Contributing
Reductio is a high-performance Swift library that implements the TextRank algorithm for automatic text summarization and keyword extraction. Built with Swift 6's strict concurrency in mind, it provides a modern, safe, and efficient way to analyze and extract meaning from text.
TextRank is an unsupervised graph-based ranking algorithm inspired by Google's PageRank. It builds a graph representation of text where vertices are words or sentences, and edges represent semantic relationships. Through iterative calculation, it identifies the most important elements in the text.
- 📰 News Summarization: Extract key points from articles
- 🔍 SEO Optimization: Identify important keywords for content
- 📚 Academic Research: Summarize research papers and extract key concepts
- 📱 Social Media: Analyze and summarize user-generated content
- 🏢 Business Intelligence: Process reports and extract insights
- 💬 Chatbots: Generate concise responses from knowledge bases
- 🔑 Keyword Extraction: Identify the most relevant keywords using graph-based ranking
- 📝 Text Summarization: Extract key sentences while preserving context
- 🌐 Language Support: Optimized for English with extensible architecture
- ⚡ High Performance: Efficient processing of documents up to 10,000+ words
- 🔒 Thread-Safe: Full Swift 6 strict concurrency compliance
- 📦 Zero Dependencies: Pure Swift implementation
- Modern Swift APIs: Native async/await support
- Value Semantics: Immutable structs for
Keyword
andSummarizer
- NaturalLanguage Framework: Leverages Apple's ML-powered text processing
- Flexible Configuration: Customizable compression ratios and result counts
- Extension-Friendly: Convenient String extensions for quick access
- Swift: 6.0+
- Xcode: 15.0+
- Platforms:
- iOS 13.0+
- macOS 12.0+
- tvOS 13.0+
- watchOS 6.0+
Add Reductio to your Package.swift
dependencies:
dependencies: [
.package(url: "https://github.com/fdzsergio/Reductio.git", from: "1.6.0")
]
Then add it to your target:
targets: [
.target(
name: "YourApp",
dependencies: ["Reductio"]
)
]
import Reductio
let text = """
Apple Inc. announced groundbreaking updates to its developer tools at WWDC 2024.
The company introduced Swift 6 with major improvements to concurrency and performance.
Developers praised the new features, particularly the enhanced type safety and
async/await improvements that make concurrent programming more intuitive.
"""
// Extract top keywords
let keywords = await Reductio.keywords(from: text, count: 5)
print(keywords) // ["swift", "developers", "improvements", "concurrency", "features"]
// Get a summary
let summary = await Reductio.summarize(text: text, count: 2)
summary.forEach { print("• \($0)") }
// Extract all keywords ranked by importance
let allKeywords = await Reductio.keywords(from: text)
// Get top 10 keywords
let topKeywords = await Reductio.keywords(from: text, count: 10)
// Extract keywords with 70% compression (top 30% of keywords)
let compressedKeywords = await Reductio.keywords(from: text, compression: 0.7)
// Synchronous keyword extraction
let keywords = text.keywords
// Custom configuration
let topKeywords = text.keywords(count: 5)
// Get all sentences ranked by importance
let rankedSentences = await Reductio.summarize(text: text)
// Extract top 3 most important sentences
let summary = await Reductio.summarize(text: text, count: 3)
// Summarize with 80% compression (keep 20% of sentences)
let conciseSummary = await Reductio.summarize(text: text, compression: 0.8)
// Quick summarization
let summary = text.summarize
// Custom length
let shortSummary = text.summarize(count: 2)
struct DocumentAnalyzer {
static func analyze(_ document: String) async -> DocumentInsights {
async let keywords = Reductio.keywords(from: document, count: 10)
async let summary = Reductio.summarize(text: document, count: 5)
return await DocumentInsights(
keywords: keywords,
summary: summary,
readingTime: estimateReadingTime(document)
)
}
}
extension String {
func seoAnalysis() async -> SEOReport {
let keywords = await Reductio.keywords(from: self, count: 20)
let keywordDensity = calculateDensity(keywords: keywords, in: self)
return SEOReport(
primaryKeywords: Array(keywords.prefix(5)),
secondaryKeywords: Array(keywords.dropFirst(5)),
keywordDensity: keywordDensity,
suggestedMetaDescription: self.summarize(count: 1).first ?? ""
)
}
}
Get started with documentation at https://fdzsergio.github.io/Reductio/
-
Text Preprocessing
- Sentence segmentation using NaturalLanguage framework
- Word tokenization and normalization
- Stopword removal (common words like "the", "is", "at")
- Lemmatization to reduce words to base forms
-
Graph Construction
- Each word/sentence becomes a vertex
- Edges connect co-occurring elements
- Edge weights represent semantic similarity
-
Iterative Ranking
- Initial scores assigned to all vertices
- Scores propagate through edges
- Convergence achieved after ~30 iterations
-
Result Extraction
- Vertices ranked by final scores
- Top elements returned as keywords/summary
Document Size | Processing Time | Memory Usage |
---|---|---|
100 words | ~10ms | ~1 MB |
1,000 words | ~50ms | ~5 MB |
10,000 words | ~500ms | ~20 MB |
For optimal performance:
- Process documents under 10,000 words
- Use compression ratios between 0.7-0.9
- Cache results for repeated analysis
- Consider chunking very large documents
We welcome contributions! Please see our Contributing Guide for details.
# Clone the repository
git clone https://github.com/fdzsergio/Reductio.git
cd Reductio
# Build the project
swift build
# Run tests
swift test
# Generate documentation
swift package plugin generate-documentation
- Follow Swift API Design Guidelines
- Use swift-format for consistency
- Write tests for new features
- Update documentation as needed
Reductio is released under the MIT License. See LICENSE for details.
- 🎨 Logo design by @cristinareina
- 📚 Based on TextRank by Rada Mihalcea and Paul Tarau
- 🙏 Thanks to all contributors
Sergio Fernández
📧 fdz.sergio@gmail.com
🐦 @fdzsergio
💼 LinkedIn
Made with ❤️ and Swift