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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! `oink` is a library & command-line tool that allows you to translate from English to Pig Latin.
//!
//! ## How to use as a library
//!
//! ```
//! use oink::{word_to_pig_latin, sentence_to_pig_latin};
//!
//! // Convert a single word to pig latin & print it.
//! match word_to_pig_latin("Word") {
//!     Some(word) => {println!("{}", word)}
//!     None => {println!("Word")}
//! }
//!
//! // Convert a sentence/paragraph(s) to pig latin & print it.
//! match sentence_to_pig_latin("This is a sentence.") {
//!     Some(sentence) => {println!("{}", sentence)}
//!     None => {println!("This is a sentence.")}
//! }
//! ```
//!
//! ## How to use as a command-line tool
//!
//! Install using `cargo`:
//!
//! ```
//! cargo install oink
//! ```
//!
//! Use command:
//!
//! ```
//! oink <STRING>
//! ```

#[macro_use]
extern crate lazy_static;
use regex::Regex;

/// Capitalize a word i.e. make the first letter of a word uppercase.
fn capitalize(word: &str) -> String {
    let mut chars = word.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
    }
}

/// Translates a single word from English to Pig Latin.
///
/// # Examples
///
/// ```
/// let translated = oink::word_to_pig_latin("Consequence").unwrap();
///
/// assert_eq!("Onsequencecay", &translated);
/// ```
pub fn word_to_pig_latin(word: &str) -> Option<String> {
    lazy_static! {
        static ref IS_VOWEL: Regex = Regex::new(r"(?i)[aeiou]").unwrap();
        static ref IS_CONSONANT: Regex = Regex::new(r"(?i)[^aeiou]+").unwrap();
    }
    if word.len() == 0 {
        return None;
    }
    let word = word;
    if IS_VOWEL.is_match(&word[0..1]) {
        Some(format!("{}yay", {
            if word.chars().next().unwrap().is_uppercase() {
                capitalize(word)
            } else {
                word.to_string()
            }
        }))
    } else {
        let mat = IS_CONSONANT.find(&word).unwrap();
        Some(format!(
            "{}{}ay",
            {
                let consonant = &word[0..mat.end()];
                let other = &word[mat.end()..];
                if consonant.chars().next().unwrap().is_uppercase() {
                    capitalize(&word[mat.end()..])
                } else {
                    other.to_string()
                }
            },
            &word[0..mat.end()].to_lowercase()
        ))
    }
}

/// Translates a sentence from English to Pig Latin.
///
/// # Examples
///
/// ```
/// let translated = oink::sentence_to_pig_latin("Hello, my name is Scott Walker.").unwrap();
///
/// assert_eq!("Ellohay, myay amenay isyay Ottscay Alkerway.", &translated);
/// ```
pub fn sentence_to_pig_latin(sentence: &str) -> Option<String> {
    if sentence.len() == 0 {
        return None;
    }

    let is_word = Regex::new(r"(\w+'?\w*)").unwrap();
    let words = is_word.find_iter(sentence);
    let mut sentence = sentence.to_string();
    for word in words {
        let reg = format!(r"\b{}\b", word.as_str());
        let word_regex = Regex::new(&reg).unwrap();
        let pig = word_to_pig_latin(word.as_str());
        match pig {
            Some(pig) => {
                let pig_ref = pig.as_str();
                let replaced = word_regex.replacen(&sentence, 1, pig_ref);
                sentence = replaced.to_string();
            }
            None => {}
        }
    }

    Some(sentence)
}

#[cfg(test)]
mod tests {
    use crate::{sentence_to_pig_latin, word_to_pig_latin};

    #[test]
    fn to_pig_consonant() {
        assert_eq!(word_to_pig_latin("Hello").unwrap(), "ellohay");
    }

    #[test]
    fn to_pig_consonant_cluster() {
        assert_eq!(word_to_pig_latin("Smile").unwrap(), "ilesmay");
    }

    #[test]
    fn to_pig_vowel() {
        assert_eq!(word_to_pig_latin("Aman").unwrap(), "amanyay");
    }

    #[test]
    fn to_pig_sentence() {
        assert_eq!(
            sentence_to_pig_latin("Hello, I am Aman").unwrap(),
            "ellohay, iyay amyay amanyay"
        );
    }
}