1
- use std:: { fmt:: Display , rc:: Rc } ;
2
-
3
- const DEFAULT_A : & str = "/A:xx+xx+xx" ;
4
- const DEFAULT_B : & str = "/B:xx-xx_xx" ;
5
- const DEFAULT_C : & str = "/C:xx_xx+xx" ;
6
- const DEFAULT_D : & str = "/D:xx+xx_xx" ;
7
- const DEFAULT_E : & str = "/E:xx_xx!xx_xx-xx" ;
8
- const DEFAULT_F : & str = "/F:xx_xx#xx_xx@xx_xx|xx_xx" ;
9
- const DEFAULT_G : & str = "/G:xx_xx%xx_xx_xx" ;
10
- const DEFAULT_H : & str = "/H:xx_xx" ;
11
- const DEFAULT_I : & str = "/I:xx-xx@xx+xx&xx-xx|xx+xx" ;
12
- const DEFAULT_J : & str = "/J:xx_xx" ;
1
+ use std:: rc:: Rc ;
2
+
3
+ use jlabel:: {
4
+ AccentPhraseCurrent , AccentPhrasePrevNext , BreathGroupCurrent , BreathGroupPrevNext , Label ,
5
+ Mora , Phoneme , Utterance , Word ,
6
+ } ;
13
7
14
8
pub struct FeatureBuilderUtterance {
15
- k : String ,
9
+ k : Utterance ,
16
10
}
17
11
18
12
impl FeatureBuilderUtterance {
19
- pub fn new ( k : String ) -> Rc < Self > {
13
+ pub fn new ( k : Utterance ) -> Rc < Self > {
20
14
Rc :: new ( Self { k } )
21
15
}
22
16
}
23
17
24
18
pub trait TFeatureBuilderUtterance {
25
19
fn with_hij (
26
20
& self ,
27
- h : Option < String > ,
28
- i : String ,
29
- j : Option < String > ,
21
+ h : Option < BreathGroupPrevNext > ,
22
+ i : BreathGroupCurrent ,
23
+ j : Option < BreathGroupPrevNext > ,
24
+ ) -> Rc < FeatureBuilderBreathGroup > ;
25
+ fn with_hj (
26
+ & self ,
27
+ h : Option < BreathGroupPrevNext > ,
28
+ j : Option < BreathGroupPrevNext > ,
30
29
) -> Rc < FeatureBuilderBreathGroup > ;
31
- fn with_hj ( & self , h : Option < String > , j : Option < String > ) -> Rc < FeatureBuilderBreathGroup > ;
32
30
}
33
31
34
32
impl TFeatureBuilderUtterance for Rc < FeatureBuilderUtterance > {
35
33
fn with_hij (
36
34
& self ,
37
- h : Option < String > ,
38
- i : String ,
39
- j : Option < String > ,
35
+ h : Option < BreathGroupPrevNext > ,
36
+ i : BreathGroupCurrent ,
37
+ j : Option < BreathGroupPrevNext > ,
40
38
) -> Rc < FeatureBuilderBreathGroup > {
41
39
Rc :: new ( FeatureBuilderBreathGroup {
42
40
utterance : self . clone ( ) ,
@@ -45,7 +43,11 @@ impl TFeatureBuilderUtterance for Rc<FeatureBuilderUtterance> {
45
43
j,
46
44
} )
47
45
}
48
- fn with_hj ( & self , h : Option < String > , j : Option < String > ) -> Rc < FeatureBuilderBreathGroup > {
46
+ fn with_hj (
47
+ & self ,
48
+ h : Option < BreathGroupPrevNext > ,
49
+ j : Option < BreathGroupPrevNext > ,
50
+ ) -> Rc < FeatureBuilderBreathGroup > {
49
51
Rc :: new ( FeatureBuilderBreathGroup {
50
52
utterance : self . clone ( ) ,
51
53
h,
@@ -57,27 +59,31 @@ impl TFeatureBuilderUtterance for Rc<FeatureBuilderUtterance> {
57
59
58
60
pub struct FeatureBuilderBreathGroup {
59
61
utterance : Rc < FeatureBuilderUtterance > ,
60
- h : Option < String > ,
61
- i : Option < String > ,
62
- j : Option < String > ,
62
+ h : Option < BreathGroupPrevNext > ,
63
+ i : Option < BreathGroupCurrent > ,
64
+ j : Option < BreathGroupPrevNext > ,
63
65
}
64
66
65
67
pub trait TFeatureBuilderBreathGroup {
66
68
fn with_efg (
67
69
& self ,
68
- e : Option < String > ,
69
- f : String ,
70
- g : Option < String > ,
70
+ e : Option < AccentPhrasePrevNext > ,
71
+ f : AccentPhraseCurrent ,
72
+ g : Option < AccentPhrasePrevNext > ,
73
+ ) -> Rc < FeatureBuilderAccentPhrase > ;
74
+ fn with_eg (
75
+ & self ,
76
+ e : Option < AccentPhrasePrevNext > ,
77
+ g : Option < AccentPhrasePrevNext > ,
71
78
) -> Rc < FeatureBuilderAccentPhrase > ;
72
- fn with_eg ( & self , e : Option < String > , g : Option < String > ) -> Rc < FeatureBuilderAccentPhrase > ;
73
79
}
74
80
75
81
impl TFeatureBuilderBreathGroup for Rc < FeatureBuilderBreathGroup > {
76
82
fn with_efg (
77
83
& self ,
78
- e : Option < String > ,
79
- f : String ,
80
- g : Option < String > ,
84
+ e : Option < AccentPhrasePrevNext > ,
85
+ f : AccentPhraseCurrent ,
86
+ g : Option < AccentPhrasePrevNext > ,
81
87
) -> Rc < FeatureBuilderAccentPhrase > {
82
88
Rc :: new ( FeatureBuilderAccentPhrase {
83
89
breath_group : self . clone ( ) ,
@@ -86,7 +92,11 @@ impl TFeatureBuilderBreathGroup for Rc<FeatureBuilderBreathGroup> {
86
92
g,
87
93
} )
88
94
}
89
- fn with_eg ( & self , e : Option < String > , g : Option < String > ) -> Rc < FeatureBuilderAccentPhrase > {
95
+ fn with_eg (
96
+ & self ,
97
+ e : Option < AccentPhrasePrevNext > ,
98
+ g : Option < AccentPhrasePrevNext > ,
99
+ ) -> Rc < FeatureBuilderAccentPhrase > {
90
100
Rc :: new ( FeatureBuilderAccentPhrase {
91
101
breath_group : self . clone ( ) ,
92
102
e,
@@ -98,26 +108,26 @@ impl TFeatureBuilderBreathGroup for Rc<FeatureBuilderBreathGroup> {
98
108
99
109
pub struct FeatureBuilderAccentPhrase {
100
110
breath_group : Rc < FeatureBuilderBreathGroup > ,
101
- e : Option < String > ,
102
- f : Option < String > ,
103
- g : Option < String > ,
111
+ e : Option < AccentPhrasePrevNext > ,
112
+ f : Option < AccentPhraseCurrent > ,
113
+ g : Option < AccentPhrasePrevNext > ,
104
114
}
105
115
106
116
pub trait TFeatureBuilderAccentPhrase {
107
- fn with_bcd ( & self , b : Option < String > , c : String , d : Option < String > ) -> Rc < FeatureBuilderWord > ;
108
- fn with_bd ( & self , b : Option < String > , d : Option < String > ) -> Rc < FeatureBuilderWord > ;
117
+ fn with_bcd ( & self , b : Option < Word > , c : Word , d : Option < Word > ) -> Rc < FeatureBuilderWord > ;
118
+ fn with_bd ( & self , b : Option < Word > , d : Option < Word > ) -> Rc < FeatureBuilderWord > ;
109
119
}
110
120
111
121
impl TFeatureBuilderAccentPhrase for Rc < FeatureBuilderAccentPhrase > {
112
- fn with_bcd ( & self , b : Option < String > , c : String , d : Option < String > ) -> Rc < FeatureBuilderWord > {
122
+ fn with_bcd ( & self , b : Option < Word > , c : Word , d : Option < Word > ) -> Rc < FeatureBuilderWord > {
113
123
Rc :: new ( FeatureBuilderWord {
114
124
accent_phrase : self . clone ( ) ,
115
125
b,
116
126
c : Some ( c) ,
117
127
d,
118
128
} )
119
129
}
120
- fn with_bd ( & self , b : Option < String > , d : Option < String > ) -> Rc < FeatureBuilderWord > {
130
+ fn with_bd ( & self , b : Option < Word > , d : Option < Word > ) -> Rc < FeatureBuilderWord > {
121
131
Rc :: new ( FeatureBuilderWord {
122
132
accent_phrase : self . clone ( ) ,
123
133
b,
@@ -129,18 +139,18 @@ impl TFeatureBuilderAccentPhrase for Rc<FeatureBuilderAccentPhrase> {
129
139
130
140
pub struct FeatureBuilderWord {
131
141
accent_phrase : Rc < FeatureBuilderAccentPhrase > ,
132
- b : Option < String > ,
133
- c : Option < String > ,
134
- d : Option < String > ,
142
+ b : Option < Word > ,
143
+ c : Option < Word > ,
144
+ d : Option < Word > ,
135
145
}
136
146
137
147
pub trait TFeatureBuilderWord {
138
- fn with_a ( & self , a : String ) -> FeatureBuilder ;
148
+ fn with_a ( & self , a : Mora ) -> FeatureBuilder ;
139
149
fn without_a ( & self ) -> FeatureBuilder ;
140
150
}
141
151
142
152
impl TFeatureBuilderWord for Rc < FeatureBuilderWord > {
143
- fn with_a ( & self , a : String ) -> FeatureBuilder {
153
+ fn with_a ( & self , a : Mora ) -> FeatureBuilder {
144
154
FeatureBuilder {
145
155
word : self . clone ( ) ,
146
156
a : Some ( a) ,
@@ -158,9 +168,10 @@ impl TFeatureBuilderWord for Rc<FeatureBuilderWord> {
158
168
}
159
169
}
160
170
171
+ #[ derive( Clone ) ]
161
172
pub struct FeatureBuilder {
162
173
word : Rc < FeatureBuilderWord > ,
163
- a : Option < String > ,
174
+ a : Option < Mora > ,
164
175
is_b_valid : bool ,
165
176
is_d_valid : bool ,
166
177
}
@@ -174,41 +185,47 @@ impl FeatureBuilder {
174
185
self . is_d_valid = false ;
175
186
}
176
187
177
- fn mask_property ( prop : Option < & String > , is_valid : bool ) -> Option < & String > {
178
- if is_valid {
179
- prop
180
- } else {
181
- None
188
+ pub fn build ( & self , phoneme : Phoneme ) -> Label {
189
+ Label {
190
+ phoneme,
191
+ mora : self . a . clone ( ) ,
192
+ word_prev : self . is_b_valid . then_some ( ( ) ) . and ( self . word . b . clone ( ) ) ,
193
+ word_curr : self . word . c . clone ( ) ,
194
+ word_next : self . is_d_valid . then_some ( ( ) ) . and ( self . word . d . clone ( ) ) ,
195
+ accent_phrase_prev : self . word . accent_phrase . e . clone ( ) ,
196
+ accent_phrase_curr : self . word . accent_phrase . f . clone ( ) ,
197
+ accent_phrase_next : self . word . accent_phrase . g . clone ( ) ,
198
+ breath_group_prev : self . word . accent_phrase . breath_group . h . clone ( ) ,
199
+ breath_group_curr : self . word . accent_phrase . breath_group . i . clone ( ) ,
200
+ breath_group_next : self . word . accent_phrase . breath_group . j . clone ( ) ,
201
+ utterance : self . word . accent_phrase . breath_group . utterance . k . clone ( ) ,
182
202
}
183
203
}
184
- fn apply_default < ' a > ( prop : Option < & ' a String > , default : & ' static str ) -> & ' a str {
185
- prop. map ( |s| s. as_str ( ) ) . unwrap_or ( default)
204
+
205
+ #[ cfg( test) ]
206
+ pub fn dummy ( ) -> Self {
207
+ let utterance = FeatureBuilderUtterance :: new ( Utterance {
208
+ breath_group_count : 0 ,
209
+ accent_phrase_count : 0 ,
210
+ mora_count : 0 ,
211
+ } ) ;
212
+ let breath_group = utterance. with_hj ( None , None ) ;
213
+ let accent_phrase = breath_group. with_eg ( None , None ) ;
214
+ let word = accent_phrase. with_bd ( None , None ) ;
215
+ word. without_a ( )
186
216
}
187
- }
188
217
189
- impl Display for FeatureBuilder {
190
- /* generate feature string */
191
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
192
- write ! (
193
- f,
194
- "{}{}{}{}{}{}{}{}{}{}{}" ,
195
- Self :: apply_default( self . a. as_ref( ) , DEFAULT_A ) ,
196
- Self :: apply_default(
197
- Self :: mask_property( self . word. b. as_ref( ) , self . is_b_valid) ,
198
- DEFAULT_B
199
- ) ,
200
- Self :: apply_default( self . word. c. as_ref( ) , DEFAULT_C ) ,
201
- Self :: apply_default(
202
- Self :: mask_property( self . word. d. as_ref( ) , self . is_d_valid) ,
203
- DEFAULT_D
204
- ) ,
205
- Self :: apply_default( self . word. accent_phrase. e. as_ref( ) , DEFAULT_E ) ,
206
- Self :: apply_default( self . word. accent_phrase. f. as_ref( ) , DEFAULT_F ) ,
207
- Self :: apply_default( self . word. accent_phrase. g. as_ref( ) , DEFAULT_G ) ,
208
- Self :: apply_default( self . word. accent_phrase. breath_group. h. as_ref( ) , DEFAULT_H ) ,
209
- Self :: apply_default( self . word. accent_phrase. breath_group. i. as_ref( ) , DEFAULT_I ) ,
210
- Self :: apply_default( self . word. accent_phrase. breath_group. j. as_ref( ) , DEFAULT_J ) ,
211
- self . word. accent_phrase. breath_group. utterance. k,
212
- )
218
+ #[ cfg( test) ]
219
+ pub fn to_string_without_phoneme ( & self ) -> String {
220
+ let phoneme = Phoneme {
221
+ p2 : None ,
222
+ p1 : None ,
223
+ c : None ,
224
+ n1 : None ,
225
+ n2 : None ,
226
+ } ;
227
+ let label = self . build ( phoneme) . to_string ( ) ;
228
+ let ( _, feature) = label. split_at ( 14 ) ;
229
+ feature. to_string ( )
213
230
}
214
231
}
0 commit comments