-
-
Save anonymous/275e3ae52357e2a5c338 to your computer and use it in GitHub Desktop.
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
# Student: Thomas-0725; | |
# Status: Complete; could use some revision for clarity; | |
# Possible improvements: correctly preserve capitalization; elegantly deal with punctuation; treat 'y' as a vowel when appropriate (simple rule: if 'y' follows a consonant, it is a vowel.) | |
def translate(phrase) | |
phrase.split.map {|word| pig_latin(word)}.join(" ") | |
end | |
def pig_latin(word) | |
postfix="ay" | |
split_position = PigLatinConsonants.first_non_consonant_position(word) | |
split_position = 0 if split_position.nil? | |
new_first_half = word[split_position..-1] | |
new_second_half = word[0, split_position] | |
"#{new_first_half}#{new_second_half}#{postfix}" | |
end | |
class PigLatinConsonants | |
@@phonemes = %w[b c d f g h j k l m n p q r s t v w x y z qu] | |
@@phonemes.sort_by {|phoneme| phoneme.length} | |
@@phonemes = @@phonemes.reverse #Puts larger phonemes at front so that they are checked first. | |
# I would like to do away with masked_word and try to find a slightly less awkward way to do this. | |
def PigLatinConsonants.first_non_consonant_position(word) | |
#Don't modify existing word. | |
masked_word = String.new(word.downcase) | |
#Replace all consonants with masking character. | |
@@phonemes.each {|phoneme| masked_word.gsub!(phoneme, "_"*phoneme.length)} | |
#Find first occurrence of unmasked character. | |
masked_word.index(/[^_]/) | |
end | |
end | |
p translate("this is a test") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment