Skip to content

Commit

Permalink
Hook up Text Extraction Preferences and Change version to v1.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
divark committed May 16, 2024
1 parent 9b938e4 commit 5e17ea2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
- This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- This project uses [ISO Standard](https://www.iso.org/iso-8601-date-and-time-format.html) date formatting

## Unreleased
## [1.0.0] - 2024-05-16
### Added
- Wayland support.
- Text Extraction Preferences.

### Changed
- Updated FLTK and various developer dependencies.
Expand Down
49 changes: 25 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "narrative-director-rs"
version = "0.1.2"
version = "1.0.0"
authors = ["Tyler Schmidt <tmschmid@protonmail.com>"]
edition = "2021"
license = "gpl-3.0"
Expand Down
78 changes: 68 additions & 10 deletions src/text/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ impl ParagraphViewer {
}
}

pub fn load_paragraphs(&mut self, text_file_path: PathBuf) {
pub fn load_paragraphs(&mut self, text_file_path: PathBuf, delimiters: &str, amount: usize) {
let mut text_file = File::open(text_file_path).expect("Could not load file.");
let mut whole_text_content = String::new();
text_file
.read_to_string(&mut whole_text_content)
.expect("Could not read text file.");

let delimiter_tokens = delimiters.chars().collect::<Vec<char>>();
let split_paragraphs: Vec<&str> = whole_text_content
.split_inclusive(|character| character == '.' || character == '?' || character == '!')
.split_inclusive(&*delimiter_tokens)
.collect();

self.paragraphs = split_paragraphs
.chunks(4)
.chunks(amount)
.map(|sentences| sentences.concat())
.collect();

Expand All @@ -108,6 +109,32 @@ impl ParagraphViewer {
self.progress_counter.update();
}

/// Changes currently loaded text to be split by the provided
/// delimiters.
pub fn reload_text_with(&mut self, delimiters: &str, amount: usize) {
let existing_text = self.paragraphs.join("");

let delimiter_tokens = delimiters.chars().collect::<Vec<char>>();
let new_splitted_text: Vec<&str> =
existing_text.split_inclusive(&*delimiter_tokens).collect();

let new_chunked_text: Vec<String> = new_splitted_text
.chunks(amount)
.map(|line| line.concat())
.collect();

if new_chunked_text == self.paragraphs {
return;
}

self.paragraphs = new_chunked_text;

self.progress_counter.set_current(0);
self.progress_counter.set_total(self.paragraphs.len());
self.progress_counter.update();
self.show_paragraph_at(0);
}

pub fn show_next_paragraph(&mut self) {
self.paragraph_num += 1;

Expand Down Expand Up @@ -183,6 +210,9 @@ mod tests {
use std::io::Write;
use tempfile::NamedTempFile;

const DELIMITERS: &str = ".?!";
const GATHERING_AMOUNT: usize = 4;

const FIRST_PARAGRAPH: &str = "This is the first paragraph. It will eventually contain four sentences. I'm serious! Okay, here is the last sentence.";
const SECOND_PARAGRAPH: &str = "This is the second paragraph. It will also contain four sentences. This paragraph is similar to the first one. It really is?";

Expand Down Expand Up @@ -246,7 +276,11 @@ mod tests {
#[test]
fn goto_exceeds_paragraphs() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_many_paragraphs().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs());

let goto_paragraph_num = 3;
Expand All @@ -260,7 +294,11 @@ mod tests {
#[test]
fn goto_paragraph_exists() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_many_paragraphs().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs());

let goto_paragraph_num = 1;
Expand Down Expand Up @@ -291,7 +329,11 @@ mod tests {
#[test]
fn next_exceeds_paragraphs() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_one_paragraph().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(1, paragraph_viewer.num_paragraphs());

paragraph_viewer.show_paragraph_at(0);
Expand All @@ -308,7 +350,11 @@ mod tests {
#[test]
fn next_paragraph_exists() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_many_paragraphs().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs());

paragraph_viewer.show_paragraph_at(0);
Expand Down Expand Up @@ -341,7 +387,11 @@ mod tests {
#[test]
fn previous_negative_paragraphs() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_one_paragraph().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(1, paragraph_viewer.num_paragraphs());

paragraph_viewer.show_paragraph_at(0);
Expand All @@ -358,7 +408,11 @@ mod tests {
#[test]
fn previous_paragraph_exists() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_many_paragraphs().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs());

paragraph_viewer.show_paragraph_at(MANY_PARAGRAPHS_LEN - 1);
Expand All @@ -379,7 +433,11 @@ mod tests {
#[test]
fn shows_paragraph() {
let mut paragraph_viewer = get_paragraph_viewer();
paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf());
paragraph_viewer.load_paragraphs(
get_file_one_paragraph().path().to_path_buf(),
DELIMITERS,
GATHERING_AMOUNT,
);
assert_eq!(1, paragraph_viewer.num_paragraphs());

paragraph_viewer.show_paragraph_at(0);
Expand Down
18 changes: 11 additions & 7 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ impl MainApplication {
let session = Session::load(file_location.clone())
.unwrap_or_else(|| Session::new(file_location.clone()));

self.paragraph_viewer.load_paragraphs(file_location);
self.paragraph_viewer.load_paragraphs(
file_location,
&session.gathering_delimiters(),
session.gathering_amount(),
);
self.paragraph_viewer
.show_paragraph_at(session.paragraph_num());

Expand Down Expand Up @@ -228,13 +232,13 @@ impl MainApplication {
// TODO: Split session into AudioPreferences, TextPreferences, and Session.
// That way, users can use the Preferences dialog without needing an existing
// session open.
if self.session.is_some() {
self.preferences_dialog.show(
self.session.as_mut().expect(
"Session is needed to fetch current audio information.",
),
);
if let Some(session) = self.session.as_mut() {
self.preferences_dialog.show(session);

self.paragraph_viewer.reload_text_with(
&session.gathering_delimiters(),
session.gathering_amount(),
);
self.load_audio_file();
}
}
Expand Down

0 comments on commit 5e17ea2

Please sign in to comment.