-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding ability to process entire wikipedia page
- Loading branch information
Showing
1 changed file
with
24 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
//build a function that grabs the content from a wikipedia page | ||
//Using wikipedia-rs crate | ||
// Add required imports | ||
extern crate wikipedia; | ||
use wikipedia::Wikipedia; | ||
use wikipedia::http::default::Client; | ||
|
||
use rust_bert::pipelines::summarization::SummarizationModel; | ||
|
||
// Function to get the content from a Wikipedia page | ||
pub fn get_wiki_content(page: &str) -> String { | ||
let wiki = wikipedia::Wikipedia::<wikipedia::http::default::Client>::default(); | ||
let page = wiki.page_from_title((page).to_owned()); | ||
page.get_content().unwrap() | ||
// Change to the correct type parameter for Wikipedia | ||
let wiki = Wikipedia::<Client>::default(); | ||
let page = wiki.page_from_title(page.to_owned()); | ||
|
||
// Check if the page exists before trying to get the content | ||
match page.get_content() { | ||
Ok(content) => content, | ||
Err(_) => String::from("Error: Could not retrieve the content."), | ||
} | ||
} | ||
|
||
//build a function that summarizes the content from a wikipedia page | ||
// Function to summarize the content from a Wikipedia page | ||
pub fn summarize_content(content: &str) -> String { | ||
let model = SummarizationModel::new(Default::default()).unwrap(); | ||
//clean up input to max 500 characters | ||
|
||
// Remove the character limit | ||
let input = content | ||
.chars() | ||
.take(500) | ||
.collect::<String>() | ||
// remove newlines with spaces | ||
.replace('\n', " "); | ||
//convert to a vector of strings | ||
|
||
// Convert to a vector of strings | ||
let input = vec![input]; | ||
//summarize the content | ||
|
||
// Summarize the content | ||
let output = model.summarize(&input); | ||
//return the first element of the vector | ||
output[0].clone() | ||
|
||
// Check if there's a summary and return the first element of the vector | ||
output.get(0).unwrap_or(&String::from("Error: Could not generate a summary.")).clone() | ||
} |