Pig Latin

1. Readme

猪的拉丁文

实现一个从英语翻译成猪拉丁语的程序.

猪拉丁语是一种拼凑的儿童语言,目的是使人困惑。它遵循一些简单的规则(下面),但是当它说得很快时,对于非儿童(以及非母语者)来说真的很难理解.

  • 规则 1如果一个单词以元音开头,在单词的末尾加上一个”ay”音。请注意,在单词开头的”xr”和”yt”会产生元音(例如 “xray” -> “xrayay”, “yttria” -> “yttriaay”)。
  • 规则 2如果一个单词以辅音开头,把它移到单词的末尾,然后在单词的末尾加上一个”ay”音。辅音可以由多个辅音组成,例如辅音群(例如”chair” -> “airchay”).
  • 规则 3如果一个单词以辅音开头,后面跟着”qu”,把它移动到单词的结尾,然后在单词的结尾加上”ay”音(例如,”square” -> “aresquay”).
  • 规则 4如果一个单词在辅音群后面包含”y”,或者作为两个字母元音的单词的第二个字母(例如,”rhythm” -> “ythmrhay”, “my” -> “ymay”)。

边缘案例还有一些规则,也有区域性的变化.

http://en.wikipedia.org/wiki/Pig_latin更多细节.

资源

猪拉丁文运动,在第一次超声波教学中的应用https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

2. 开始你的表演

pub fn translate(input: &str) -> String {
   unimplemented!(
       "Using the Pig Latin text transformation rules, convert the given input '{}'",
       input
   );
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[test]
fn test_word_beginning_with_a() {
   assert_eq!(translate("apple"), "appleay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_e() {
   assert_eq!(translate("ear"), "earay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_i() {
   assert_eq!(translate("igloo"), "iglooay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_o() {
   assert_eq!(translate("object"), "objectay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_u() {
   assert_eq!(translate("under"), "underay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_a_vowel_and_followed_by_a_qu() {
   assert_eq!(translate("equal"), "equalay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_p() {
   assert_eq!(translate("pig"), "igpay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_k() {
   assert_eq!(translate("koala"), "oalakay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_y() {
   assert_eq!(translate("yellow"), "ellowyay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_x() {
   assert_eq!(translate("xenon"), "enonxay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_q_without_a_following_u() {
   assert_eq!(translate("qat"), "atqay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_ch() {
   assert_eq!(translate("chair"), "airchay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_qu() {
   assert_eq!(translate("queen"), "eenquay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_qu_and_a_preceding_consonant() {
   assert_eq!(translate("square"), "aresquay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_th() {
   assert_eq!(translate("therapy"), "erapythay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_thr() {
   assert_eq!(translate("thrush"), "ushthray");
}

#[test]
//#[ignore]
fn test_word_beginning_with_sch() {
   assert_eq!(translate("school"), "oolschay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_yt() {
   assert_eq!(translate("yttria"), "yttriaay");
}

#[test]
//#[ignore]
fn test_word_beginning_with_xr() {
   assert_eq!(translate("xray"), "xrayay");
}

#[test]
//#[ignore]
fn test_y_is_treated_like_a_vowel_at_the_end_of_a_consonant_cluster() {
   assert_eq!(translate("rhythm"), "ythmrhay");
}

#[test]
//#[ignore]
fn test_a_whole_phrase() {
   assert_eq!(translate("quick fast run"), "ickquay astfay unray");
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
#[macro_use]
extern crate lazy_static;
extern crate regex;

use regex::Regex;

// Regular expressions from Python version of exercism

pub fn translate_word(word: &str) -> String {
   // Prevent creation and compilation at every call.
   // These are compiled exactly once
   lazy_static! {
       // Detects if it starts with a vowel
       static ref VOWEL: Regex = Regex::new(r"^([aeiou]|y[^aeiou]|xr)[a-z]*").unwrap();
       // Detects splits for initial consonants
       static ref CONSONANTS: Regex = Regex::new(r"^([^aeiou]?qu|[^aeiou][^aeiouy]*)([a-z]*)").unwrap();
   }

   if VOWEL.is_match(word) {
       String::from(word) + "ay"
   } else {
       let caps = CONSONANTS.captures(word).unwrap();
       String::from(&caps[2]) + &caps[1] + "ay"
   }
}

pub fn translate(text: &str) -> String {
   text.split(" ")
       .map(|w| translate_word(w))
       .collect::<Vec<_>>()
       .join(" ")
}

#}



填充/相关