pub struct KeywordExtractor { /* private fields */ }Expand description
Keyword extraction using TF-IDF
Implementations§
Source§impl KeywordExtractor
impl KeywordExtractor
Sourcepub fn new(_numkeywords: usize) -> Self
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}Sourcepub fn with_ngram_range(self, min_n: usize, maxn: usize) -> Result<Self>
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}Sourcepub fn extract_keywords(&self, text: &str) -> Result<Vec<(String, f64)>>
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}Sourcepub fn extract_keywords_with_positions(
&self,
text: &str,
) -> Result<Vec<(String, f64, Vec<usize>)>>
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§
impl Freeze for KeywordExtractor
impl RefUnwindSafe for KeywordExtractor
impl Send for KeywordExtractor
impl Sync for KeywordExtractor
impl Unpin for KeywordExtractor
impl UnwindSafe for KeywordExtractor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.