Compare commits
74 Commits
Author | SHA1 | Date |
---|---|---|
John Zacarias Jekel | 0179a99a32 | 1 year ago |
John Zacarias Jekel | 1dabce5224 | 1 year ago |
John Zacarias Jekel | e48f584262 | 1 year ago |
John Jekel | 4e28518ab3 | 1 year ago |
John Zacarias Jekel | 3d4dbf1ae4 | 1 year ago |
John Zacarias Jekel | 8e676b449d | 1 year ago |
John Zacarias Jekel | 1c1df67d64 | 1 year ago |
John Zacarias Jekel | 10af4bd813 | 1 year ago |
John Zacarias Jekel | ee2eef8b25 | 1 year ago |
John Zacarias Jekel | 21225aa4bc | 1 year ago |
John Zacarias Jekel | a97120228a | 1 year ago |
John Jekel | 406484b45e | 1 year ago |
John Jekel | 22c7944a7d | 1 year ago |
John Zacarias Jekel | a7866e648c | 1 year ago |
John Zacarias Jekel | 796543722a | 1 year ago |
John Jekel | 80fa163ce4 | 1 year ago |
John Jekel | edcde04247 | 1 year ago |
John Jekel | 1d83496e8a | 1 year ago |
John Jekel | c9663b9e14 | 1 year ago |
John Jekel | a425325fe6 | 1 year ago |
John Zacarias Jekel | b9d341553b | 1 year ago |
John Zacarias Jekel | 47cbf3aba0 | 1 year ago |
John Zacarias Jekel | 92d817489d | 1 year ago |
John Zacarias Jekel | c37d6a1b94 | 1 year ago |
John Zacarias Jekel | 197fd51228 | 1 year ago |
John Zacarias Jekel | c046d15723 | 1 year ago |
John Zacarias Jekel | dc6bb6a0d0 | 1 year ago |
John Zacarias Jekel | d03c413ee7 | 1 year ago |
John Zacarias Jekel | 2d2333677c | 1 year ago |
John Zacarias Jekel | 2be7ede4ee | 1 year ago |
John Zacarias Jekel | 97cc72fc47 | 1 year ago |
John Zacarias Jekel | bc05413834 | 1 year ago |
John Zacarias Jekel | e535a69ac3 | 1 year ago |
John Zacarias Jekel | 4d9a89dec2 | 1 year ago |
John Zacarias Jekel | 9ebeb071da | 1 year ago |
John Zacarias Jekel | e232c34202 | 1 year ago |
John Zacarias Jekel | 0684da4741 | 1 year ago |
John Zacarias Jekel | efbcba6080 | 1 year ago |
John Zacarias Jekel | 39bdc0eb62 | 1 year ago |
John Zacarias Jekel | 1c0c52a975 | 1 year ago |
John Zacarias Jekel | caf6bdee78 | 1 year ago |
John Zacarias Jekel | e4e898847d | 1 year ago |
John Zacarias Jekel | 9b8688271a | 1 year ago |
John Zacarias Jekel | 3ebceaff28 | 1 year ago |
John Jekel | 107efc314e | 1 year ago |
John Zacarias Jekel | 870512da4f | 1 year ago |
John Zacarias Jekel | 3b7e6b85a8 | 1 year ago |
John Zacarias Jekel | 46b24494e8 | 1 year ago |
John Zacarias Jekel | a0417566ed | 1 year ago |
John Zacarias Jekel | 13a36fc15b | 1 year ago |
John Zacarias Jekel | d898f44220 | 1 year ago |
John Zacarias Jekel | 37c4b0fac0 | 1 year ago |
John Zacarias Jekel | 2f2e6d782b | 1 year ago |
John Zacarias Jekel | a55ae8c857 | 1 year ago |
John Zacarias Jekel | a989f9eb31 | 1 year ago |
John Zacarias Jekel | e563d24d65 | 1 year ago |
John Zacarias Jekel | 77a7b190a0 | 1 year ago |
John Zacarias Jekel | 992aa61afd | 1 year ago |
John Zacarias Jekel | 1bc7b19916 | 1 year ago |
John Zacarias Jekel | eccd70b586 | 1 year ago |
John Zacarias Jekel | b49c43c6ee | 1 year ago |
John Jekel | d557d80a20 | 1 year ago |
John Zacarias Jekel | 15f5a3f979 | 1 year ago |
John Zacarias Jekel | da98dc30cd | 1 year ago |
John Zacarias Jekel | d8afef06f4 | 1 year ago |
John Zacarias Jekel | 145df94a80 | 1 year ago |
John Zacarias Jekel | 02f2c30076 | 1 year ago |
John Zacarias Jekel | 36779d0923 | 1 year ago |
John Zacarias Jekel | c16ab972f6 | 1 year ago |
John Zacarias Jekel | c85c98a5c7 | 1 year ago |
John Zacarias Jekel | c10248db01 | 1 year ago |
John Zacarias Jekel | 3c43ea6cf2 | 1 year ago |
John Zacarias Jekel | 0150a771be | 1 year ago |
John Zacarias Jekel | db99cfe29f | 1 year ago |
@ -0,0 +1 @@ |
||||
|
@ -1,38 +0,0 @@ |
||||
/* anslatetray-file
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Frontend to translate a file |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use anslatortray::translate; |
||||
|
||||
/* Functions */ |
||||
|
||||
fn main() { |
||||
eprintln!("Note: anslatetray-file is highly experimental and has poor error handling. You have been warned."); |
||||
|
||||
//Get all arguments after the executable's name
|
||||
let args: Vec<String> = std::env::args().collect(); |
||||
if args.len() != 3 { |
||||
eprintln!("Error: expected two arguments, the input file to be translated and the file to output the translated text to"); |
||||
return; |
||||
} |
||||
|
||||
//TODO error handling
|
||||
//TODO preserve file formatting better
|
||||
|
||||
let input_file = std::env::args().nth(1).unwrap(); |
||||
let output_file = std::env::args().nth(2).unwrap(); |
||||
|
||||
let file_contents = std::fs::read_to_string(input_file).unwrap(); |
||||
let start_time = std::time::Instant::now(); |
||||
let translated_file_contents = translate(&file_contents); |
||||
let time_to_translate = start_time.elapsed(); |
||||
std::fs::write(output_file, &translated_file_contents).unwrap(); |
||||
|
||||
eprintln!("Sucessful: took {}ns to translate", time_to_translate.as_nanos()); |
||||
} |
@ -1,29 +0,0 @@ |
||||
/* anslatetray
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Simple command-line frontend for the Anslatortray for Rust |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use anslatortray::translate; |
||||
|
||||
/* Functions */ |
||||
|
||||
fn main() { |
||||
//Get all arguments after the executable's name
|
||||
let mut args: Vec<String> = std::env::args().collect(); |
||||
if args.len() < 2 { |
||||
eprintln!("Error: expected at least one string to translate"); |
||||
return; |
||||
} |
||||
args.remove(0); |
||||
|
||||
//Translate the arguments and print them out for the user
|
||||
for string in args { |
||||
print!("{} ", translate(&string)); |
||||
} |
||||
println!(); |
||||
} |
@ -0,0 +1,175 @@ |
||||
/* anslatortray
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Command-line frontend for the anslatortray library |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use anslatortray::translate; |
||||
use anslatortray::byte_string; |
||||
|
||||
/* Functions */ |
||||
|
||||
fn main() { |
||||
eprintln!("Anslatortray: frontend for the Anslatortray for Rust library\n"); |
||||
|
||||
//Get all arguments after the executable's name
|
||||
let mut args: Vec<String> = std::env::args().collect(); |
||||
args.remove(0); |
||||
|
||||
if args.len() == 0 { |
||||
interactive(&args); |
||||
return; |
||||
} |
||||
|
||||
let option = args[0].clone(); |
||||
args.remove(0); |
||||
|
||||
match option.as_str() { |
||||
"--help" => { help(); }, |
||||
"--interactive" => { interactive(&args); }, |
||||
"--file" => { file(&args); }, |
||||
"--benchmark-file" => { benchmark_file(&args); }, |
||||
"--translate-args" => { translate_args(&args); }, |
||||
"--stdin-to-stdout" => { stdin_to_stdout(&args); }, |
||||
bad_option => { |
||||
eprintln!("Error: {} is not a valid option", bad_option); |
||||
help(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
fn help() { |
||||
eprintln!("Options:"); |
||||
eprintln!("--help Print this helpful text!"); |
||||
eprintln!("--interactive Start an interactive translation session"); |
||||
eprintln!("--file Translate a file (requires two arguments, the file to translate and the destination)"); |
||||
eprintln!("--benchmark-file Benchmark translating a file (requires two arguments, the file to translate and the number of iterations to perform)"); |
||||
eprintln!("--translate-args Translates all remaining arguments provided and outputs them to stdout"); |
||||
eprintln!("--stdin-to-stdout Translates input from stdin directly to stdout"); |
||||
|
||||
eprintln!("\n{}", translate("Have a good day!")); |
||||
} |
||||
|
||||
fn interactive(args: &Vec<String>) { |
||||
if args.len() != 0 { |
||||
eprintln!("Error: didn't expect any arguments"); |
||||
help(); |
||||
return; |
||||
} |
||||
|
||||
eprintln!("Starting interactive mode!"); |
||||
eprintln!("Type what you'd like to translate and then press enter, or press Ctrl+C to exit...\n"); |
||||
|
||||
let stdin = std::io::stdin(); |
||||
let mut line_buffer = String::new(); |
||||
loop { |
||||
eprint!("anslatortray> "); |
||||
stdin.read_line(&mut line_buffer).unwrap(); |
||||
eprintln!("{}", translate(&line_buffer)); |
||||
line_buffer.truncate(0); |
||||
} |
||||
} |
||||
|
||||
fn file(args: &Vec<String>) { |
||||
eprintln!("Note: anslatortray --file is highly experimental and has poor error handling. You have been warned."); |
||||
|
||||
if args.len() != 2 { |
||||
eprintln!("Error: expected two arguments, two arguments, the file to translate and the destination"); |
||||
help(); |
||||
return; |
||||
} |
||||
|
||||
//TODO error handling
|
||||
//TODO switch to using byte_string for efficiency
|
||||
|
||||
let input_file = &args[0]; |
||||
let output_file = &args[1]; |
||||
|
||||
let file_contents = std::fs::read_to_string(input_file).unwrap(); |
||||
let start_time = std::time::Instant::now(); |
||||
let translated_file_contents = translate(&file_contents); |
||||
let time_to_translate = start_time.elapsed(); |
||||
std::fs::write(output_file, &translated_file_contents).unwrap(); |
||||
|
||||
eprintln!("Sucessful: took {}ns to translate", time_to_translate.as_nanos()); |
||||
} |
||||
|
||||
fn benchmark_file(args: &Vec<String>) { |
||||
eprintln!("Note: anslatortray --benchmark-file is highly experimental and has poor error handling. You have been warned."); |
||||
|
||||
if args.len() != 2 { |
||||
eprintln!("Error: expected two arguments, the file to translate and the number of iterations to perform"); |
||||
help(); |
||||
return; |
||||
} |
||||
|
||||
//TODO error handling
|
||||
|
||||
let input_file = &args[0]; |
||||
let iterations = args[1].parse::<u128>().unwrap();//TODO error handling
|
||||
|
||||
let file_contents = std::fs::read_to_string(input_file).unwrap(); |
||||
|
||||
let mut total_duration_regular = std::time::Duration::new(0, 0); |
||||
|
||||
for _ in 0..iterations { |
||||
let start_time = std::time::Instant::now(); |
||||
let translated_file_contents = translate(&file_contents); |
||||
let time_to_translate = start_time.elapsed(); |
||||
|
||||
total_duration_regular += time_to_translate; |
||||
std::fs::write("/dev/null", &translated_file_contents).unwrap();//TODO avoid needing unix
|
||||
} |
||||
eprintln!("Sucessful: Regular translation took {}ns to translate on average over {} runs.", total_duration_regular.as_nanos() / iterations, iterations); |
||||
|
||||
let mut total_duration_byte_string = std::time::Duration::new(0, 0); |
||||
|
||||
let mut translated_file_contents = Vec::<u8>::new();//TODO set a sane initial size
|
||||
for _ in 0..iterations { |
||||
let start_time = std::time::Instant::now(); |
||||
translated_file_contents.truncate(0); |
||||
byte_string::translate(file_contents.as_bytes(), &mut translated_file_contents); |
||||
let time_to_translate = start_time.elapsed(); |
||||
total_duration_byte_string += time_to_translate; |
||||
std::fs::write("/dev/null", &translated_file_contents).unwrap();//TODO avoid needing unix
|
||||
} |
||||
eprintln!("Sucessful: Byte-string translation with reused allocations took {}ns to translate on average over {} runs.", total_duration_byte_string.as_nanos() / iterations, iterations); |
||||
} |
||||
|
||||
fn translate_args(args: &Vec<String>) { |
||||
if args.len() == 0 { |
||||
eprintln!("Error: expected at least one string to translate"); |
||||
help(); |
||||
return; |
||||
} |
||||
|
||||
//Translate the arguments and print them out for the user
|
||||
for string in args { |
||||
print!("{} ", translate(&string)); |
||||
} |
||||
println!(); |
||||
} |
||||
|
||||
fn stdin_to_stdout(args: &Vec<String>) { |
||||
use std::io::{Read, Write}; |
||||
|
||||
if args.len() != 0 { |
||||
eprintln!("Error: didn't expect any arguments"); |
||||
help(); |
||||
return; |
||||
} |
||||
|
||||
let mut stdin = std::io::stdin(); |
||||
let mut stdout = std::io::stdout(); |
||||
let mut buffer = String::new(); |
||||
|
||||
while let Ok(bytes_read) = stdin.read_to_string(&mut buffer) { |
||||
if bytes_read == 0 { return; } |
||||
write!(stdout, "{}", translate(&buffer)).unwrap();//TODO do this more efficiently (avoid format string)
|
||||
buffer.truncate(0);//TODO is this needed here?
|
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,111 +0,0 @@ |
||||
/* helpers.rs
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Contains helper functions for translate_words.rs |
||||
* |
||||
*/ |
||||
|
||||
/* Functions */ |
||||
|
||||
///Returns whether a letter is a vowel or not.
|
||||
///
|
||||
///If the parameter is a letter, returns Some(true) if it is a vowel, and Some(false) otherwise.
|
||||
///If the parameter isn't a letter, it will return None
|
||||
///
|
||||
///This is a helper function used by [`translate_word()`](crate::translate_word), but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///for letter in "aeiouAEIOU".chars() {
|
||||
/// assert!(anslatortray::is_vowel(letter).unwrap());
|
||||
///}
|
||||
///
|
||||
///for letter in "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ".chars() {
|
||||
/// assert!(!anslatortray::is_vowel(letter).unwrap());
|
||||
///}
|
||||
///
|
||||
///for not_letter in " !@#$%^&*()_+={}|\":>?~`\\][';/.,".chars() {
|
||||
/// assert!(matches!(anslatortray::is_vowel(not_letter), None));
|
||||
///}
|
||||
///```
|
||||
pub fn is_vowel(letter: char) -> Option<bool> { |
||||
if !letter.is_alphabetic() { |
||||
return None; |
||||
} |
||||
|
||||
match letter.to_ascii_lowercase() { |
||||
'a' | 'e' | 'i' | 'o' | 'u' => { return Some(true); } |
||||
_ => { return Some(false); } |
||||
} |
||||
} |
||||
|
||||
///Returns whether a letter is y or not.
|
||||
///
|
||||
///If the parameter is a letter, returns Some(true) if it is y, and Some(false) otherwise.
|
||||
///If the parameter isn't a letter, it will return None
|
||||
///
|
||||
///This is a helper function used by [`translate_word()`](crate::translate_word), but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///for letter in "yY".chars() {
|
||||
/// assert!(anslatortray::is_y(letter).unwrap());
|
||||
///}
|
||||
///
|
||||
///for letter in "abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXZ".chars() {
|
||||
/// assert!(!anslatortray::is_y(letter).unwrap());
|
||||
///}
|
||||
///
|
||||
///for not_letter in " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n".chars() {
|
||||
/// assert!(matches!(anslatortray::is_y(not_letter), None));
|
||||
///}
|
||||
///```
|
||||
pub fn is_y(letter: char) -> Option<bool> { |
||||
if !letter.is_alphabetic() { |
||||
return None; |
||||
} |
||||
|
||||
return Some(letter.to_ascii_lowercase() == 'y'); |
||||
} |
||||
|
||||
/* Tests */ |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn test_is_vowel() { |
||||
for letter in "aeiouAEIOU".chars() { |
||||
assert!(is_vowel(letter).unwrap()); |
||||
} |
||||
|
||||
for letter in "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ".chars() { |
||||
assert!(!is_vowel(letter).unwrap()); |
||||
} |
||||
|
||||
for not_letter in " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n".chars() { |
||||
assert!(matches!(is_vowel(not_letter), None)); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn test_is_y() { |
||||
for letter in "yY".chars() { |
||||
assert!(is_y(letter).unwrap()); |
||||
} |
||||
|
||||
for letter in "abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXZ".chars() { |
||||
assert!(!is_y(letter).unwrap()); |
||||
} |
||||
|
||||
for not_letter in " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n".chars() { |
||||
assert!(matches!(is_y(not_letter), None)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,397 @@ |
||||
/* string.rs
|
||||
* By: John Jekel |
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Translation functions operating on &str and String (the ones most users will want to use) |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use crate::byte_string::translate_with_style_lower_and_upper_suffixes as translate_byte_string_with_style_lower_and_upper_suffixes; |
||||
|
||||
/* Functions */ |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin!
|
||||
///
|
||||
///Uses the default suffix and special_case_suffix, "ay" and "way" respectively when calling [`translate_with_style()`].
|
||||
///
|
||||
///Equivalent to [`translate_way()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate;
|
||||
///
|
||||
///assert_eq!(translate("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyway Englishway exttay. Itway ancay evenway andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeway asescay. Atthay isway away ontractioncay, asway ellway asway away ordway erewhay ethay onlyway owelvay isway yway. Eatnay, allway atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz"),
|
||||
/// "Atwhay ifway away ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("Hello-world"), "Ellohay-orldway");
|
||||
///assert_eq!(translate("Hyphens-are-difficult-aren't-they?"), "Yphenshay-areway-ifficultday-arenway't-eythay?");
|
||||
///```
|
||||
pub fn translate(english: &str) -> String { |
||||
return translate_way(english); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin (way-style)!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "way" respectively when calling [`translate_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_way;
|
||||
///
|
||||
///assert_eq!(translate_way("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate_way("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyway Englishway exttay. Itway ancay evenway andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeway asescay. Atthay isway away ontractioncay, asway ellway asway away ordway erewhay ethay onlyway owelvay isway yway. Eatnay, allway atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz"),
|
||||
/// "Atwhay ifway away ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("Hello-world"), "Ellohay-orldway");
|
||||
///assert_eq!(translate_way("Hyphens-are-difficult-aren't-they?"), "Yphenshay-areway-ifficultday-arenway't-eythay?");
|
||||
///```
|
||||
pub fn translate_way(english: &str) -> String { |
||||
return translate_with_style_lower_and_upper_suffixes(english, "ay", "way", "AY", "WAY"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin (yay-style)!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "yay" respectively when calling [`translate_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_yay;
|
||||
///
|
||||
///assert_eq!(translate_yay("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate_yay("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyyay Englishyay exttay. Ityay ancay evenyay andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeyay asescay. Atthay isyay ayay ontractioncay, asyay ellway asyay ayay ordway erewhay ethay onlyyay owelvay isyay yyay. Eatnay, allyay atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz"),
|
||||
/// "Atwhay ifyay ayay ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("Hello-world"), "Ellohay-orldway");
|
||||
///assert_eq!(translate_yay("Hyphens-are-difficult-aren't-they?"), "Yphenshay-areyay-ifficultday-arenyay't-eythay?");
|
||||
///```
|
||||
pub fn translate_yay(english: &str) -> String { |
||||
return translate_with_style_lower_and_upper_suffixes(english, "ay", "yay", "AY", "YAY"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin (hay-style)!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "hay" respectively when calling [`translate_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_hay;
|
||||
///
|
||||
///assert_eq!(translate_hay("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate_hay("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyhay Englishhay exttay. Ithay ancay evenhay andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_hay("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgehay asescay. Atthay ishay ahay ontractioncay, ashay ellway ashay ahay ordway erewhay ethay onlyhay owelvay ishay yhay. Eatnay, allhay atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_hay("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz"),
|
||||
/// "Atwhay ifhay ahay ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_hay("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_hay("Hello-world"), "Ellohay-orldway");
|
||||
///assert_eq!(translate_hay("Hyphens-are-difficult-aren't-they?"), "Yphenshay-arehay-ifficultday-arenhay't-eythay?");
|
||||
///```
|
||||
pub fn translate_hay(english: &str) -> String { |
||||
return translate_with_style_lower_and_upper_suffixes(english, "ay", "hay", "AY", "HAY"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Ferb Latin!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "erb" and "ferb" respectively when calling [`translate_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_ferb;
|
||||
///
|
||||
///assert_eq!(translate_ferb("Hello world from the coolest Ferb Latin translator!"), "Elloherb orldwerb omfrerb etherb oolestcerb Erbferb Atinlerb anslatortrerb!");
|
||||
///
|
||||
///assert_eq!(translate_ferb("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Istherb ibrarylerb ancerb anslatetrerb anyferb Englishferb extterb. Itferb ancerb evenferb andleherb ultiplemerb entencesserb!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_ferb("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlerb's ytrerb omeserb edgeferb asescerb. Attherb isferb aferb ontractioncerb, asferb ellwerb asferb aferb ordwerb erewherb etherb onlyferb owelverb isferb yferb. Eatnerb, allferb attherb orkswerb!"
|
||||
///);
|
||||
///assert_eq!(translate_ferb("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz"),
|
||||
/// "Atwherb ifferb aferb ordwerb asherb onerb owelsverb, ikelerb istherb: bcdfghjklmnpqrstvwxzerb"
|
||||
///);
|
||||
///assert_eq!(translate_ferb("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcerb, oserb etherb euristicsherb akemerb ettyprerb oodgerb uessesgerb ithwerb atwherb eytherb're edferb!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_ferb("Hello-world"), "Elloherb-orldwerb");
|
||||
///assert_eq!(translate_ferb("Hyphens-are-difficult-aren't-they?"), "Yphensherb-areferb-ifficultderb-arenferb't-eytherb?");
|
||||
///```
|
||||
pub fn translate_ferb(english: &str) -> String { |
||||
return translate_with_style_lower_and_upper_suffixes(english, "erb", "ferb", "ERB", "FERB"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into a custom-styled play language!
|
||||
///
|
||||
///Pass the string you wish to translate, the suffix you wish to have appended to most words, and the suffix
|
||||
///you wish to have appended in various special-cases (such as when a word is only one letter or starts with a vowel).
|
||||
///
|
||||
///Note: The suffixes must be entirely lower-case or weird results may occur.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_with_style;
|
||||
///
|
||||
///let suffix = "ancy";
|
||||
///let special_case_suffix = "fancy";
|
||||
///
|
||||
///assert_eq!(translate_with_style("Hello world from the coolest Pig Latin translator!", suffix, special_case_suffix),
|
||||
/// "Ellohancy orldwancy omfrancy ethancy oolestcancy Igpancy Atinlancy anslatortrancy!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_with_style("This library can translate any English text. It can even handle multiple sentences!", suffix, special_case_suffix),
|
||||
/// "Isthancy ibrarylancy ancancy anslatetrancy anyfancy Englishfancy exttancy. Itfancy ancancy evenfancy andlehancy ultiplemancy entencessancy!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_with_style("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!", suffix, special_case_suffix),
|
||||
/// "Etlancy's ytrancy omesancy edgefancy asescancy. Atthancy isfancy afancy ontractioncancy, asfancy ellwancy asfancy afancy ordwancy erewhancy ethancy onlyfancy owelvancy isfancy yfancy. Eatnancy, allfancy atthancy orkswancy!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_with_style("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz", suffix, special_case_suffix),
|
||||
/// "Atwhancy iffancy afancy ordwancy ashancy onancy owelsvancy, ikelancy isthancy: bcdfghjklmnpqrstvwxzancy"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_with_style("Cool, so the heuristics make pretty good guesses with what they're fed!", suffix, special_case_suffix),
|
||||
/// "Oolcancy, osancy ethancy euristicshancy akemancy ettyprancy oodgancy uessesgancy ithwancy atwhancy eythancy're edfancy!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_with_style("Hello-world", suffix, special_case_suffix), "Ellohancy-orldwancy");
|
||||
///
|
||||
///assert_eq!(translate_with_style("Hyphens-are-difficult-aren't-they?", suffix, special_case_suffix), "Yphenshancy-arefancy-ifficultdancy-arenfancy't-eythancy?");
|
||||
///```
|
||||
pub fn translate_with_style(english: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String { |
||||
//Convert the suffix and special_case_suffix we were provided to uppercase for words that are capitalized
|
||||
let mut suffix_upper = String::with_capacity(suffix_lower.len()); |
||||
for letter in suffix_lower.chars() { |
||||
suffix_upper.push(letter.to_ascii_uppercase()); |
||||
} |
||||
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len()); |
||||
for letter in special_case_suffix_lower.chars() { |
||||
special_case_suffix_upper.push(letter.to_ascii_uppercase()); |
||||
} |
||||
|
||||
return translate_with_style_lower_and_upper_suffixes ( |
||||
english, |
||||
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper |
||||
); |
||||
} |
||||
|
||||
//More efficient: Does not need to convert to upppercase at runtime
|
||||
fn translate_with_style_lower_and_upper_suffixes ( |
||||
english: &str, |
||||
suffix_lower: &str, special_case_suffix_lower: &str, suffix_upper: &str, special_case_suffix_upper: &str |
||||
) -> String { |
||||
//Convert the string slices to byte slices and translate those (only ASCII letters are affected, non-letters or UTF-8 are preserved)
|
||||
let mut pig_latin_string_bytes = Vec::<u8>::with_capacity(english.len() * 2);//Plenty of headroom in case the words are very small or the suffixes are long
|
||||
translate_byte_string_with_style_lower_and_upper_suffixes ( |
||||
english.as_bytes(), |
||||
suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), suffix_upper.as_bytes(), special_case_suffix_upper.as_bytes(), |
||||
&mut pig_latin_string_bytes |
||||
); |
||||
|
||||
//This is safe since translate_byte_string_with_style does not touch any unicode bytes (it just copies them)
|
||||
return unsafe { String::from_utf8_unchecked(pig_latin_string_bytes) }; |
||||
} |
||||
|
||||
/* Tests */ |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
const SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS: [(&str, &str); 9] = [ |
||||
("ay", "way"), ("ay", "yay"), ("ay", "hay"), ("erb", "ferb"), ("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust") |
||||
]; |
||||
const SUFFIX_SPECIAL_CASE_SUFFIX_LOWER_UPPER_TUPLES: [(&str, &str, &str, &str); 9] = [ |
||||
("ay", "way", "AY", "WAY"), ("ay", "yay", "AY", "YAY"), ("ay", "hay", "AY", "HAY"), ("erb", "ferb", "ERB", "FERB"), |
||||
("ancy", "fancy", "ANCY", "FANCY"), ("orange", "porange", "ORANGE", "PORANGE"), |
||||
("anana", "banana", "ANANA", "BANANA"), ("atin", "latin", "ATIN", "LATIN"), ("ust", "rust", "UST", "RUST"), |
||||
]; |
||||
|
||||
#[test] |
||||
fn test_translate_with_style() { |
||||
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS { |
||||
let suffix = pair.0; |
||||
let special_case_suffix = pair.1; |
||||
|
||||
assert_eq!(translate_with_style("Hello world from the coolest Pig Latin translator!", suffix, special_case_suffix), |
||||
"Elloh".to_string() + suffix + " orldw" + suffix + " omfr" + suffix + " eth" + suffix + " oolestc" + suffix + " Igp" + suffix + " Atinl" + suffix + " anslatortr" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("This library can translate any English text. It can even handle multiple sentences!", suffix, special_case_suffix), |
||||
"Isth".to_string() + suffix + " ibraryl" + suffix + " anc" + suffix + " anslatetr" + suffix + " any" + special_case_suffix + " English" + special_case_suffix + " extt" + suffix + |
||||
". It" + special_case_suffix + " anc" + suffix + " even" + special_case_suffix + " andleh" + suffix + " ultiplem" + suffix + " entencess" + suffix + "!" |
||||
); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_with_style_edgecases() { |
||||
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_PAIRS { |
||||
let suffix = pair.0; |
||||
let special_case_suffix = pair.1; |
||||
|
||||
assert_eq!(translate_with_style("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!", suffix, special_case_suffix), |
||||
"Etl".to_string() + suffix + "'s ytr" + suffix + " omes" + suffix + " edge" + special_case_suffix + " asesc" + suffix + ". Atth" + suffix + " is" + special_case_suffix + " a" + |
||||
special_case_suffix + " ontractionc" + suffix + ", as" + special_case_suffix + " ellw" + suffix + " as" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix + |
||||
" erewh" + suffix + " eth" + suffix + " only" + special_case_suffix + " owelv" + suffix + " is" + special_case_suffix + " y" + special_case_suffix + ". Eatn" + suffix + ", all" + |
||||
special_case_suffix + " atth" + suffix + " orksw" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxz", suffix, special_case_suffix), |
||||
"Atwh".to_string() + suffix + " if" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix + " ash" + suffix + " on" + suffix + " owelsv" + suffix + ", ikel" + suffix + " isth" + suffix + ": bcdfghjklmnpqrstvwxz" + suffix |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("Cool, so the heuristics make pretty good guesses with what they're fed!", suffix, special_case_suffix), |
||||
"Oolc".to_string() + suffix + ", os" + suffix + " eth" + suffix + " euristicsh" + suffix + " akem" + suffix + " ettypr" + suffix + " oodg" + suffix + " uessesg" + suffix + " ithw" + suffix + " atwh" + suffix + " eyth" + suffix + "'re edf" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("Hello-world", suffix, special_case_suffix), "Elloh".to_string() + suffix + "-orldw" + suffix); |
||||
|
||||
assert_eq!(translate_with_style("Hyphens-are-difficult-aren't-they?", suffix, special_case_suffix), |
||||
"Yphensh".to_string() + suffix + "-are" + special_case_suffix + "-ifficultd" + suffix + "-aren" + special_case_suffix + "'t-eyth" + suffix + "?" |
||||
); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_with_style_uppercase() { |
||||
for pair in SUFFIX_SPECIAL_CASE_SUFFIX_LOWER_UPPER_TUPLES { |
||||
let suffix_lower = pair.0; |
||||
let special_case_suffix_lower = pair.1; |
||||
let suffix_upper = pair.2; |
||||
let special_case_suffix_upper = pair.3; |
||||
|
||||
assert_eq!(translate_with_style("HELLO WORLD!", suffix_lower, special_case_suffix_lower), |
||||
"ELLOH".to_string() + suffix_upper + " ORLDW" + suffix_upper + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("ISN't THIS COOL?", suffix_lower, special_case_suffix_lower), |
||||
"ISN".to_string() + special_case_suffix_upper + "'t ISTH" + suffix_upper + " OOLC" + suffix_upper + "?" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("What ABOUT a MIX?", suffix_lower, special_case_suffix_lower), |
||||
"Atwh".to_string() + suffix_lower + " ABOUT" + special_case_suffix_upper + " a" + special_case_suffix_lower + " IXM" + suffix_upper + "?" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("Luke, I am your father!", suffix_lower, special_case_suffix_lower),//We don't want to capitalize single-letter words
|
||||
"Ukel".to_string() + suffix_lower + ", I" + special_case_suffix_lower+ " am" + special_case_suffix_lower + " oury" + suffix_lower + " atherf" + suffix_lower + "!" |
||||
); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Benches */ |
||||
|
||||
#[cfg_attr(feature = "nightly-features-benches", cfg(test))] |
||||
#[cfg(feature = "nightly-features-benches")] |
||||
mod benches { |
||||
extern crate test; |
||||
use test::Bencher; |
||||
use super::*; |
||||
|
||||
const PROJECT_DESCRIPTION: &str = "A simple Rust library to translate from English to Pig Latin!"; |
||||
const LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; |
||||
|
||||
#[bench] |
||||
fn way_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate(PROJECT_DESCRIPTION); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn yay_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_yay(PROJECT_DESCRIPTION); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn hay_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_hay(PROJECT_DESCRIPTION); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn ferb_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_ferb(PROJECT_DESCRIPTION); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn way_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate(LOREM_IPSUM); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn yay_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_yay(LOREM_IPSUM); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn hay_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_hay(LOREM_IPSUM); }); |
||||
} |
||||
|
||||
#[bench] |
||||
fn ferb_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { return translate_ferb(LOREM_IPSUM); }); |
||||
} |
||||
} |
@ -1,385 +0,0 @@ |
||||
/* translate_strings.rs
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Contains functions for translating multiple sentences. |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use crate::translate_words::translate_word_with_style; |
||||
|
||||
/* Functions */ |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin!
|
||||
///
|
||||
///Uses the default suffix and special_case_suffix, "ay" and "way" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///Equivalent to [`translate_way()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate;
|
||||
///
|
||||
///assert_eq!(translate("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyway Englishway exttay. Itway ancay evenway andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeway asescay. Atthay isway away ontractioncay, asway ellway asway away ordway erewhay ethay onlyway owelvay isway yway. Eatnay, allway atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"),
|
||||
/// "Atwhay ifway away ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///```
|
||||
pub fn translate(english: &str) -> String { |
||||
return translate_way(english); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin (way-style)!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "way" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_way;
|
||||
///
|
||||
///assert_eq!(translate_way("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate_way("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyway Englishway exttay. Itway ancay evenway andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeway asescay. Atthay isway away ontractioncay, asway ellway asway away ordway erewhay ethay onlyway owelvay isway yway. Eatnay, allway atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"),
|
||||
/// "Atwhay ifway away ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_way("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///```
|
||||
pub fn translate_way(english: &str) -> String { |
||||
return translate_with_style(english, "ay", "way"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Pig Latin (yay-style)!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "yay" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_yay;
|
||||
///
|
||||
///assert_eq!(translate_yay("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!");
|
||||
///
|
||||
///assert_eq!(translate_yay("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Isthay ibrarylay ancay anslatetray anyyay Englishyay exttay. Ityay ancay evenyay andlehay ultiplemay entencessay!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlay's ytray omesay edgeyay asescay. Atthay isyay ayay ontractioncay, asyay ellway asyay ayay ordway erewhay ethay onlyyay owelvay isyay yyay. Eatnay, allyay atthay orksway!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"),
|
||||
/// "Atwhay ifyay ayay ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZay"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_yay("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!"
|
||||
///);
|
||||
///```
|
||||
pub fn translate_yay(english: &str) -> String { |
||||
return translate_with_style(english, "ay", "yay"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into Ferb Latin!
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "erb" and "ferb" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_ferb;
|
||||
///
|
||||
///assert_eq!(translate_ferb("Hello world from the coolest Ferb Latin translator!"), "Elloherb orldwerb omfrerb etherb oolestcerb Erbferb Atinlerb anslatortrerb!");
|
||||
///
|
||||
///assert_eq!(translate_ferb("This library can translate any English text. It can even handle multiple sentences!"),
|
||||
/// "Istherb ibrarylerb ancerb anslatetrerb anyferb Englishferb extterb. Itferb ancerb evenferb andleherb ultiplemerb entencesserb!"
|
||||
///);
|
||||
///
|
||||
///assert_eq!(translate_ferb("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"),
|
||||
/// "Etlerb's ytrerb omeserb edgeferb asescerb. Attherb isferb aferb ontractioncerb, asferb ellwerb asferb aferb ordwerb erewherb etherb onlyferb owelverb isferb yferb. Eatnerb, allferb attherb orkswerb!"
|
||||
///);
|
||||
///assert_eq!(translate_ferb("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"),
|
||||
/// "Atwherb ifferb aferb ordwerb asherb onerb owelsverb, ikelerb istherb: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZerb"
|
||||
///);
|
||||
///assert_eq!(translate_ferb("Cool, so the heuristics make pretty good guesses with what they're fed!"),
|
||||
/// "Oolcerb, oserb etherb euristicsherb akemerb ettyprerb oodgerb uessesgerb ithwerb atwherb eytherb're edferb!"
|
||||
///);
|
||||
///```
|
||||
pub fn translate_ferb(english: &str) -> String { |
||||
return translate_with_style(english, "erb", "ferb"); |
||||
} |
||||
|
||||
///Translates a multi-word string (including punctuation) into a custom-styled play language!
|
||||
///
|
||||
///Pass the string you wish to translate, the suffix you wish to have appended to most words, and the suffix
|
||||
///you wish to have appended in various special-cases (such as when a word is only one letter or starts with a vowel).
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_with_style;
|
||||
///
|
||||
///let suffix = "ancy";
|
||||
///let special_case_suffix = "fancy";
|
||||
///assert_eq!(translate_with_style("Hello world from the coolest Pig Latin translator!", suffix, special_case_suffix),
|
||||
/// "Ellohancy orldwancy omfrancy ethancy oolestcancy Igpancy Atinlancy anslatortrancy!"
|
||||
///);
|
||||
///assert_eq!(translate_with_style("This library can translate any English text. It can even handle multiple sentences!", suffix, special_case_suffix),
|
||||
/// "Isthancy ibrarylancy ancancy anslatetrancy anyfancy Englishfancy exttancy. Itfancy ancancy evenfancy andlehancy ultiplemancy entencessancy!"
|
||||
///);
|
||||
/////TODO
|
||||
/////NOTE that this function used in an standalone fashion is not currently tested, and is thus considered experimental
|
||||
///```
|
||||
pub fn translate_with_style(english: &str, suffix: &str, special_case_suffix: &str) -> String { |
||||
if english.is_empty() { |
||||
return "".to_string(); |
||||
} |
||||
|
||||
//TODO perhaps make this multithreaded?
|
||||
|
||||
let mut pig_latin_string: String = "".to_string(); |
||||
let mut current_word: String = "".to_string(); |
||||
let mut in_word: bool = false; |
||||
|
||||
for character in english.chars().peekable() { |
||||
if in_word { |
||||
if character.is_alphabetic() || (character == '\'') { |
||||
//Save the character to translate once the word ends; we also keep apostrophes so that translate_word_with_style can handle contractions
|
||||
current_word.push(character); |
||||
} else { |
||||
//The word ended, so translate the chararacters we've saved up until this point!
|
||||
in_word = false; |
||||
pig_latin_string.push_str(translate_word_with_style(current_word.as_str(), suffix, special_case_suffix).as_str()); |
||||
|
||||
//Append the symbol/whitespace we just got after the translated word
|
||||
pig_latin_string.push(character); |
||||
} |
||||
} else {//We are not currently in a word
|
||||
if character.is_alphabetic() { |
||||
//If we see a letter, we are in a word, so save the character for now so we can translate the word later
|
||||
in_word = true; |
||||
current_word = character.to_string(); |
||||
} else { |
||||
//Otherwise copy symbols and whitespace as-is
|
||||
pig_latin_string.push(character); |
||||
} |
||||
} |
||||
} |
||||
//If we ended on a word, we translate it and push it to the end of the string
|
||||
if in_word { |
||||
pig_latin_string.push_str(translate_word_with_style(current_word.as_str(), suffix, special_case_suffix).as_str()); |
||||
} |
||||
|
||||
return pig_latin_string; |
||||
} |
||||
|
||||
/* Tests */ |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn test_translate_and_translate_way() { |
||||
assert_eq!(translate("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!"); |
||||
|
||||
assert_eq!(translate("This library can translate any English text. It can even handle multiple sentences!"), |
||||
"Isthay ibrarylay ancay anslatetray anyway Englishway exttay. Itway ancay evenway andlehay ultiplemay entencessay!" |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_and_translate_way_edgecases() { |
||||
assert_eq!(translate("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"), |
||||
"Etlay's ytray omesay edgeway asescay. Atthay isway away ontractioncay, asway ellway asway away ordway erewhay ethay onlyway owelvay isway yway. Eatnay, allway atthay orksway!" |
||||
); |
||||
|
||||
assert_eq!(translate("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"), |
||||
"Atwhay ifway away ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZay" |
||||
); |
||||
|
||||
assert_eq!(translate("Cool, so the heuristics make pretty good guesses with what they're fed!"), |
||||
"Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!" |
||||
); |
||||
|
||||
assert_eq!(translate("Hello-world"), "Ellohay-orldway"); |
||||
assert_eq!(translate("Hyphens-are-difficult-aren't-they?"), "Yphenshay-areway-ifficultday-arenway't-eythay?"); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_yay() { |
||||
assert_eq!(translate_yay("Hello world from the coolest Pig Latin translator!"), "Ellohay orldway omfray ethay oolestcay Igpay Atinlay anslatortray!"); |
||||
|
||||
assert_eq!(translate_yay("This library can translate any English text. It can even handle multiple sentences!"), |
||||
"Isthay ibrarylay ancay anslatetray anyyay Englishyay exttay. Ityay ancay evenyay andlehay ultiplemay entencessay!" |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_yay_edgecases() { |
||||
assert_eq!(translate_yay("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"), |
||||
"Etlay's ytray omesay edgeyay asescay. Atthay isyay ayay ontractioncay, asyay ellway asyay ayay ordway erewhay ethay onlyyay owelvay isyay yyay. Eatnay, allyay atthay orksway!" |
||||
); |
||||
|
||||
assert_eq!(translate_yay("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"), |
||||
"Atwhay ifyay ayay ordway ashay onay owelsvay, ikelay isthay: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZay" |
||||
); |
||||
|
||||
assert_eq!(translate_yay("Cool, so the heuristics make pretty good guesses with what they're fed!"), |
||||
"Oolcay, osay ethay euristicshay akemay ettypray oodgay uessesgay ithway atwhay eythay're edfay!" |
||||
); |
||||
|
||||
assert_eq!(translate_yay("Hello-world"), "Ellohay-orldway"); |
||||
assert_eq!(translate_yay("Hyphens-are-difficult-aren't-they?"), "Yphenshay-areyay-ifficultday-arenyay't-eythay?"); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_ferb() { |
||||
assert_eq!(translate_ferb("Hello world from the coolest Ferb Latin translator!"), "Elloherb orldwerb omfrerb etherb oolestcerb Erbferb Atinlerb anslatortrerb!"); |
||||
|
||||
assert_eq!(translate_ferb("This library can translate any English text. It can even handle multiple sentences!"), |
||||
"Istherb ibrarylerb ancerb anslatetrerb anyferb Englishferb extterb. Itferb ancerb evenferb andleherb ultiplemerb entencesserb!" |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_ferb_edgecases() { |
||||
assert_eq!(translate_ferb("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!"), |
||||
"Etlerb's ytrerb omeserb edgeferb asescerb. Attherb isferb aferb ontractioncerb, asferb ellwerb asferb aferb ordwerb erewherb etherb onlyferb owelverb isferb yferb. Eatnerb, allferb attherb orkswerb!" |
||||
); |
||||
|
||||
assert_eq!(translate_ferb("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"), |
||||
"Atwherb ifferb aferb ordwerb asherb onerb owelsverb, ikelerb istherb: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZerb" |
||||
); |
||||
|
||||
assert_eq!(translate_ferb("Cool, so the heuristics make pretty good guesses with what they're fed!"), |
||||
"Oolcerb, oserb etherb euristicsherb akemerb ettyprerb oodgerb uessesgerb ithwerb atwherb eytherb're edferb!" |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_with_style() { |
||||
let suffix_special_case_suffix_pairs = [("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")]; |
||||
|
||||
for pair in suffix_special_case_suffix_pairs { |
||||
let suffix = pair.0; |
||||
let special_case_suffix = pair.1; |
||||
|
||||
assert_eq!(translate_with_style("Hello world from the coolest Pig Latin translator!", suffix, special_case_suffix), |
||||
"Elloh".to_string() + suffix + " orldw" + suffix + " omfr" + suffix + " eth" + suffix + " oolestc" + suffix + " Igp" + suffix + " Atinl" + suffix + " anslatortr" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("This library can translate any English text. It can even handle multiple sentences!", suffix, special_case_suffix), |
||||
"Isth".to_string() + suffix + " ibraryl" + suffix + " anc" + suffix + " anslatetr" + suffix + " any" + special_case_suffix + " English" + special_case_suffix + " extt" + suffix + |
||||
". It" + special_case_suffix + " anc" + suffix + " even" + special_case_suffix + " andleh" + suffix + " ultiplem" + suffix + " entencess" + suffix + "!" |
||||
); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_with_style_edgecases() { |
||||
let suffix_special_case_suffix_pairs = [("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")]; |
||||
|
||||
for pair in suffix_special_case_suffix_pairs { |
||||
let suffix = pair.0; |
||||
let special_case_suffix = pair.1; |
||||
|
||||
assert_eq!(translate_with_style("Let's try some edge cases. That is a contraction, as well as a word where the only vowel is y. Neat, all that works!", suffix, special_case_suffix), |
||||
"Etl".to_string() + suffix + "'s ytr" + suffix + " omes" + suffix + " edge" + special_case_suffix + " asesc" + suffix + ". Atth" + suffix + " is" + special_case_suffix + " a" + |
||||
special_case_suffix + " ontractionc" + suffix + ", as" + special_case_suffix + " ellw" + suffix + " as" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix + |
||||
" erewh" + suffix + " eth" + suffix + " only" + special_case_suffix + " owelv" + suffix + " is" + special_case_suffix + " y" + special_case_suffix + ". Eatn" + suffix + ", all" + |
||||
special_case_suffix + " atth" + suffix + " orksw" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("What if a word has no vowels, like this: bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ", suffix, special_case_suffix), |
||||
"Atwh".to_string() + suffix + " if" + special_case_suffix + " a" + special_case_suffix + " ordw" + suffix + " ash" + suffix + " on" + suffix + " owelsv" + suffix + ", ikel" + suffix + " isth" + suffix + ": bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ" + suffix |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("Cool, so the heuristics make pretty good guesses with what they're fed!", suffix, special_case_suffix), |
||||
"Oolc".to_string() + suffix + ", os" + suffix + " eth" + suffix + " euristicsh" + suffix + " akem" + suffix + " ettypr" + suffix + " oodg" + suffix + " uessesg" + suffix + " ithw" + suffix + " atwh" + suffix + " eyth" + suffix + "'re edf" + suffix + "!" |
||||
); |
||||
|
||||
assert_eq!(translate_with_style("Hello-world", suffix, special_case_suffix), "Elloh".to_string() + suffix + "-orldw" + suffix); |
||||
//assert_eq!(translate_with_style("Hyphens-are-difficult-aren't-they?", suffix, special_case_suffix), "Yphenshay-areyay-ifficultday-arenyay't-eythay?");
|
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Benches */ |
||||
|
||||
#[cfg_attr(feature = "nightly-features", cfg(test))] |
||||
#[cfg(feature = "nightly-features")] |
||||
mod benches { |
||||
extern crate test; |
||||
use test::Bencher; |
||||
use super::*; |
||||
|
||||
#[bench] |
||||
fn translate_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate("A simple Rust library to translate from English to Pig Latin!"); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_yay_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_yay("A simple Rust library to translate from English to Pig Latin!"); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_ferb_project_description(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_ferb("A simple Rust library to translate from English to Pig Latin!"); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_yay_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_yay("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_ferb_lorem_ipsum(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_ferb("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); |
||||
}); |
||||
} |
||||
} |
@ -1,561 +0,0 @@ |
||||
/* translate_words.rs
|
||||
* Copyright (C) 2022 John Jekel |
||||
* See the LICENSE file at the root of the project for licensing info. |
||||
* |
||||
* Contains functions to translate individual words (used by translate_strings.rs) |
||||
* |
||||
*/ |
||||
|
||||
/* Imports */ |
||||
|
||||
use crate::helpers::{is_vowel, is_y}; |
||||
|
||||
/* Functions */ |
||||
|
||||
///Translates a single word or contraction string into Pig Latin!
|
||||
///
|
||||
///Can have leading and trailing punctuation or whitespace.
|
||||
///It generally does a pretty good job with valid english words and contractions,
|
||||
///and leaves symbols and spaces mostly unchanged.
|
||||
///
|
||||
///Uses the default suffix and special_case_suffix, "ay" and "way" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///Equivalent to [`translate_word_way()`].
|
||||
///
|
||||
///This is a helper function used by the [`translate()`](crate::translate) family of functions, but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_word;
|
||||
///
|
||||
///assert_eq!(translate_word("Hello"), "Ellohay");
|
||||
///assert_eq!(translate_word("World!"), "Orldway!");
|
||||
///
|
||||
///assert_eq!(translate_word("This"), "Isthay");
|
||||
///assert_eq!(translate_word("is"), "isway");
|
||||
///assert_eq!(translate_word("a"), "away");
|
||||
///assert_eq!(translate_word("test"), "esttay");
|
||||
///assert_eq!(translate_word("of"), "ofway");
|
||||
///assert_eq!(translate_word("the"), "ethay");
|
||||
///assert_eq!(translate_word("function"), "unctionfay");
|
||||
///assert_eq!(translate_word("translate_"), "anslatetray_");
|
||||
///assert_eq!(translate_word("word."), "ordway.");
|
||||
///
|
||||
///assert_eq!(translate_word("I"), "Iway");
|
||||
///assert_eq!(translate_word("Love"), "Ovelay");
|
||||
///assert_eq!(translate_word("Pig"), "Igpay");
|
||||
///assert_eq!(translate_word("Latin!"), "Atinlay!");
|
||||
///
|
||||
///assert_eq!(translate_word("You"), "Ouyay");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word("should"), "ouldshay");
|
||||
///assert_eq!(translate_word("try"), "ytray");//Y is a vowel here
|
||||
///assert_eq!(translate_word("yougurt,"), "ougurtyay,");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word("it's"), "itway's");//Contraction
|
||||
///assert_eq!(translate_word("quite"), "uiteqay");//Awful to pronounce, but correct
|
||||
///assert_eq!(translate_word("nice!"), "icenay!");
|
||||
///
|
||||
///assert_eq!(translate_word(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
///assert_eq!(translate_word(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordway|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
///assert_eq!(translate_word("12345678"), "12345678");//A number
|
||||
///assert_eq!(translate_word("100 pizzas"), "100 izzaspay");//A number before a word
|
||||
///assert_eq!(translate_word("over 9000"), "overway 9000");//A number after a word
|
||||
///```
|
||||
pub fn translate_word(english_word: &str) -> String { |
||||
return translate_word_way(english_word); |
||||
} |
||||
|
||||
///Translates a single word or contraction string into Pig Latin (way-style)!
|
||||
///
|
||||
///Can have leading and trailing punctuation or whitespace.
|
||||
///It generally does a pretty good job with valid english words and contractions,
|
||||
///and leaves symbols and spaces mostly unchanged.
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "way" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///This is a helper function used by the [`translate()`](crate::translate) family of functions, but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_word_way;
|
||||
///
|
||||
///assert_eq!(translate_word_way("Hello"), "Ellohay");
|
||||
///assert_eq!(translate_word_way("World!"), "Orldway!");
|
||||
///
|
||||
///assert_eq!(translate_word_way("This"), "Isthay");
|
||||
///assert_eq!(translate_word_way("is"), "isway");
|
||||
///assert_eq!(translate_word_way("a"), "away");
|
||||
///assert_eq!(translate_word_way("test"), "esttay");
|
||||
///assert_eq!(translate_word_way("of"), "ofway");
|
||||
///assert_eq!(translate_word_way("the"), "ethay");
|
||||
///assert_eq!(translate_word_way("function"), "unctionfay");
|
||||
///assert_eq!(translate_word_way("translate_"), "anslatetray_");
|
||||
///assert_eq!(translate_word_way("word."), "ordway.");
|
||||
///
|
||||
///assert_eq!(translate_word_way("I"), "Iway");
|
||||
///assert_eq!(translate_word_way("Love"), "Ovelay");
|
||||
///assert_eq!(translate_word_way("Pig"), "Igpay");
|
||||
///assert_eq!(translate_word_way("Latin!"), "Atinlay!");
|
||||
///
|
||||
///assert_eq!(translate_word_way("You"), "Ouyay");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_way("should"), "ouldshay");
|
||||
///assert_eq!(translate_word_way("try"), "ytray");//Y is a vowel here
|
||||
///assert_eq!(translate_word_way("yougurt,"), "ougurtyay,");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_way("it's"), "itway's");//Contraction
|
||||
///assert_eq!(translate_word_way("quite"), "uiteqay");//Awful to pronounce, but correct
|
||||
///assert_eq!(translate_word_way("nice!"), "icenay!");
|
||||
///
|
||||
///assert_eq!(translate_word_way(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
///assert_eq!(translate_word_way(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordway|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
///assert_eq!(translate_word_way("12345678"), "12345678");//A number
|
||||
///assert_eq!(translate_word_way("100 pizzas"), "100 izzaspay");//A number before a word
|
||||
///assert_eq!(translate_word_way("over 9000"), "overway 9000");//A number after a word
|
||||
///```
|
||||
pub fn translate_word_way(english_word: &str) -> String { |
||||
return translate_word_with_style(english_word, "ay", "way"); |
||||
} |
||||
|
||||
///Translates a single word or contraction string into Pig Latin (yay-style)!
|
||||
///
|
||||
///Can have leading and trailing punctuation or whitespace.
|
||||
///It generally does a pretty good job with valid english words and contractions,
|
||||
///and leaves symbols and spaces mostly unchanged.
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "ay" and "way" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///This is a helper function used by the [`translate()`](crate::translate) family of functions, but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_word_yay;
|
||||
///
|
||||
///assert_eq!(translate_word_yay("Hello"), "Ellohay");
|
||||
///assert_eq!(translate_word_yay("World!"), "Orldway!");
|
||||
///
|
||||
///assert_eq!(translate_word_yay("This"), "Isthay");
|
||||
///assert_eq!(translate_word_yay("is"), "isyay");
|
||||
///assert_eq!(translate_word_yay("a"), "ayay");
|
||||
///assert_eq!(translate_word_yay("test"), "esttay");
|
||||
///assert_eq!(translate_word_yay("of"), "ofyay");
|
||||
///assert_eq!(translate_word_yay("the"), "ethay");
|
||||
///assert_eq!(translate_word_yay("function"), "unctionfay");
|
||||
///assert_eq!(translate_word_yay("translate_"), "anslatetray_");
|
||||
///assert_eq!(translate_word_yay("word."), "ordway.");
|
||||
///
|
||||
///assert_eq!(translate_word_yay("I"), "Iyay");
|
||||
///assert_eq!(translate_word_yay("Love"), "Ovelay");
|
||||
///assert_eq!(translate_word_yay("Pig"), "Igpay");
|
||||
///assert_eq!(translate_word_yay("Latin!"), "Atinlay!");
|
||||
///
|
||||
///assert_eq!(translate_word_yay("You"), "Ouyay");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_yay("should"), "ouldshay");
|
||||
///assert_eq!(translate_word_yay("try"), "ytray");//Y is a vowel here
|
||||
///assert_eq!(translate_word_yay("yougurt,"), "ougurtyay,");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_yay("it's"), "ityay's");//Contraction
|
||||
///assert_eq!(translate_word_yay("quite"), "uiteqay");//Awful to pronounce, but correct
|
||||
///assert_eq!(translate_word_yay("nice!"), "icenay!");
|
||||
///
|
||||
///assert_eq!(translate_word_yay(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
///assert_eq!(translate_word_yay(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordway|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
///assert_eq!(translate_word_yay("12345678"), "12345678");//A number
|
||||
///assert_eq!(translate_word_yay("100 pizzas"), "100 izzaspay");//A number before a word
|
||||
///assert_eq!(translate_word_yay("over 9000"), "overyay 9000");//A number after a word
|
||||
///```
|
||||
pub fn translate_word_yay(english_word: &str) -> String { |
||||
return translate_word_with_style(english_word, "ay", "yay"); |
||||
} |
||||
|
||||
///Translates a single word or contraction string into Ferb Latin!
|
||||
///
|
||||
///Can have leading and trailing punctuation or whitespace.
|
||||
///It generally does a pretty good job with valid english words and contractions,
|
||||
///and leaves symbols and spaces mostly unchanged.
|
||||
///
|
||||
///Uses the suffix and special_case_suffix "erb" and "ferb" respectively when calling [`translate_word_with_style()`].
|
||||
///
|
||||
///This is a helper function used by the [`translate()`](crate::translate) family of functions, but
|
||||
///it is publically exposed as potential users may find this useful.
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_word_ferb;
|
||||
///
|
||||
///assert_eq!(translate_word_ferb("Hello"), "Elloherb");
|
||||
///assert_eq!(translate_word_ferb("World!"), "Orldwerb!");
|
||||
///
|
||||
///assert_eq!(translate_word_ferb("This"), "Istherb");
|
||||
///assert_eq!(translate_word_ferb("is"), "isferb");
|
||||
///assert_eq!(translate_word_ferb("a"), "aferb");
|
||||
///assert_eq!(translate_word_ferb("test"), "estterb");
|
||||
///assert_eq!(translate_word_ferb("of"), "offerb");
|
||||
///assert_eq!(translate_word_ferb("the"), "etherb");
|
||||
///assert_eq!(translate_word_ferb("function"), "unctionferb");
|
||||
///assert_eq!(translate_word_ferb("translate_"), "anslatetrerb_");
|
||||
///assert_eq!(translate_word_ferb("word."), "ordwerb.");
|
||||
///
|
||||
///assert_eq!(translate_word_ferb("I"), "Iferb");
|
||||
///assert_eq!(translate_word_ferb("Love"), "Ovelerb");
|
||||
///assert_eq!(translate_word_ferb("Pig"), "Igperb");
|
||||
///assert_eq!(translate_word_ferb("Latin!"), "Atinlerb!");
|
||||
///
|
||||
///assert_eq!(translate_word_ferb("You"), "Ouyerb");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_ferb("should"), "ouldsherb");
|
||||
///assert_eq!(translate_word_ferb("try"), "ytrerb");//Y is a vowel here
|
||||
///assert_eq!(translate_word_ferb("yougurt,"), "ougurtyerb,");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_ferb("it's"), "itferb's");//Contraction
|
||||
///assert_eq!(translate_word_ferb("quite"), "uiteqerb");//Awful to pronounce, but correct
|
||||
///assert_eq!(translate_word_ferb("nice!"), "icenerb!");
|
||||
///
|
||||
///assert_eq!(translate_word_ferb(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
///assert_eq!(translate_word_ferb(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordwerb|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
///assert_eq!(translate_word_ferb("12345678"), "12345678");//A number
|
||||
///assert_eq!(translate_word_ferb("100 pizzas"), "100 izzasperb");//A number before a word
|
||||
///assert_eq!(translate_word_ferb("over 9000"), "overferb 9000");//A number after a word
|
||||
///```
|
||||
pub fn translate_word_ferb(english_word: &str) -> String { |
||||
return translate_word_with_style(english_word, "erb", "ferb"); |
||||
} |
||||
|
||||
///Translates a single word or contraction string into a custom-styled play language!
|
||||
///
|
||||
///Pass the word you wish to translate, the suffix you wish to have appended to most words, and the suffix
|
||||
///you wish to have appended in various special-cases (such as when a word is only one letter or starts with a vowel).
|
||||
///
|
||||
///# Examples
|
||||
///
|
||||
///```
|
||||
///use anslatortray::translate_word_with_style;
|
||||
///
|
||||
///let suffix = "ancy";
|
||||
///let special_case_suffix = "fancy";
|
||||
///
|
||||
///assert_eq!(translate_word_with_style("Hello", suffix, special_case_suffix), "Ellohancy");
|
||||
///assert_eq!(translate_word_with_style("World!", suffix, special_case_suffix), "Orldwancy!");
|
||||
///
|
||||
///assert_eq!(translate_word_with_style("This", suffix, special_case_suffix), "Isthancy");
|
||||
///assert_eq!(translate_word_with_style("is", suffix, special_case_suffix), "isfancy");
|
||||
///assert_eq!(translate_word_with_style("a", suffix, special_case_suffix), "afancy");
|
||||
///assert_eq!(translate_word_with_style("test", suffix, special_case_suffix), "esttancy");
|
||||
///assert_eq!(translate_word_with_style("of", suffix, special_case_suffix), "offancy");
|
||||
///assert_eq!(translate_word_with_style("the", suffix, special_case_suffix), "ethancy");
|
||||
///assert_eq!(translate_word_with_style("function", suffix, special_case_suffix), "unctionfancy");
|
||||
///assert_eq!(translate_word_with_style("translate_", suffix, special_case_suffix), "anslatetrancy_");
|
||||
///assert_eq!(translate_word_with_style("word.", suffix, special_case_suffix), "ordwancy.");
|
||||
///
|
||||
///assert_eq!(translate_word_with_style("I", suffix, special_case_suffix), "Ifancy");
|
||||
///assert_eq!(translate_word_with_style("Love", suffix, special_case_suffix), "Ovelancy");
|
||||
///assert_eq!(translate_word_with_style("Pig", suffix, special_case_suffix), "Igpancy");
|
||||
///assert_eq!(translate_word_with_style("Latin!", suffix, special_case_suffix), "Atinlancy!");
|
||||
///
|
||||
///assert_eq!(translate_word_with_style("You", suffix, special_case_suffix), "Ouyancy");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_with_style("should", suffix, special_case_suffix), "ouldshancy");
|
||||
///assert_eq!(translate_word_with_style("try", suffix, special_case_suffix), "ytrancy");//Y is a vowel here
|
||||
///assert_eq!(translate_word_with_style("yougurt,", suffix, special_case_suffix), "ougurtyancy,");//Y isn't a vowel here
|
||||
///assert_eq!(translate_word_with_style("it's", suffix, special_case_suffix), "itfancy's");//Contraction
|
||||
///assert_eq!(translate_word_with_style("quite", suffix, special_case_suffix), "uiteqancy");//Awful to pronounce, but correct
|
||||
///assert_eq!(translate_word_with_style("nice!", suffix, special_case_suffix), "icenancy!");
|
||||
///
|
||||
///assert_eq!(translate_word_with_style(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n", suffix, special_case_suffix), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
///assert_eq!(translate_word_with_style(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n", suffix, special_case_suffix), " !@#$%^&*()_+={}ordwancy|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
///assert_eq!(translate_word_with_style("12345678", suffix, special_case_suffix), "12345678");//A number
|
||||
///assert_eq!(translate_word_with_style("100 pizzas", suffix, special_case_suffix), "100 izzaspancy");//A number before a word
|
||||
///assert_eq!(translate_word_with_style("over 9000", suffix, special_case_suffix), "overfancy 9000");//A number after a word
|
||||
///```
|
||||
pub fn translate_word_with_style(english_word: &str, suffix: &str, special_case_suffix: &str) -> String { |
||||
if english_word.is_empty() { |
||||
return "".to_string(); |
||||
} |
||||
|
||||
let mut pig_latin_word: String = "".to_string(); |
||||
let mut iterator = english_word.chars().peekable(); |
||||
|
||||
//Copy leading symbols/whitespace until the first letter
|
||||
let first_letter: char; |
||||
loop { |
||||
match iterator.next() { |
||||
None => { return english_word.to_string(); },//There are only symbols/whitespace in the word
|
||||
Some(character) => { |
||||
if character.is_alphabetic() { |
||||
first_letter = character;//We found the first character of the word/contraction
|
||||
break; |
||||
} else { |
||||
pig_latin_word.push(character);//Copy whitespace/symbol
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
//TODO what if the word is all uppercase?
|
||||
|
||||
//As a herustic, we consider Y to be a vowel when it is not at the start of the word
|
||||
//However, if any word is only one letter long, this takes priority and the word is treated like a vowel
|
||||
let first_letter_was_vowel: bool = { |
||||
is_vowel(first_letter).unwrap()//Not including y
|
||||
|| if let Some(character) = iterator.peek() { !character.is_alphabetic() } else { true }//Non-alphabetic character after the first letter, or the word ends after the first letter
|
||||
}; |
||||
let mut starting_consonants: String = "".to_string(); |
||||
|
||||
if first_letter_was_vowel { |
||||
pig_latin_word.push(first_letter); |
||||
} else { |
||||
let first_char_was_upper = first_letter.is_ascii_uppercase(); |
||||
starting_consonants.push(first_letter.to_ascii_lowercase()); |
||||
|
||||
//Grab all of the starting consonants, and push the first vowel we enounter to pig_latin_word
|
||||
loop { |
||||
match iterator.next() { |
||||
None => { break; },//The word has no vowels, but it is a herustic to pass it on so that ex. the acroynm binary code decimal or bcd becomes bcdway, etc.
|
||||
Some(character) => { |
||||
if character.is_alphabetic() { |
||||
if is_vowel(character).unwrap() || is_y(character).unwrap() {//As a herustic, we consider Y to be a vowel when it is not at the start of the word
|
||||
//The vowel is the first letter of the word; we want it match the capitalization of the first letter of the original word
|
||||
if first_char_was_upper { |
||||
pig_latin_word.push(character.to_ascii_uppercase()); |
||||
} else { |
||||
pig_latin_word.push(character.to_ascii_lowercase()); |
||||
} |
||||
break; |
||||
} else { |
||||
starting_consonants.push(character); |
||||
} |
||||
} else {//The word ended without vowels or we met an apostrophe
|
||||
break;//It is a herustic to pass it on so that ex. the letter y becomes yway, the word a becomes away, etc.
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
//Copy all of the remaining letters up to the end of the word or up until we enounter the ' as part of a contraction
|
||||
let trailing_character: Option<char>; |
||||
loop { |
||||
match iterator.next() { |
||||
None => { |
||||
trailing_character = None; |
||||
break; |
||||
},//End of the word
|
||||
Some(character) => { |
||||
if character.is_alphabetic() { |
||||
pig_latin_word.push(character); |
||||
} else { |
||||
trailing_character = Some(character); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
//Copy starting consonants and add the suffix, or add the special_case_suffix depending on the circumstances
|
||||
if first_letter_was_vowel { |
||||
pig_latin_word.push_str(special_case_suffix); |
||||
} else { |
||||
pig_latin_word.push_str(&starting_consonants); |
||||
pig_latin_word.push_str(suffix); |
||||
} |
||||
|
||||
//Re-add the trailing character we "accidentally" took in the previous loop (if we do in fact have one)
|
||||
if let Some(character) = trailing_character { |
||||
pig_latin_word.push(character); |
||||
} |
||||
|
||||
//Copy any remaining characters as-is
|
||||
loop { |
||||
match iterator.next() { |
||||
None => { break; },//End of the word
|
||||
Some(character) => { pig_latin_word.push(character); }, |
||||
} |
||||
} |
||||
|
||||
return pig_latin_word; |
||||
} |
||||
|
||||
/* Tests */ |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn test_translate_word_and_translate_word_way() { |
||||
assert_eq!(translate_word("Hello"), "Ellohay"); |
||||
assert_eq!(translate_word("World!"), "Orldway!"); |
||||
|
||||
assert_eq!(translate_word("This"), "Isthay"); |
||||
assert_eq!(translate_word("is"), "isway"); |
||||
assert_eq!(translate_word("a"), "away"); |
||||
assert_eq!(translate_word("test"), "esttay"); |
||||
assert_eq!(translate_word("of"), "ofway"); |
||||
assert_eq!(translate_word("the"), "ethay"); |
||||
assert_eq!(translate_word("function"), "unctionfay"); |
||||
assert_eq!(translate_word("translate_"), "anslatetray_"); |
||||
assert_eq!(translate_word("word."), "ordway."); |
||||
|
||||
assert_eq!(translate_word("I"), "Iway"); |
||||
assert_eq!(translate_word("Love"), "Ovelay"); |
||||
assert_eq!(translate_word("Pig"), "Igpay"); |
||||
assert_eq!(translate_word("Latin!"), "Atinlay!"); |
||||
|
||||
assert_eq!(translate_word("You"), "Ouyay");//Y isn't a vowel here
|
||||
assert_eq!(translate_word("should"), "ouldshay"); |
||||
assert_eq!(translate_word("try"), "ytray");//Y is a vowel here
|
||||
assert_eq!(translate_word("yougurt,"), "ougurtyay,");//Y isn't a vowel here
|
||||
assert_eq!(translate_word("it's"), "itway's");//Contraction
|
||||
assert_eq!(translate_word("quite"), "uiteqay");//Awful to pronounce, but correct
|
||||
assert_eq!(translate_word("nice!"), "icenay!"); |
||||
|
||||
assert_eq!(translate_word(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
assert_eq!(translate_word(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordway|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
assert_eq!(translate_word("12345678"), "12345678");//A number
|
||||
assert_eq!(translate_word("100 pizzas"), "100 izzaspay");//A number before a word
|
||||
assert_eq!(translate_word("over 9000"), "overway 9000");//A number after a word
|
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_word_yay() { |
||||
assert_eq!(translate_word_yay("Hello"), "Ellohay"); |
||||
assert_eq!(translate_word_yay("World!"), "Orldway!"); |
||||
|
||||
assert_eq!(translate_word_yay("This"), "Isthay"); |
||||
assert_eq!(translate_word_yay("is"), "isyay"); |
||||
assert_eq!(translate_word_yay("a"), "ayay"); |
||||
assert_eq!(translate_word_yay("test"), "esttay"); |
||||
assert_eq!(translate_word_yay("of"), "ofyay"); |
||||
assert_eq!(translate_word_yay("the"), "ethay"); |
||||
assert_eq!(translate_word_yay("function"), "unctionfay"); |
||||
assert_eq!(translate_word_yay("translate_"), "anslatetray_"); |
||||
assert_eq!(translate_word_yay("word."), "ordway."); |
||||
|
||||
assert_eq!(translate_word_yay("I"), "Iyay"); |
||||
assert_eq!(translate_word_yay("Love"), "Ovelay"); |
||||
assert_eq!(translate_word_yay("Pig"), "Igpay"); |
||||
assert_eq!(translate_word_yay("Latin!"), "Atinlay!"); |
||||
|
||||
assert_eq!(translate_word_yay("You"), "Ouyay");//Y isn't a vowel here
|
||||
assert_eq!(translate_word_yay("should"), "ouldshay"); |
||||
assert_eq!(translate_word_yay("try"), "ytray");//Y is a vowel here
|
||||
assert_eq!(translate_word_yay("yougurt,"), "ougurtyay,");//Y isn't a vowel here
|
||||
assert_eq!(translate_word_yay("it's"), "ityay's");//Contraction
|
||||
assert_eq!(translate_word_yay("quite"), "uiteqay");//Awful to pronounce, but correct
|
||||
assert_eq!(translate_word_yay("nice!"), "icenay!"); |
||||
|
||||
assert_eq!(translate_word_yay(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
assert_eq!(translate_word_yay(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordway|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
assert_eq!(translate_word_yay("12345678"), "12345678");//A number
|
||||
assert_eq!(translate_word_yay("100 pizzas"), "100 izzaspay");//A number before a word
|
||||
assert_eq!(translate_word_yay("over 9000"), "overyay 9000");//A number after a word
|
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_word_ferb() { |
||||
assert_eq!(translate_word_ferb("Hello"), "Elloherb"); |
||||
assert_eq!(translate_word_ferb("World!"), "Orldwerb!"); |
||||
|
||||
assert_eq!(translate_word_ferb("This"), "Istherb"); |
||||
assert_eq!(translate_word_ferb("is"), "isferb"); |
||||
assert_eq!(translate_word_ferb("a"), "aferb"); |
||||
assert_eq!(translate_word_ferb("test"), "estterb"); |
||||
assert_eq!(translate_word_ferb("of"), "offerb"); |
||||
assert_eq!(translate_word_ferb("the"), "etherb"); |
||||
assert_eq!(translate_word_ferb("function"), "unctionferb"); |
||||
assert_eq!(translate_word_ferb("translate_"), "anslatetrerb_"); |
||||
assert_eq!(translate_word_ferb("word."), "ordwerb."); |
||||
|
||||
assert_eq!(translate_word_ferb("I"), "Iferb"); |
||||
assert_eq!(translate_word_ferb("Love"), "Ovelerb"); |
||||
assert_eq!(translate_word_ferb("Pig"), "Igperb"); |
||||
assert_eq!(translate_word_ferb("Latin!"), "Atinlerb!"); |
||||
|
||||
assert_eq!(translate_word_ferb("You"), "Ouyerb");//Y isn't a vowel here
|
||||
assert_eq!(translate_word_ferb("should"), "ouldsherb"); |
||||
assert_eq!(translate_word_ferb("try"), "ytrerb");//Y is a vowel here
|
||||
assert_eq!(translate_word_ferb("yougurt,"), "ougurtyerb,");//Y isn't a vowel here
|
||||
assert_eq!(translate_word_ferb("it's"), "itferb's");//Contraction
|
||||
assert_eq!(translate_word_ferb("quite"), "uiteqerb");//Awful to pronounce, but correct
|
||||
assert_eq!(translate_word_ferb("nice!"), "icenerb!"); |
||||
|
||||
assert_eq!(translate_word_ferb(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
assert_eq!(translate_word_ferb(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n"), " !@#$%^&*()_+={}ordwerb|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
assert_eq!(translate_word_ferb("12345678"), "12345678");//A number
|
||||
assert_eq!(translate_word_ferb("100 pizzas"), "100 izzasperb");//A number before a word
|
||||
assert_eq!(translate_word_ferb("over 9000"), "overferb 9000");//A number after a word
|
||||
} |
||||
|
||||
#[test] |
||||
fn test_translate_word_with_style() { |
||||
let suffix_special_case_suffix_pairs = [("ancy", "fancy"), ("orange", "porange"), ("anana", "banana"), ("atin", "latin"), ("ust", "rust")]; |
||||
|
||||
for pair in suffix_special_case_suffix_pairs { |
||||
let suffix = pair.0; |
||||
let special_case_suffix = pair.1; |
||||
|
||||
assert_eq!(translate_word_with_style("Hello", suffix, special_case_suffix), "Elloh".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("World!", suffix, special_case_suffix), "Orldw".to_string() + suffix + "!"); |
||||
|
||||
assert_eq!(translate_word_with_style("This", suffix, special_case_suffix), "Isth".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("is", suffix, special_case_suffix), "is".to_string() + special_case_suffix); |
||||
assert_eq!(translate_word_with_style("a", suffix, special_case_suffix), "a".to_string() + special_case_suffix); |
||||
assert_eq!(translate_word_with_style("test", suffix, special_case_suffix), "estt".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("of", suffix, special_case_suffix), "of".to_string() + special_case_suffix); |
||||
assert_eq!(translate_word_with_style("the", suffix, special_case_suffix), "eth".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("function", suffix, special_case_suffix), "unctionf".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("translate_", suffix, special_case_suffix), "anslatetr".to_string() + suffix + "_"); |
||||
assert_eq!(translate_word_with_style("word.", suffix, special_case_suffix), "ordw".to_string() + suffix + "."); |
||||
|
||||
assert_eq!(translate_word_with_style("I", suffix, special_case_suffix), "I".to_string() + special_case_suffix); |
||||
assert_eq!(translate_word_with_style("Love", suffix, special_case_suffix), "Ovel".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("Pig", suffix, special_case_suffix), "Igp".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("Latin!", suffix, special_case_suffix), "Atinl".to_string() + suffix + "!"); |
||||
|
||||
assert_eq!(translate_word_with_style("You", suffix, special_case_suffix), "Ouy".to_string() + suffix);//Y isn't a vowel here
|
||||
assert_eq!(translate_word_with_style("should", suffix, special_case_suffix), "ouldsh".to_string() + suffix); |
||||
assert_eq!(translate_word_with_style("try", suffix, special_case_suffix), "ytr".to_string() + suffix);//Y is a vowel here
|
||||
assert_eq!(translate_word_with_style("yougurt,", suffix, special_case_suffix), "ougurty".to_string() + suffix + ",");//Y isn't a vowel here
|
||||
assert_eq!(translate_word_with_style("it's", suffix, special_case_suffix), "it".to_string() + special_case_suffix + "'s");//Contraction
|
||||
assert_eq!(translate_word_with_style("quite", suffix, special_case_suffix), "uiteq".to_string() + suffix);//Awful to pronounce, but correct
|
||||
assert_eq!(translate_word_with_style("nice!", suffix, special_case_suffix), "icen".to_string() + suffix + "!"); |
||||
|
||||
assert_eq!(translate_word_with_style(" !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n", suffix, special_case_suffix), " !@#$%^&*()_+={}|\":>?~`\\][';/.,\t\n");//Lots of symbols
|
||||
assert_eq!(translate_word_with_style(" !@#$%^&*()_+={}word|\":>?~`\\][';/.,\t\n", suffix, special_case_suffix), " !@#$%^&*()_+={}ordw".to_string() + suffix + "|\":>?~`\\][';/.,\t\n");//Symbols around a word
|
||||
assert_eq!(translate_word_with_style("12345678", suffix, special_case_suffix), "12345678");//A number
|
||||
assert_eq!(translate_word_with_style("100 pizzas", suffix, special_case_suffix), "100 izzasp".to_string() + suffix);//A number before a word
|
||||
assert_eq!(translate_word_with_style("over 9000", suffix, special_case_suffix), "over".to_string() + special_case_suffix + " 9000");//A number after a word
|
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Benches */ |
||||
|
||||
#[cfg_attr(feature = "nightly-features", cfg(test))] |
||||
#[cfg(feature = "nightly-features")] |
||||
mod benches { |
||||
extern crate test; |
||||
use test::Bencher; |
||||
use super::*; |
||||
|
||||
#[bench] |
||||
fn translate_word_the_word_translator(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_word("translator"); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_word_yay_the_word_translator(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_word_yay("translator"); |
||||
}); |
||||
} |
||||
|
||||
#[bench] |
||||
fn translate_word_ferb_the_word_translator(b: &mut Bencher) { |
||||
b.iter(|| -> String { |
||||
return translate_word_ferb("translator"); |
||||
}); |
||||
} |
||||
} |
Loading…
Reference in new issue