1mod contrib;
2mod node;
3mod open_jtalk;
4
5use jpreprocess_core::{token::Token, word_entry::WordEntry, JPreprocessResult};
6use jpreprocess_window::{IterQuintMut, IterQuintMutTrait};
7
8pub use contrib::*;
9pub use node::*;
10pub use open_jtalk::*;
11
12#[derive(Clone, Debug, PartialEq)]
13pub struct NJD {
14 pub nodes: Vec<NJDNode>,
15}
16
17impl NJD {
18 pub fn remove_silent_node(&mut self) {
19 self.nodes.retain(|node| !node.get_pron().is_empty())
20 }
21
22 pub fn from_tokens<'a, T: Token>(
23 tokens: impl 'a + IntoIterator<Item = T>,
24 ) -> JPreprocessResult<Self> {
25 let mut nodes = Vec::new();
26 for mut token in tokens {
27 let (string, entry) = token.fetch()?;
28 nodes.extend(NJDNode::load(string, &entry));
29 }
30
31 Ok(Self { nodes })
32 }
33 pub fn from_strings(njd_features: Vec<String>) -> Self {
34 Self {
35 nodes: njd_features
36 .iter()
37 .flat_map(|feature| NJDNode::load_csv(feature))
38 .collect(),
39 }
40 }
41
42 pub fn preprocess(&mut self) {
43 use open_jtalk::*;
44
45 pronunciation::njd_set_pronunciation(self);
46 digit_sequence::njd_digit_sequence(self);
47 digit::njd_set_digit(self);
48 accent_phrase::njd_set_accent_phrase(self);
49 accent_type::njd_set_accent_type(self);
50 unvoiced_vowel::njd_set_unvoiced_vowel(self);
51 }
54}
55
56impl<'a> FromIterator<(&'a str, &'a WordEntry)> for NJD {
57 fn from_iter<I: IntoIterator<Item = (&'a str, &'a WordEntry)>>(iter: I) -> Self {
58 let nodes = iter
59 .into_iter()
60 .flat_map(|(text, word_entry)| NJDNode::load(text, word_entry))
61 .collect();
62 Self { nodes }
63 }
64}
65impl<'a> FromIterator<&'a str> for NJD {
66 fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
67 let nodes = iter.into_iter().flat_map(NJDNode::load_csv).collect();
68 Self { nodes }
69 }
70}
71
72impl IterQuintMutTrait for NJD {
73 type Item = NJDNode;
74 fn iter_quint_mut(&mut self) -> IterQuintMut<'_, Self::Item> {
75 IterQuintMut::new(&mut self.nodes)
76 }
77 fn iter_quint_mut_range(&mut self, start: usize, end: usize) -> IterQuintMut<'_, Self::Item> {
78 IterQuintMut::new(&mut self.nodes[start..end])
79 }
80}
81
82impl From<NJD> for Vec<String> {
83 fn from(njd: NJD) -> Self {
84 njd.nodes.into_iter().map(|node| node.to_string()).collect()
85 }
86}