KeywordExtractor

Struct KeywordExtractor 

Source
pub struct KeywordExtractor { /* private fields */ }
Expand description

Keyword extraction using TF-IDF

Implementations§

Source§

impl KeywordExtractor

Source

pub fn new(_numkeywords: usize) -> Self

Create a new keyword extractor

Examples found in repository?
examples/summarization_demo.rs (line 55)
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}
Source

pub fn with_ngram_range(self, min_n: usize, maxn: usize) -> Result<Self>

Configure n-gram range

Examples found in repository?
examples/summarization_demo.rs (line 55)
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}
Source

pub fn extract_keywords(&self, text: &str) -> Result<Vec<(String, f64)>>

Extract keywords from text

Examples found in repository?
examples/summarization_demo.rs (line 57)
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}
Source

pub fn extract_keywords_with_positions( &self, text: &str, ) -> Result<Vec<(String, f64, Vec<usize>)>>

Extract keywords with position information

Examples found in repository?
examples/summarization_demo.rs (line 68)
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}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V