jpreprocess_jpcommon/label/
accent_phrase.rs1use jpreprocess_njd::NJDNode;
2
3use crate::limit::Limit;
4
5use super::*;
6
7#[derive(Clone, Debug)]
8pub struct AccentPhrase {
9 accent: usize,
10 is_interrogative: bool,
11 pub words: Vec<Word>,
12}
13
14impl AccentPhrase {
15 pub fn new(start_node: &NJDNode) -> Self {
16 Self {
17 accent: start_node.get_pron().accent(),
18 is_interrogative: false,
19 words: vec![start_node.into()],
20 }
21 }
22 pub(super) fn push_node(&mut self, node: &NJDNode) {
23 if !matches!(node.get_chain_flag(), Some(true)) {
24 panic!("push_node of AccentPhrase should not be called unless chain flag is true");
25 }
26 self.words.push(node.into());
27 }
28 pub(super) fn set_interrogative(&mut self) {
29 self.is_interrogative = true;
30 }
31
32 pub fn to_e(&self, is_prev_pause: Option<bool>) -> jlabel::AccentPhrasePrevNext {
33 let mora_count = self.count_mora();
34 jlabel::AccentPhrasePrevNext {
35 mora_count: Limit::M.ulimit(mora_count),
36 accent_position: Limit::M.ulimit(if self.accent == 0 {
37 mora_count
38 } else {
39 self.accent
40 }),
41 is_interrogative: self.is_interrogative,
42 is_pause_insertion: is_prev_pause,
43 }
44 }
45 pub fn to_f(
46 &self,
47 accent_phrase_count_in_breath_group: usize,
48 accent_phrase_index_in_breath_group: usize,
49 mora_count_in_breath_group: usize,
50 mora_index_in_breath_group: usize,
51 ) -> jlabel::AccentPhraseCurrent {
52 let mora_count = self.count_mora();
53 jlabel::AccentPhraseCurrent {
54 mora_count: Limit::M.ulimit(mora_count),
55 accent_position: Limit::M.ulimit(if self.accent == 0 {
56 mora_count
57 } else {
58 self.accent
59 }),
60 is_interrogative: self.is_interrogative,
61 accent_phrase_position_forward: Limit::M
62 .ulimit(accent_phrase_index_in_breath_group + 1),
63 accent_phrase_position_backward: Limit::M
64 .ulimit(accent_phrase_count_in_breath_group - accent_phrase_index_in_breath_group),
65 mora_position_forward: Limit::L.ulimit(mora_index_in_breath_group + 1),
66 mora_position_backward: Limit::L
67 .ulimit(mora_count_in_breath_group - mora_index_in_breath_group),
68 }
69 }
70 pub fn to_g(&self, is_next_pause: Option<bool>) -> jlabel::AccentPhrasePrevNext {
71 let mora_count = self.count_mora();
72 jlabel::AccentPhrasePrevNext {
73 mora_count: Limit::M.ulimit(mora_count),
74 accent_position: Limit::M.ulimit(if self.accent == 0 {
75 mora_count
76 } else {
77 self.accent
78 }),
79 is_interrogative: self.is_interrogative,
80 is_pause_insertion: is_next_pause,
81 }
82 }
83
84 pub fn generate_mora_a(&self) -> Vec<jlabel::Mora> {
85 let mora_count = self.count_mora();
86 let accent = if self.accent == 0 {
87 mora_count
88 } else {
89 self.accent
90 };
91 (0..mora_count)
92 .map(|mora_index| jlabel::Mora {
93 relative_accent_position: Limit::M
94 .ilimit(mora_index as isize - accent as isize + 1),
95 position_forward: Limit::M.ulimit(mora_index + 1),
96 position_backward: Limit::M.ulimit(mora_count - mora_index),
97 })
98 .collect()
99 }
100
101 pub fn count_mora(&self) -> usize {
102 self.words.iter().map(|word| word.count_mora()).sum()
103 }
104}