Subscribe to Sergio Tapia - Lost in the GC.        RSS Feed
-----

Writing a pig latin converter in Ruby 1.8.7.

Icon 1 Comments
This morning I was kind of bored while waiting for the kids to wake up so I decided to whip up something in Ruby.

Since I've completely switched to Linux on my desktop, I was suprised how everything seemed to work a bit faster and generally get out of my way.

sudo apt-get install ruby


Yep, that installed Ruby for me and I could start working. I downloaded a good Geany theme for good measure and off I went.

First I want to write a helper method that read the contents of a text file and return it to me as a string.

def read_file_for_contents(filePath)
    file = File.new(filePath, "r")

    content = ""
    while (line = file.gets)
        content = content + line
    end

    file.close
    content
end



Next, I wrote a helper method to easily let me write translated text to a file with the same filename as the original only with 'pig' added to it to easily identify what file is was a translation of.

def write_translation_to_file(filePath, translatedText)
    File.open("pig" + filePath, "w") do |file|
        file.puts translatedText
    end
end


Now for the actual nitty gritty of it all, I wrote the method to translate a word to pig latin.

I've followed the spec on Wikipedia and it seems to work 'good enough' but I'm sure some words aren't correctly translated,especially the dual syllable words, such as 'birdhouse'.

def convert_word_to_pig_latin(word)
    consonants = [ "B", "C", "D", "F", "G", "H", 
		   "J", "K", "L", "M", "N", "P", 
		   "Q", "R", "S", "T", "V", "X", 
		   "Z", "W", "Y"]

    if consonants.include?(word.chars.first.capitalize)
        if consonants.include?(word[1,1].capitalize)
	    word[2..-1] + "-" + word[0,2] + "ay "
	else
	    word[1..-1] + "-" + word[0,1] + "ay "
	end
    else
 	word + "-way "
    end
end




Here's the entire script:

def read_file_for_contents(filePath)
	file = File.new(filePath, "r")
	content = ""
	while (line = file.gets)
		content = content + line
	end
	file.close
	content
end

def write_translation_to_file(filePath, translatedText)
	File.open("pig" + filePath, "w") do |file|
		file.puts translatedText
	end
end

def convert_word_to_pig_latin(word)
	consonants = [ "B", "C", "D", "F", "G", "H", 
				   "J", "K", "L", "M", "N", "P", 
				   "Q", "R", "S", "T", "V", "X", 
				   "Z", "W", "Y"]

	if consonants.include?(word.chars.first.capitalize)
		if consonants.include?(word[1,1].capitalize)
			word[2..-1] + "-" + word[0,2] + "ay "
		else
			word[1..-1] + "-" + word[0,1] + "ay "
		end
	else
		word + "-way "
	end
end


#Beginning of the actual script.
content = read_file_for_contents(ARGV[0])
translatedText = ""

content.split.each do |word|
	translatedText = translatedText + convert_word_to_pig_latin(word)
end

write_translation_to_file(ARGV[0], translatedText)

puts "Translation completed"



Here's how you invoke it:

ruby piglatin.rb test.txt



Ruby is a pretty awesome language and I like the power of its blocks. Also notice how the code is very easy to follow? If you don't know Ruby I highly suggest you give it a shot, you won't regret it! :)

1 Comments On This Entry

Page 1 of 1

Skaggles Icon

10 May 2011 - 12:30 PM
If interested in using Ruby, here's a good post on using RVM to install and setup ruby on Ubuntu - http://ryanbigg.com/...m-rails-and-you
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)

About Me

Posted Image


Bienvenidos! I'm a USA ex-pat living in Bolivia for the past 10 years. Web development is my forte with a heavy lean for usability and optimization. I'm fluent in both English and Spanish. I guest write for the popular Python website Python Central. Visit my website.

Categories