This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jesusthekiller's profile picture

Pig Latin Translator in CC [200 posts special :P]

Started by jesusthekiller, 07 June 2013 - 08:19 AM
jesusthekiller #1
Posted 07 June 2013 - 10:19 AM
Pig Latin Translator

igPay atinLay TWFay!


So, this is a small thing that I've just created (for 200 posts special, why not?). It translates English into Pig Latin and Pig Latin into English.


Download *oink oink*:


pastebin get 6mWFvSgR pig


Screens *oink oink*:

Spoiler








License *oink oink*:

theoriginalbit #2
Posted 07 June 2013 - 10:26 AM
Haha, nice, not quite translating correct but it would be very hard to get that working, the program would have to have a working knowledge of English…

One slight improvement which would reduce the resources and complexity:
Spoiler


local vowels = { a = true, e = true, i = true, o = true, u = true }
local function isVowel(l)
  return vowels[ l:lower() ] == true
end
jesusthekiller #3
Posted 07 June 2013 - 10:31 AM
Thanks!

And actually there is almost no difference in O(N) and O(N*21) in worst scenario :P/>
ElvishJerricco #4
Posted 08 June 2013 - 05:23 AM
I felt like this could be done much more easily with the string library patterns, so I did that and also cleaned up some code I felt was dirty.


local args = {...}
local vowels = "AEIOUaeiou"

local function usageError()
	error([[
Usage:
Code:
	pig c [text]
Decode:
	pig d [text]

Output is also saved to pig_out]])
end

if #args < 2 then
	usageError()
end

local f = assert(fs.open(fs.combine(shell.dir(), "pig_out"), "w"))
local ow = write

local write = function(t)
	f.write(t)
	ow(t)
end

local mode = table.remove(args, 1)
local str = table.concat(args, " ")
if mode == "c" then
	local s = str:gsub("(%A*)([^" .. vowels .. "']?)(%a+)",function(a,b,c)
		if #b == 0 then
			return a .. c:sub(2) .. c:sub(1,1) .. "way"
		else
			return a .. c .. b .. "ay"
		end
	end)
	write(s)
elseif mode == "d" then
	local s = str:gsub("(%A*)([%a']+)(ay)", function(a,b,c)
		if b:find("[" .. vowels .. "]w$") == #b - 1 then
			b = b:sub(1,-2)
		end
		return a .. b:sub(-1) .. b:sub(1,-2)
	end)
	write(s)
else
	usageError()
end

print()
f.close()

EDIT: To add a bit more detail about what I changed, the pig_out file is now put in the shell.dir() directory, the program errors out when usage is wrong instead of just printing and returning (same effect, but this way if you're using a shell that handles errors differently, it's handled that way), the program errors out when file opening fails, apostrophe's are allowed in words, and it's set up so str = table.concat(args, " ") can be replaced with any means of getting a string with all the words and whitespace your heart desires. Oh and also it handles decoding words with a vowel at the beginning a little better. I can't think of a better way to make that perfect than to have a dictionary of real words, and at that point it's questionable whether that's faster than polling some site via http.