Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Automatic summarizer text in Swift

License

NotificationsYou must be signed in to change notification settings

fdzsergio/Reductio

Repository files navigation

Reductio Logo

Text Summarization and Keyword Extraction for Swift

Swift 6.0MIT LicenseSwift Package Manager
Platform Support

FeaturesRequirementsInstallationUsageDocumentationContributing

Overview

Reductio is a high-performance Swift library that implements theTextRank 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.

What is TextRank?

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.

Use Cases

  • 📰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

Features

Core Capabilities

  • 🔑 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

Technical Highlights

  • Modern Swift APIs: Native async/await support
  • Value Semantics: Immutable structs forKeyword 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

Requirements

  • Swift: 6.0+
  • Xcode: 15.0+
  • Platforms:
    • iOS 13.0+
    • macOS 12.0+
    • tvOS 13.0+
    • watchOS 6.0+

Installation

Swift Package Manager

Add Reductio to yourPackage.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"])]

Usage

Quick Start

import Reductiolettext="""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 andasync/await improvements that make concurrent programming more intuitive."""// Extract top keywordsletkeywords=awaitReductio.keywords(from: text, count:5)print(keywords)  // ["swift", "developers", "improvements", "concurrency", "features"]// Get a summaryletsummary=awaitReductio.summarize(text: text, count:2)summary.forEach{print("\($0)")}

Keyword Extraction

Basic Usage

// Extract all keywords ranked by importanceletallKeywords=awaitReductio.keywords(from: text)// Get top 10 keywordslettopKeywords=awaitReductio.keywords(from: text, count:10)// Extract keywords with 70% compression (top 30% of keywords)letcompressedKeywords=awaitReductio.keywords(from: text, compression:0.7)

Using String Extensions

// Synchronous keyword extractionletkeywords= text.keywords// Custom configurationlettopKeywords= text.keywords(count:5)

Text Summarization

Sentence Extraction

// Get all sentences ranked by importanceletrankedSentences=awaitReductio.summarize(text: text)// Extract top 3 most important sentencesletsummary=awaitReductio.summarize(text: text, count:3)// Summarize with 80% compression (keep 20% of sentences)letconciseSummary=awaitReductio.summarize(text: text, compression:0.8)

String Extension

// Quick summarizationletsummary= text.summarize// Custom lengthletshortSummary= text.summarize(count:2)

Advanced Examples

Document Analysis Pipeline

structDocumentAnalyzer{staticfunc analyze(_ document:String)async->DocumentInsights{asyncletkeywords=Reductio.keywords(from: document, count:10)asyncletsummary=Reductio.summarize(text: document, count:5)returnawaitDocumentInsights(      keywords: keywords,      summary: summary,      readingTime:estimateReadingTime(document))}}

SEO Content Optimizer

extensionString{func seoAnalysis()async->SEOReport{letkeywords=awaitReductio.keywords(from:self, count:20)letkeywordDensity=calculateDensity(keywords: keywords, in:self)returnSEOReport(      primaryKeywords:Array(keywords.prefix(5)),      secondaryKeywords:Array(keywords.dropFirst(5)),      keywordDensity: keywordDensity,      suggestedMetaDescription:self.summarize(count:1).first??"")}}

Documentation

Get started with documentation athttps://fdzsergio.github.io/Reductio/

How TextRank Works

  1. 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
  2. Graph Construction

    • Each word/sentence becomes a vertex
    • Edges connect co-occurring elements
    • Edge weights represent semantic similarity
  3. Iterative Ranking

    • Initial scores assigned to all vertices
    • Scores propagate through edges
    • Convergence achieved after ~30 iterations
  4. Result Extraction

    • Vertices ranked by final scores
    • Top elements returned as keywords/summary

Performance Considerations

Document SizeProcessing TimeMemory 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

Contributing

We welcome contributions! Please see ourContributing Guide for details.

Development Setup

# Clone the repositorygit clone https://github.com/fdzsergio/Reductio.gitcd Reductio# Build the projectswift build# Run testsswifttest# Generate documentationswift package plugin generate-documentation

Code Style

  • Follow Swift API Design Guidelines
  • Use swift-format for consistency
  • Write tests for new features
  • Update documentation as needed

License

Reductio is released under the MIT License. SeeLICENSE for details.

Acknowledgments

Contact

Sergio Fernández
📧fdz.sergio@gmail.com
🐦@fdzsergio
💼LinkedIn


Made with ❤️ and Swift

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp