1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{Phoneme, word_phoneme::WordPhonemeProsody};
/// 音素ごとのピッチアクセント (高低) を表す enum
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PitchAccent {
Low,
High,
}
// プロソディ情報つきの音素を表す enum
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProsodicPhoneme {
Phoneme {
phoneme: Phoneme,
pitch: Option<PitchAccent>,
},
/// アクセント句境界 (`#`)
AccentPhraseBoundary,
/// 通常のポーズ・読点 (`_`)
Pause,
/// 疑問の終結・ポーズ (`?`)
Interrogative,
/// 感嘆の終結・ポーズ (`!`)
Exclamatory,
}
impl ProsodicPhoneme {
pub(crate) fn pau() -> Self {
Self::Phoneme {
phoneme: Phoneme::Pau,
pitch: None,
}
}
pub(crate) fn unk() -> Self {
Self::Phoneme {
phoneme: Phoneme::Unk,
pitch: None,
}
}
pub(crate) fn sp() -> Self {
Self::Phoneme {
phoneme: Phoneme::Sp,
pitch: None,
}
}
}
/// `g2p_prosody_with_options` のような関数で出力形式を指定する enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProsodyFormat {
/// tdmelodic 風 (`a [ o ] i #`) の記法
#[default]
Default,
/// `L_a`, `H_o` のようなプレフィックス表現
Prefix,
/// `a:0`, `o:1` のような数値サフィックス表現
Numeric,
}
impl WordPhonemeProsody {
/// 単語のプロソディ情報を、指定されたフォーマットで文字列配列に変換する。
///
/// # Arguments
/// - `format` - 出力するプロソディ表現のフォーマット
/// - `prev_pitch` - 直前の音素のピッチ状態。`Default` における `[` `]` 挿入の判定に使用する。
/// イテレーション間で状態を保持するため可変参照を渡す。
pub fn to_formatted_strings(
&self,
format: ProsodyFormat,
prev_pitch: &mut Option<PitchAccent>,
) -> Vec<String> {
let mut result = Vec::new();
if self.is_unknown {
result.push("{".to_string());
}
for p in &self.phonemes {
match p {
ProsodicPhoneme::Phoneme { phoneme, pitch } => {
if format == ProsodyFormat::Default
&& let Some(curr) = pitch
{
match (*prev_pitch, *curr) {
(Some(PitchAccent::Low), PitchAccent::High) => {
result.push("[".to_string());
}
(Some(PitchAccent::High), PitchAccent::Low) => {
result.push("]".to_string());
}
_ => {}
}
*prev_pitch = Some(*curr);
}
let phoneme_str = phoneme.as_str();
match format {
ProsodyFormat::Prefix => {
let prefix = match pitch {
Some(PitchAccent::High) => "H_",
Some(PitchAccent::Low) => "L_",
None => "",
};
result.push(format!("{}{}", prefix, phoneme_str));
}
ProsodyFormat::Numeric => {
let suffix = match pitch {
Some(PitchAccent::High) => ":1",
Some(PitchAccent::Low) => ":0",
None => "",
};
result.push(format!("{}{}", phoneme_str, suffix));
}
ProsodyFormat::Default => {
result.push(phoneme_str.to_string());
}
}
}
ProsodicPhoneme::AccentPhraseBoundary => {
result.push("#".to_string());
// アクセント句を跨いだらピッチの追跡をリセット
if format == ProsodyFormat::Default {
*prev_pitch = None;
}
}
ProsodicPhoneme::Pause => {
result.push("_".to_string());
if format == ProsodyFormat::Default {
*prev_pitch = None;
}
}
ProsodicPhoneme::Interrogative => {
result.push("?".to_string());
if format == ProsodyFormat::Default {
*prev_pitch = None;
}
}
ProsodicPhoneme::Exclamatory => {
result.push("!".to_string());
if format == ProsodyFormat::Default {
*prev_pitch = None;
}
}
}
}
if self.is_unknown {
result.push("}".to_string());
}
result
}
}