Skip to content

Commit

Permalink
refactor: Rust APIのAudioQuery系の型名から接尾辞"Model"を削除 (#805)
Browse files Browse the repository at this point in the history
* refactor: Rust APIのAudioQuery系の型名から接尾辞"Model"を削除

* doc修正

* fixup! doc修正
  • Loading branch information
qryxip authored Jul 7, 2024
1 parent 537d9b2 commit 2dd2c87
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 163 deletions.
64 changes: 30 additions & 34 deletions crates/voicevox_core/src/engine/full_context_label.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::str::FromStr;

use crate::{
engine::{self, open_jtalk::FullcontextExtractor, MoraModel},
AccentPhraseModel,
engine::{self, open_jtalk::FullcontextExtractor},
AccentPhrase,
};
use jlabel::{Label, Mora};
use jlabel::Label;
use smallvec::SmallVec;

// FIXME: 入力テキストをここで持って、メッセージに含む
Expand Down Expand Up @@ -33,7 +33,7 @@ type Result<T> = std::result::Result<T, FullContextLabelError>;
pub(crate) fn extract_full_context_label(
open_jtalk: &impl FullcontextExtractor,
text: impl AsRef<str>,
) -> Result<Vec<AccentPhraseModel>> {
) -> Result<Vec<AccentPhrase>> {
let labels = open_jtalk
.extract_fullcontext(text.as_ref())
.map_err(|source| FullContextLabelError {
Expand All @@ -58,7 +58,7 @@ pub(crate) fn extract_full_context_label(

fn generate_accent_phrases(
utterance: &[Label],
) -> std::result::Result<Vec<AccentPhraseModel>, ErrorKind> {
) -> std::result::Result<Vec<AccentPhrase>, ErrorKind> {
let mut accent_phrases = Vec::with_capacity(
utterance
.first()
Expand Down Expand Up @@ -88,7 +88,7 @@ fn generate_accent_phrases(
let pause_mora = if ap_curr.accent_phrase_position_backward == 1
&& bg_curr.breath_group_position_backward != 1
{
Some(MoraModel::new(
Some(crate::Mora::new(
"、".into(),
None,
None,
Expand All @@ -103,7 +103,7 @@ fn generate_accent_phrases(
// workaround for VOICEVOX/voicevox_engine#55
let accent = usize::from(ap_curr.accent_position).min(moras.len());

accent_phrases.push(AccentPhraseModel::new(
accent_phrases.push(AccentPhrase::new(
moras,
accent,
pause_mora,
Expand All @@ -113,7 +113,7 @@ fn generate_accent_phrases(
Ok(accent_phrases)
}

fn generate_moras(accent_phrase: &[Label]) -> std::result::Result<Vec<MoraModel>, ErrorKind> {
fn generate_moras(accent_phrase: &[Label]) -> std::result::Result<Vec<crate::Mora>, ErrorKind> {
let mut moras = Vec::with_capacity(accent_phrase.len());

let split = accent_phrase.chunk_by(|a, b| a.mora == b.mora);
Expand All @@ -136,7 +136,7 @@ fn generate_moras(accent_phrase: &[Label]) -> std::result::Result<Vec<MoraModel>
// position_forwardとposition_backwardが飽和している場合は無視する
[Label {
mora:
Some(Mora {
Some(jlabel::Mora {
position_forward: 49,
position_backward: 49,
..
Expand All @@ -151,10 +151,10 @@ fn generate_moras(accent_phrase: &[Label]) -> std::result::Result<Vec<MoraModel>
Ok(moras)
}

fn generate_mora(consonant: Option<&Label>, vowel: &Label) -> MoraModel {
fn generate_mora(consonant: Option<&Label>, vowel: &Label) -> crate::Mora {
let consonant_phoneme = consonant.and_then(|c| c.phoneme.c.to_owned());
let vowel_phoneme = vowel.phoneme.c.as_deref().unwrap();
MoraModel::new(
crate::Mora::new(
mora_to_text(consonant_phoneme.as_deref(), vowel_phoneme),
consonant_phoneme,
consonant.and(Some(0.0)),
Expand Down Expand Up @@ -190,14 +190,14 @@ mod tests {
engine::{
full_context_label::{extract_full_context_label, generate_accent_phrases},
open_jtalk::FullcontextExtractor,
MoraModel,
Mora,
},
AccentPhraseModel,
AccentPhrase,
};
use jlabel::Label;

fn mora(text: &str, consonant: Option<&str>, vowel: &str) -> MoraModel {
MoraModel::new(
fn mora(text: &str, consonant: Option<&str>, vowel: &str) -> Mora {
Mora::new(
text.into(),
consonant.map(|c| c.into()),
consonant.and(Some(0.0)),
Expand All @@ -218,7 +218,7 @@ mod tests {
"y^e-sil+xx=xx/A:xx+xx+xx/B:xx-xx_xx/C:xx_xx+xx/D:xx+xx_xx/E:1_1!0_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:xx_xx%xx_xx_xx/H:1_1/I:xx-xx@xx+xx&xx-xx|xx+xx/J:xx_xx/K:1+1-1",
],
&[
AccentPhraseModel::new(
AccentPhrase::new(
vec![mora("イェ", Some("y"), "e")],
1,
None,
Expand All @@ -236,7 +236,7 @@ mod tests {
"N^cl-sil+xx=xx/A:xx+xx+xx/B:09-xx_xx/C:xx_xx+xx/D:xx+xx_xx/E:3_3!0_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:xx_xx%xx_xx_xx/H:1_3/I:xx-xx@xx+xx&xx-xx|xx+xx/J:xx_xx/K:1+1-3",
],
&[
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("ン", None, "N"),
mora("ン", None, "N"),
Expand Down Expand Up @@ -271,7 +271,7 @@ mod tests {
"s^U-sil+xx=xx/A:xx+xx+xx/B:10-7_2/C:xx_xx+xx/D:xx+xx_xx/E:5_1!0_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:xx_xx%xx_xx_xx/H:2_8/I:xx-xx@xx+xx&xx-xx|xx+xx/J:xx_xx/K:1+2-8",
],
&[
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("コ", Some("k"), "o"),
mora("レ", Some("r"), "e"),
Expand All @@ -281,7 +281,7 @@ mod tests {
None,
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("テ", Some("t"), "e"),
mora("ス", Some("s"), "U"),
Expand Down Expand Up @@ -324,7 +324,7 @@ mod tests {
"k^u-sil+xx=xx/A:xx+xx+xx/B:05-xx_xx/C:xx_xx+xx/D:xx+xx_xx/E:4_2!1_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:xx_xx%xx_xx_xx/H:1_4/I:xx-xx@xx+xx&xx-xx|xx+xx/J:xx_xx/K:4+4-12",
],
&[
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("イ", None, "i"),
mora("チ", Some("ch"), "i"),
Expand All @@ -333,7 +333,7 @@ mod tests {
Some(mora("、", None, "pau")),
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("セ", Some("s"), "e"),
mora("ン", None, "N"),
Expand All @@ -342,7 +342,7 @@ mod tests {
Some(mora("、", None, "pau")),
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("ヒャ", Some("hy"), "a"),
mora("ク", Some("k"), "u"),
Expand All @@ -353,7 +353,7 @@ mod tests {
Some(mora("、", None, "pau")),
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("イ", None, "i"),
mora("チ", Some("ch"), "i"),
Expand Down Expand Up @@ -386,7 +386,7 @@ mod tests {
"a^a-sil+xx=xx/A:xx+xx+xx/B:09-xx_xx/C:xx_xx+xx/D:xx+xx_xx/E:1_1!0_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:xx_xx%xx_xx_xx/H:2_3/I:xx-xx@xx+xx&xx-xx|xx+xx/J:xx_xx/K:2+3-8",
],
&[
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("クヮ", Some("kw"), "a"),
mora("ル", Some("r"), "u"),
Expand All @@ -398,7 +398,7 @@ mod tests {
Some(mora("、", None, "pau")),
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![
mora("ア", None, "a"),
mora("ア", None, "a"),
Expand All @@ -407,7 +407,7 @@ mod tests {
None,
false,
),
AccentPhraseModel::new(
AccentPhrase::new(
vec![mora("ア", None, "a")],
1,
None,
Expand All @@ -418,21 +418,21 @@ mod tests {
fn label_cases(
#[case] text: &str,
#[case] labels: &[&str],
#[case] accent_phrase: &[AccentPhraseModel],
#[case] accent_phrase: &[AccentPhrase],
) {
}

#[apply(label_cases)]
#[tokio::test]
async fn open_jtalk(text: &str, labels: &[&str], _accent_phrase: &[AccentPhraseModel]) {
async fn open_jtalk(text: &str, labels: &[&str], _accent_phrase: &[AccentPhrase]) {
let open_jtalk = crate::tokio::OpenJtalk::new(OPEN_JTALK_DIC_DIR)
.await
.unwrap();
assert_eq!(&open_jtalk.extract_fullcontext(text).unwrap(), labels);
}

#[apply(label_cases)]
fn parse_labels(_text: &str, labels: &[&str], accent_phrase: &[AccentPhraseModel]) {
fn parse_labels(_text: &str, labels: &[&str], accent_phrase: &[AccentPhrase]) {
let parsed_labels = labels
.iter()
.map(|s| Label::from_str(s).unwrap())
Expand All @@ -446,11 +446,7 @@ mod tests {

#[apply(label_cases)]
#[tokio::test]
async fn extract_fullcontext(
text: &str,
_labels: &[&str],
accent_phrase: &[AccentPhraseModel],
) {
async fn extract_fullcontext(text: &str, _labels: &[&str], accent_phrase: &[AccentPhrase]) {
let open_jtalk = crate::tokio::OpenJtalk::new(OPEN_JTALK_DIC_DIR)
.await
.unwrap();
Expand Down
25 changes: 10 additions & 15 deletions crates/voicevox_core/src/engine/kana_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::engine::model::{AccentPhraseModel, MoraModel};
use crate::engine::model::{AccentPhrase, Mora};
use crate::engine::mora_list::MORA_LIST_MINIMUM;
use once_cell::sync::Lazy;
use std::collections::HashMap;
Expand All @@ -16,7 +16,7 @@ pub(crate) struct KanaParseError(String);

type KanaParseResult<T> = std::result::Result<T, KanaParseError>;

static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, MoraModel>> = Lazy::new(|| {
static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, Mora>> = Lazy::new(|| {
let mut text2mora_with_unvoice = HashMap::new();
for [text, consonant, vowel] in MORA_LIST_MINIMUM {
let consonant = if !consonant.is_empty() {
Expand All @@ -28,7 +28,7 @@ static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, MoraModel>> = Lazy::new(|| {

if ["a", "i", "u", "e", "o"].contains(vowel) {
let upper_vowel = vowel.to_uppercase();
let unvoice_mora = MoraModel::new(
let unvoice_mora = Mora::new(
text.to_string(),
consonant.clone(),
consonant_length,
Expand All @@ -39,7 +39,7 @@ static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, MoraModel>> = Lazy::new(|| {
text2mora_with_unvoice.insert(UNVOICE_SYMBOL.to_string() + text, unvoice_mora);
}

let mora = MoraModel::new(
let mora = Mora::new(
text.to_string(),
consonant,
consonant_length,
Expand All @@ -52,10 +52,10 @@ static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, MoraModel>> = Lazy::new(|| {
text2mora_with_unvoice
});

fn text_to_accent_phrase(phrase: &str) -> KanaParseResult<AccentPhraseModel> {
fn text_to_accent_phrase(phrase: &str) -> KanaParseResult<AccentPhrase> {
let phrase_vec: Vec<char> = phrase.chars().collect();
let mut accent_index: Option<usize> = None;
let mut moras: Vec<MoraModel> = Vec::new();
let mut moras: Vec<Mora> = Vec::new();
let mut stack = String::new();
let mut matched_text: Option<String> = None;
let text2mora = &TEXT2MORA_WITH_UNVOICE;
Expand Down Expand Up @@ -107,15 +107,10 @@ fn text_to_accent_phrase(phrase: &str) -> KanaParseResult<AccentPhraseModel> {
"accent not found in accent phrase: {phrase}"
)));
}
Ok(AccentPhraseModel::new(
moras,
accent_index.unwrap(),
None,
false,
))
Ok(AccentPhrase::new(moras, accent_index.unwrap(), None, false))
}

pub(crate) fn parse_kana(text: &str) -> KanaParseResult<Vec<AccentPhraseModel>> {
pub(crate) fn parse_kana(text: &str) -> KanaParseResult<Vec<AccentPhrase>> {
const TERMINATOR: char = '\0';
let mut parsed_result = Vec::new();
let chars_of_text = text.chars().chain([TERMINATOR]);
Expand All @@ -142,7 +137,7 @@ pub(crate) fn parse_kana(text: &str) -> KanaParseResult<Vec<AccentPhraseModel>>
let accent_phrase = {
let mut accent_phrase = text_to_accent_phrase(&phrase)?;
if letter == PAUSE_DELIMITER {
accent_phrase.set_pause_mora(Some(MoraModel::new(
accent_phrase.set_pause_mora(Some(Mora::new(
PAUSE_DELIMITER.to_string(),
None,
None,
Expand All @@ -163,7 +158,7 @@ pub(crate) fn parse_kana(text: &str) -> KanaParseResult<Vec<AccentPhraseModel>>
Ok(parsed_result)
}

pub(crate) fn create_kana(accent_phrases: &[AccentPhraseModel]) -> String {
pub(crate) fn create_kana(accent_phrases: &[AccentPhrase]) -> String {
let mut text = String::new();
for phrase in accent_phrases {
let moras = phrase.moras();
Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub(crate) use self::full_context_label::{
extract_full_context_label, mora_to_text, FullContextLabelError,
};
pub(crate) use self::kana_parser::{create_kana, parse_kana, KanaParseError};
pub use self::model::{AccentPhraseModel, AudioQueryModel, MoraModel};
pub use self::model::{AccentPhrase, AudioQuery, Mora};
pub(crate) use self::mora_list::mora2text;
pub use self::open_jtalk::FullcontextExtractor;
Loading

0 comments on commit 2dd2c87

Please sign in to comment.