1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use crate::entities::sentence::Sentence;
use regex::Regex;

/// Structure of novel line
///
/// Lines are defined below:
/// * Starts after breakline or chapter head
/// * End with breakline
pub struct Line {
    elements: Vec<Sentence>,
}

/// Implementation of novel line structure
impl Line {
    /// Constructor
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::line::Line;
    ///
    /// Line::new("我が輩は猫である。名前はまだない。");
    /// ```
    pub fn new(text: &str) -> Self {
        Self::format(text)
    }

    /// Print formatted line
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::line::Line;
    ///
    /// let line = Line::new("我が輩は猫である。名前はまだない。");
    /// line.print()
    /// ```
    pub fn print(self) {
        for element in self.elements {
            element.print();
        }
    }

    /// Get string of formatted sentence
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::line::Line;
    /// let line = Line::new("我が[輩:.]は[猫:ねこ]である。どこで生まれたかとんと見当がつかぬ。");
    /// assert_eq!(line.get(), " 我が|輩《・》は|猫《ねこ》である。どこで生まれたかとんと見当がつかぬ。");
    /// ```
    ///
    pub fn get(self) -> String {
        self.elements.into_iter().map(|sentence| sentence.get()).collect()
    }

    /// Format line
    fn format(text: &str) -> Self {
        let line = Self::add_header_space(text.trim());
        let line = Self::split(&line)
            .into_iter()
            .map(|sentence| Sentence::new(&sentence))
            .collect();
        Self { elements: line }
    }

    /// Insert 2 byte whitespace to line head
    fn add_header_space(text: &str) -> String {
        if Self::is_speech(text) {
            return " ".to_string() + text;
        }
        " ".to_string() + text
    }

    /// Split line to sentences
    fn split(text: &str) -> Vec<&str> {
        let sentence_terminators = Regex::new(r".*([」。.?!]|!\?|\?!|\z)").unwrap();
        sentence_terminators.find_iter(text).map(|m| m.as_str()).collect()
    }

    /// Return true if a line is speech line
    fn is_speech(text: &str) -> bool {
        let line_head = text.chars().nth(0).unwrap_or(' ');
        match line_head {
            '「' => true,
            _ => false,
        }
    }
}
#[cfg(test)]
mod tests {
    use super::Line;

    #[test]
    fn get() {
        let source = "我が[輩:.]は[猫:ねこ]である。どこで生まれたかとんと見当がつかぬ。";
        let expected = " 我が|輩《・》は|猫《ねこ》である。どこで生まれたかとんと見当がつかぬ。";
        let line = Line::new(&source);
        assert_eq!(line.get(), expected);
    }

    #[test]
    fn get_min() {
        let source = "我";
        let expected = " 我";
        let line = Line::new(&source);
        assert_eq!(line.get(), expected);
    }
}