Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fe24f7

Browse files
authored
use jlabel on jpreprocess-jpcommon (#219)
1 parent 93063ce commit 5fe24f7

File tree

16 files changed

+241
-235
lines changed

16 files changed

+241
-235
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ jpreprocess-jpcommon = { path = "crates/jpreprocess-jpcommon", version = "0.6.3"
2525
jpreprocess-naist-jdic = { path = "crates/jpreprocess-naist-jdic", version = "0.6.3" }
2626
jpreprocess-njd = { path = "crates/jpreprocess-njd", version = "0.6.3" }
2727
jpreprocess-window = { path = "crates/jpreprocess-window", version = "0.6.3" }
28+
29+
jlabel = "0.1.2"

crates/jpreprocess-jpcommon/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ rust-version.workspace = true
1313
documentation = "https://docs.rs/jpreprocess-jpcommon"
1414

1515
[dependencies]
16+
jlabel.workspace = true
1617
jpreprocess-core.workspace = true
1718
jpreprocess-njd.workspace = true
Lines changed: 95 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
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+
};
137

148
pub struct FeatureBuilderUtterance {
15-
k: String,
9+
k: Utterance,
1610
}
1711

1812
impl FeatureBuilderUtterance {
19-
pub fn new(k: String) -> Rc<Self> {
13+
pub fn new(k: Utterance) -> Rc<Self> {
2014
Rc::new(Self { k })
2115
}
2216
}
2317

2418
pub trait TFeatureBuilderUtterance {
2519
fn with_hij(
2620
&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>,
3029
) -> Rc<FeatureBuilderBreathGroup>;
31-
fn with_hj(&self, h: Option<String>, j: Option<String>) -> Rc<FeatureBuilderBreathGroup>;
3230
}
3331

3432
impl TFeatureBuilderUtterance for Rc<FeatureBuilderUtterance> {
3533
fn with_hij(
3634
&self,
37-
h: Option<String>,
38-
i: String,
39-
j: Option<String>,
35+
h: Option<BreathGroupPrevNext>,
36+
i: BreathGroupCurrent,
37+
j: Option<BreathGroupPrevNext>,
4038
) -> Rc<FeatureBuilderBreathGroup> {
4139
Rc::new(FeatureBuilderBreathGroup {
4240
utterance: self.clone(),
@@ -45,7 +43,11 @@ impl TFeatureBuilderUtterance for Rc<FeatureBuilderUtterance> {
4543
j,
4644
})
4745
}
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> {
4951
Rc::new(FeatureBuilderBreathGroup {
5052
utterance: self.clone(),
5153
h,
@@ -57,27 +59,31 @@ impl TFeatureBuilderUtterance for Rc<FeatureBuilderUtterance> {
5759

5860
pub struct FeatureBuilderBreathGroup {
5961
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>,
6365
}
6466

6567
pub trait TFeatureBuilderBreathGroup {
6668
fn with_efg(
6769
&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>,
7178
) -> Rc<FeatureBuilderAccentPhrase>;
72-
fn with_eg(&self, e: Option<String>, g: Option<String>) -> Rc<FeatureBuilderAccentPhrase>;
7379
}
7480

7581
impl TFeatureBuilderBreathGroup for Rc<FeatureBuilderBreathGroup> {
7682
fn with_efg(
7783
&self,
78-
e: Option<String>,
79-
f: String,
80-
g: Option<String>,
84+
e: Option<AccentPhrasePrevNext>,
85+
f: AccentPhraseCurrent,
86+
g: Option<AccentPhrasePrevNext>,
8187
) -> Rc<FeatureBuilderAccentPhrase> {
8288
Rc::new(FeatureBuilderAccentPhrase {
8389
breath_group: self.clone(),
@@ -86,7 +92,11 @@ impl TFeatureBuilderBreathGroup for Rc<FeatureBuilderBreathGroup> {
8692
g,
8793
})
8894
}
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> {
90100
Rc::new(FeatureBuilderAccentPhrase {
91101
breath_group: self.clone(),
92102
e,
@@ -98,26 +108,26 @@ impl TFeatureBuilderBreathGroup for Rc<FeatureBuilderBreathGroup> {
98108

99109
pub struct FeatureBuilderAccentPhrase {
100110
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>,
104114
}
105115

106116
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>;
109119
}
110120

111121
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> {
113123
Rc::new(FeatureBuilderWord {
114124
accent_phrase: self.clone(),
115125
b,
116126
c: Some(c),
117127
d,
118128
})
119129
}
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> {
121131
Rc::new(FeatureBuilderWord {
122132
accent_phrase: self.clone(),
123133
b,
@@ -129,18 +139,18 @@ impl TFeatureBuilderAccentPhrase for Rc<FeatureBuilderAccentPhrase> {
129139

130140
pub struct FeatureBuilderWord {
131141
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>,
135145
}
136146

137147
pub trait TFeatureBuilderWord {
138-
fn with_a(&self, a: String) -> FeatureBuilder;
148+
fn with_a(&self, a: Mora) -> FeatureBuilder;
139149
fn without_a(&self) -> FeatureBuilder;
140150
}
141151

142152
impl TFeatureBuilderWord for Rc<FeatureBuilderWord> {
143-
fn with_a(&self, a: String) -> FeatureBuilder {
153+
fn with_a(&self, a: Mora) -> FeatureBuilder {
144154
FeatureBuilder {
145155
word: self.clone(),
146156
a: Some(a),
@@ -158,9 +168,10 @@ impl TFeatureBuilderWord for Rc<FeatureBuilderWord> {
158168
}
159169
}
160170

171+
#[derive(Clone)]
161172
pub struct FeatureBuilder {
162173
word: Rc<FeatureBuilderWord>,
163-
a: Option<String>,
174+
a: Option<Mora>,
164175
is_b_valid: bool,
165176
is_d_valid: bool,
166177
}
@@ -174,41 +185,47 @@ impl FeatureBuilder {
174185
self.is_d_valid = false;
175186
}
176187

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(),
182202
}
183203
}
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()
186216
}
187-
}
188217

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()
213230
}
214231
}

crates/jpreprocess-jpcommon/src/feature/limit.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ pub enum Limit {
99
LL,
1010
}
1111
impl Limit {
12-
pub fn ulimit(self, value: usize) -> usize {
12+
pub fn ulimit(self, value: usize) -> u8 {
1313
match self {
14-
Self::S => Self::clamp(value, 1, 19),
15-
Self::M => Self::clamp(value, 1, 49),
16-
Self::L => Self::clamp(value, 1, 99),
17-
Self::LL => Self::clamp(value, 1, 199),
14+
Self::S => Self::clamp(value, 1, 19) as u8,
15+
Self::M => Self::clamp(value, 1, 49) as u8,
16+
Self::L => Self::clamp(value, 1, 99) as u8,
17+
Self::LL => Self::clamp(value, 1, 199) as u8,
1818
}
1919
}
20-
pub fn ilimit(self, value: isize) -> isize {
20+
pub fn ilimit(self, value: isize) -> i8 {
2121
match self {
22-
Self::S => Self::clamp(value, -19, 19),
23-
Self::M => Self::clamp(value, -49, 49),
24-
Self::L => Self::clamp(value, -99, 99),
25-
Self::LL => Self::clamp(value, -199, 199),
22+
Self::S => Self::clamp(value, -19, 19) as i8,
23+
Self::M => Self::clamp(value, -49, 49) as i8,
24+
Self::L => Self::clamp(value, -99, 99) as i8,
25+
Self::LL => Self::clamp(value, -199, 199) as i8,
2626
}
2727
}
2828
fn clamp<T>(value: T, lower_bound: T, upper_bound: T) -> T

0 commit comments

Comments
 (0)