Skip to main content

jpreprocess_njd/
lib.rs

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