Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl serde for Voice #30

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rust-version = "1.75.0"
[features]
default = ["htsvoice"]
binary = ["htsvoice", "dep:hound"]
htsvoice = ["dep:nom","dep:serde"]
htsvoice = ["dep:nom"]

[[example]]
name = "is-bonsai"
Expand All @@ -22,8 +22,10 @@ byteorder = "1.5.0"
thiserror = "1.0.56"
approx = "0.5.1"

serde = { version = "1.0", features = ["derive"] }

nom = { version = "7.1.3", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
hound = { version = "3.5.1", optional = true }
jlabel = "0.1.3"
jlabel-question = { version = "0.1.3", features = ["regex"]}

jlabel = { version = "0.1.4", features = ["serde"] }
jlabel-question = { version = "0.1.4", features = ["regex", "serde"] }
4 changes: 3 additions & 1 deletion src/model/mean_vari.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::{
ops::{Add, Mul},
};

#[derive(Debug, Clone, Copy, PartialEq)]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct MeanVari(pub f64, pub f64);

impl MeanVari {
Expand Down
10 changes: 6 additions & 4 deletions src/model/voice/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use self::{model::Model, window::Windows};

pub mod model;
pub mod question;
pub mod tree;
pub mod window;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Voice {
pub metadata: GlobalModelMetadata,
pub duration_model: Model,
Expand All @@ -25,7 +27,7 @@ impl Display for Voice {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GlobalModelMetadata {
pub hts_voice_version: String,
pub sampling_frequency: usize,
Expand Down Expand Up @@ -55,7 +57,7 @@ impl Display for GlobalModelMetadata {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StreamModels {
pub metadata: StreamModelMetadata,

Expand Down Expand Up @@ -101,7 +103,7 @@ impl StreamModels {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StreamModelMetadata {
pub vector_length: usize,
pub num_windows: usize,
Expand Down
5 changes: 3 additions & 2 deletions src/model/voice/model.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::fmt::Display;

use jlabel::Label;
use serde::{Deserialize, Serialize};

use crate::model::MeanVari;

use super::tree::Tree;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Model {
trees: Vec<Tree>,
pdf: Vec<Vec<ModelParameter>>,
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Model {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelParameter {
pub parameters: Vec<MeanVari>,
pub msd: Option<f64>,
Expand Down
56 changes: 46 additions & 10 deletions src/model/voice/question.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use jlabel_question::{regex::RegexQuestion, AllQuestion, QuestionMatcher};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Question {
AllQustion(AllQuestion),
Regex(RegexQuestion),
Regex(RegexWrap),
}

impl Question {
pub fn parse(patterns: &[&str]) -> Result<Self, jlabel_question::ParseError> {
match AllQuestion::parse(patterns) {
Ok(question) => Ok(Self::AllQustion(question)),
Err(_) => Ok(Self::Regex(RegexQuestion::parse(patterns)?)),
Err(_) => Ok(Self::Regex(RegexWrap::parse(patterns)?)),
}
}

Expand All @@ -22,13 +23,48 @@
}
}

impl PartialEq for Question {
#[derive(Debug, Clone)]
pub struct RegexWrap {
orig: Vec<String>,
q: RegexQuestion,
}

impl RegexWrap {
pub fn parse(patterns: &[&str]) -> Result<Self, jlabel_question::ParseError> {
Ok(Self {
orig: patterns.iter().map(|s| s.to_string()).collect(),
q: RegexQuestion::parse(patterns)?,
})
}
pub fn test(&self, label: &jlabel::Label) -> bool {
self.q.test(label)
}
}

impl PartialEq for RegexWrap {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::AllQustion(a), Self::AllQustion(b)) => a == b,
// TODO: :exploding_head:
(Self::Regex(_), Self::Regex(_)) => true,
_ => false,
}
self.orig == other.orig

Check warning on line 46 in src/model/voice/question.rs

View check run for this annotation

Codecov / codecov/patch

src/model/voice/question.rs#L46

Added line #L46 was not covered by tests
}
}

impl Serialize for RegexWrap {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.orig.serialize(serializer)

Check warning on line 55 in src/model/voice/question.rs

View check run for this annotation

Codecov / codecov/patch

src/model/voice/question.rs#L55

Added line #L55 was not covered by tests
}
}

impl<'de> Deserialize<'de> for RegexWrap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let orig = Vec::deserialize(deserializer)?;
Ok(Self {
q: RegexQuestion::parse(&orig).map_err(serde::de::Error::custom)?,
orig,

Check warning on line 67 in src/model/voice/question.rs

View check run for this annotation

Codecov / codecov/patch

src/model/voice/question.rs#L64-L67

Added lines #L64 - L67 were not covered by tests
})
}
}
5 changes: 3 additions & 2 deletions src/model/voice/tree.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use jlabel::Label;
use serde::{Deserialize, Serialize};

use super::question::Question;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tree {
pub state: usize,
pub nodes: Vec<TreeNode>,
Expand All @@ -26,7 +27,7 @@ impl Tree {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TreeNode {
Node {
question: Question,
Expand Down
6 changes: 4 additions & 2 deletions src/model/voice/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Debug, Clone, PartialEq)]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Windows {
windows: Vec<Window>,
}
Expand All @@ -19,7 +21,7 @@ impl Windows {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Window {
coefficients: Vec<f64>,
}
Expand Down
Loading