Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
added sentence reduction functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Asudox committed Jan 16, 2024
1 parent fb590df commit 3230df3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/apis/wikipedia_api.rs
Original file line number Diff line number Diff line change
@@ -1,15 1,15 @@
use regex::Regex;
use wikipedia::http::default::Client;
use wikipedia::Wikipedia;
use regex::Regex;

pub struct CustomWikipediaPage {
pub page_title: String,
pub content: String,
pub is_section: bool,
}

impl CustomWikipediaPage{
pub fn reduce_sentences(&mut self, limit: u16){
impl CustomWikipediaPage {
pub fn reduce_sentences(&mut self, limit: u16) {
let re = Regex::new(r"[^.!?]*[.!?] ").unwrap();
let mut sentences = Vec::new();
for cap in re.captures_iter(self.content.as_ref()) {
Expand All @@ -31,7 31,7 @@ pub fn get_wiki_page(
let wiki = Wikipedia::<Client>::default();
let page = wiki.page_from_title(page_title.clone());

if let Some(section_title) = section_title{
if let Some(section_title) = section_title {
// add the section name to the page_title
let mut new_page_title = page_title;
new_page_title.push_str(&section_title);
Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 19,12 @@ fn main() {
env::var("LEMMY_PASSWORD").expect("LEMMY_PASSWORD not configured in .env"),
env::var("LEMMY_INSTANCE").expect("LEMMY_INSTANCE not configured in .env"),
env::var("LEMMY_COMMUNITY").expect("LEMMY_COMMUNITY not configured in .env"),
env::var("SENTENCE_REDUCTION_LIMIT").expect("SENTENCE_REDUCTION_LIMIT not configured in .env"),
env::var("SENTENCE_REDUCTION_LIMIT")
.expect("SENTENCE_REDUCTION_LIMIT not configured in .env"),
);
let sentence_reduction_limit: u16 = sentence_reduction_limit.parse().expect("SENTENCE_REDUCTION_LIMIT is not a number within the range of u16");
let sentence_reduction_limit: u16 = sentence_reduction_limit
.parse()
.expect("SENTENCE_REDUCTION_LIMIT is not a number within the range of u16");

// login to lemmy client
let mut client = LemmyClient::new(username_or_email, password, instance, community);
Expand Down Expand Up @@ -128,10 131,11 @@ fn main() {
.captures(haystack)
.map(|caps| caps.get(1).unwrap().as_str().to_string());

let mut wiki_page = match get_wiki_page(title.to_string(), extracted_section) {
Some(wiki_page) => wiki_page,
None => continue,
};
let mut wiki_page =
match get_wiki_page(title.to_string(), extracted_section) {
Some(wiki_page) => wiki_page,
None => continue,
};
wiki_page.reduce_sentences(sentence_reduction_limit);

let built_comment = comment_builder(wiki_page);
Expand Down

0 comments on commit 3230df3

Please sign in to comment.