Last active
August 26, 2019 02:32
-
-
Save glinesbdev/0cb0d9b8a8c039036855f405c6535112 to your computer and use it in GitHub Desktop.
Basic Pig Latin Translator - Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{ | |
fs::File, | |
io::{prelude::*, BufReader}, | |
}; | |
mod pig_latin; | |
fn main() { | |
let file = File::open("text.txt").expect("Can't open file."); | |
let file_size = file.metadata().unwrap().len(); | |
let file_buffer = BufReader::with_capacity(file_size as usize, file); | |
let mut translated = File::create("translated.txt").expect("Cannot create translation file."); | |
for line in file_buffer.lines() { | |
for word in line.unwrap().split_whitespace() { | |
translated | |
// keep formatting with `format!` so it doesn't look like garbarge | |
.write_all(&format!("{} ", pig_latin::translate(word)).as_bytes()) | |
.ok(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct WordParts { | |
first_lower: char, | |
is_vowel: bool, | |
rest_of_word: String, | |
punctuation: char, | |
} | |
pub fn translate(word: &str) -> String { | |
let mut word_parts = build_word_parts(word); | |
word_parts.rest_of_word = word_parts.rest_of_word.replace(word_parts.punctuation, ""); | |
print(&word_parts) | |
} | |
fn build_word_parts(word: &str) -> WordParts { | |
let ftl = first_to_lower(word); | |
let rest = word[1..].to_string(); | |
let index = punctuation_index(&rest); | |
WordParts { | |
first_lower: ftl, | |
is_vowel: is_vowel(&ftl), | |
punctuation: punctuation_char(index, &rest), | |
rest_of_word: rest, | |
} | |
} | |
fn is_vowel(subject: &char) -> bool { | |
['a', 'e', 'i', 'o', 'u'].contains(subject) | |
} | |
fn punctuation_index(subject: &str) -> usize { | |
match subject.find(|x: char| x.is_ascii_punctuation()) { | |
Some(index) => index, | |
None => usize::max_value(), // Really high number that can't be possible index | |
} | |
} | |
fn punctuation_char(index: usize, subject: &str) -> char { | |
if index != usize::max_value() { | |
match subject.chars().nth(index) { | |
Some(character) => character, | |
None => ' ', // Return space to be trimmed | |
} | |
} else { | |
' ' // Return space to be trimmed | |
} | |
} | |
fn first_to_lower(word: &str) -> char { | |
String::from(&word[..1]) | |
.to_lowercase() | |
.chars() | |
.next() | |
.unwrap() | |
} | |
fn print(parts: &WordParts) -> String { | |
if parts.is_vowel { | |
String::from( | |
format!( | |
"{}{}-hay{}", | |
parts.first_lower, parts.rest_of_word, parts.punctuation | |
) | |
.trim(), | |
) | |
} else { | |
String::from( | |
format!( | |
"{}-{}ay{}", | |
parts.rest_of_word, parts.first_lower, parts.punctuation | |
) | |
.trim(), | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Both versions convey a topic; it’s pretty easy to predict that the paragraph will be about epidemiological evidence, but only the second version establishes an argumentative point and puts it in context. The paragraph doesn’t just describe the epidemiological evidence; it shows how epidemiology is telling the same story as etiology. Similarly, while Version A doesn’t relate to anything in particular, Version B immediately suggests that the prior paragraph addresses the biological pathway (i.e. etiology) of a disease and that the new paragraph will bolster the emerging hypothesis with a different kind of evidence. As a reader, it’s easy to keep track of how the paragraph about cells and chemicals and such relates to the paragraph about populations in different places. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment