This commit is contained in:
Masato Kikuchi
2025-03-27 14:42:43 +09:00
parent 472d1c600f
commit a67df43fc7
3 changed files with 31 additions and 26 deletions

View File

@@ -76,7 +76,12 @@ static MORA_PATTERN: Lazy<Vec<String>> = Lazy::new(|| {
});
static LONG_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\w)(ー*)").unwrap());
fn phone_tone_to_kana(phones: Vec<String>, tones: Vec<i32>) -> Vec<(String, i32)> {
fn phone_tone_to_kana(
phones: Vec<String>,
tones: Vec<i32>,
) -> Vec<(String, Option<String>, String, i32)> {
let phones = &phones[1..];
let tones = &tones[1..];
let mut results = Vec::new();
let mut current_mora = String::new();
for ((phone, next_phone), (&tone, &next_tone)) in phones
@@ -85,7 +90,7 @@ fn phone_tone_to_kana(phones: Vec<String>, tones: Vec<i32>) -> Vec<(String, i32)
.zip(tones.iter().zip(tones.iter().skip(1)))
{
if PUNCTUATIONS.contains(&phone.clone().as_str()) {
results.push((phone.to_string(), tone));
results.push((phone.to_string(), None, "pau".to_string(), tone));
continue;
}
if CONSONANTS.contains(&phone.clone()) {
@@ -94,13 +99,8 @@ fn phone_tone_to_kana(phones: Vec<String>, tones: Vec<i32>) -> Vec<(String, i32)
current_mora = phone.to_string()
} else {
current_mora += phone;
results.push((
MORA_PHONEMES_TO_MORA_KATA
.get(&current_mora)
.unwrap()
.to_string(),
tone,
));
let (kana, consonant, vowel) = MORA_PHONEMES_TO_MORA_KATA.get(&current_mora).unwrap();
results.push((kana.to_string(), consonant.clone(), vowel.to_string(), tone));
current_mora = String::new();
}
}
@@ -196,7 +196,7 @@ impl JTalkProcess {
Ok((phones, tones, new_word2ph))
}
pub fn g2kana_tone(&self) -> Result<Vec<(String, i32)>> {
pub fn g2kana_tone(&self) -> Result<Vec<(String, Option<String>, String, i32)>> {
let (phones, tones, _) = self.g2p()?;
Ok(phone_tone_to_kana(phones, tones))
}

View File

@@ -25,20 +25,25 @@ static MORA_LIST_ADDITIONAL: Lazy<Vec<Mora>> = Lazy::new(|| {
data.additional
});
pub static MORA_PHONEMES_TO_MORA_KATA: Lazy<HashMap<String, String>> = Lazy::new(|| {
let mut map = HashMap::new();
for mora in MORA_LIST_MINIMUM.iter() {
map.insert(
format!(
"{}{}",
mora.consonant.clone().unwrap_or("".to_string()),
mora.vowel
),
mora.mora.clone(),
);
}
map
});
pub static MORA_PHONEMES_TO_MORA_KATA: Lazy<HashMap<String, (String, Option<String>, String)>> =
Lazy::new(|| {
let mut map = HashMap::new();
for mora in MORA_LIST_MINIMUM.iter() {
map.insert(
format!(
"{}{}",
mora.consonant.clone().unwrap_or("".to_string()),
mora.vowel
),
(
mora.mora.clone(),
mora.consonant.clone(),
mora.vowel.clone(),
),
);
}
map
});
pub static MORA_KATA_TO_MORA_PHONEMES: Lazy<HashMap<String, (Option<String>, String)>> =
Lazy::new(|| {

View File

@@ -12,11 +12,11 @@ struct RequestCreateAudioQuery {
text: String,
}
async fn create_audio_query(Query(request): Query<RequestCreateAudioQuery>) -> AppResult<()> {
async fn create_audio_query(Query(request): Query<RequestCreateAudioQuery>) -> AppResult<String> {
let (normalized_text, process) = preprocess_parse_text(&request.text, &JTalk::new()?)?;
let kana_tone_list = process.g2kana_tone()?;
println!("{:?}", kana_tone_list);
Ok(())
Ok(normalized_text)
}
#[tokio::main]