summarization_demo/
summarization_demo.rs

1//! Text summarization demonstration
2
3use scirs2_text::{CentroidSummarizer, KeywordExtractor, TextRank};
4
5#[allow(dead_code)]
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("Text Summarization Demo");
8    println!("======================\n");
9
10    // Sample text for summarization
11    let text = "Artificial intelligence (AI) is intelligence demonstrated by machines, \
12        in contrast to the natural intelligence displayed by humans and animals. \
13        Leading AI textbooks define the field as the study of intelligent agents: \
14        any device that perceives its environment and takes actions that maximize \
15        its chance of successfully achieving its goals. Colloquially, the term \
16        artificial intelligence is often used to describe machines that mimic \
17        cognitive functions that humans associate with the human mind, such as \
18        learning and problem solving. As machines become increasingly capable, \
19        tasks considered to require intelligence are often removed from the \
20        definition of AI, a phenomenon known as the AI effect. A quip in \
21        Tesler's Theorem states that AI is whatever hasn't been done yet. \
22        For instance, optical character recognition is frequently excluded \
23        from things considered to be AI, having become a routine technology. \
24        Modern machine capabilities generally classified as AI include \
25        successfully understanding human speech, competing at the highest \
26        level in strategic game systems, autonomously operating cars, \
27        intelligent routing in content delivery networks, and military simulations.";
28
29    // TextRank summarization
30    println!("1. TextRank Summarization");
31    println!("------------------------");
32
33    let textrank = TextRank::new(3); // Extract 3 sentences
34    let summary = textrank.summarize(text)?;
35
36    println!("Original text length: {} characters", text.len());
37    println!("Summary length: {} characters", summary.len());
38    println!("\nSummary:");
39    println!("{summary}\n");
40
41    // Centroid-based summarization
42    println!("2. Centroid-based Summarization");
43    println!("------------------------------");
44
45    let centroid = CentroidSummarizer::new(3);
46    let centroid_summary = centroid.summarize(text)?;
47
48    println!("Summary:");
49    println!("{centroid_summary}\n");
50
51    // Keyword extraction
52    println!("3. Keyword Extraction");
53    println!("--------------------");
54
55    let extractor = KeywordExtractor::new(10).with_ngram_range(1, 2)?;
56
57    let keywords = extractor.extract_keywords(text)?;
58
59    println!("Top 10 keywords/keyphrases:");
60    for (i, (keyword, score)) in keywords.iter().enumerate() {
61        println!("{:2}. {} (score: {:.4})", i + 1, keyword, score);
62    }
63
64    // Keywords with positions
65    println!("\n4. Keyword Positions");
66    println!("-------------------");
67
68    let keywords_with_pos = extractor.extract_keywords_with_positions(text)?;
69
70    for (keyword, _score, positions) in keywords_with_pos.iter().take(5) {
71        println!("'{keyword}' appears at positions: {positions:?}");
72    }
73
74    // Multi-document summarization example
75    println!("\n5. Multi-document Summarization");
76    println!("-------------------------------");
77
78    let docs = [
79        "Machine learning is a subset of artificial intelligence. \
80         It focuses on the development of computer programs that can learn from data.",
81        "Deep learning is a subset of machine learning. \
82         It uses neural networks with multiple layers to progressively extract features.",
83        "Natural language processing enables computers to understand human language. \
84         It combines computational linguistics with machine learning.",
85        "Computer vision is a field of AI that trains computers to interpret visual information. \
86         It uses deep learning models to process images and videos.",
87    ];
88
89    let combinedtext = docs.join(" ");
90    let multi_doc_summary = textrank.summarize(&combinedtext)?;
91
92    println!("Combined documents summary:");
93    println!("{multi_doc_summary}\n");
94
95    // Comparative analysis
96    println!("6. Comparative Analysis");
97    println!("----------------------");
98
99    let techniques = vec![
100        ("TextRank", TextRank::new(2).summarize(text)?),
101        ("Centroid", CentroidSummarizer::new(2).summarize(text)?),
102    ];
103
104    for (name, summary) in techniques {
105        println!("{} (length: {} chars):", name, summary.len());
106        println!("{summary}\n");
107    }
108
109    Ok(())
110}