Skip to content

Commit

Permalink
adding ability to process entire wikipedia page
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgift committed Mar 29, 2023
1 parent bcf423a commit 8c56ac5
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions hfdemo/src/lib.rs
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()
}

0 comments on commit 8c56ac5

Please sign in to comment.