Leet to English Translation
Javascript (49 + 5635) * 10 / 2174 = 26.14
Online Demo:
"Real dictionary" option doesnt work on drop box but it will work if run in a real web server. Tested in visual studio development server and google chrome.
https://dl.dropboxusercontent.com/u/141246873/leettranslator/index.html
Score:
49 leet items = 49
Capitalization bonus = 5 * 49 = 245
dictionary lookup bonus = 20 * 49 = 980
exclamation bonus * 3 = 90 * 49 = 4410
(leet items + bonuses) * 10 / (code length)
(49 + 5635) * 10 / 2174 = 26.14
Code:
function IsInDict(e) { return W[e] } function translate(e) { words = e.split(" "); res = ""; for (var t in words) { ex = ""; function n(e, r, i) { var s = false; for (var o = 1; o <= e.length; o++) { var u = e.substring(0, o) === "!" || i; var a = et[e.substring(0, o)]; var f = e.substring(o); if (a) { s = true; if (f.length === 0) { if (u) { ex = r + a; words[t] = "" } } else n(f, r + a, u) } } if (i && !s && r) { ex = r; words[t] = e.split("").reverse().join("") } } n(words[t].split("").reverse().join(""), "", false); mes = []; function r(e, t) { for (var n = 1; n <= e.length; n++) { var i = tokens[e.substring(0, n)]; var s = e.substring(n); if (i) { mFound = true; if (s.length === 0) for (var o in i) { mes.push(t + i[o]) } else for (var o in i) r(s, t + i[o]) } } if (e.length > 1) r(e.substring(1), t + e.substring(0, 1)); else { mes.push(t + e) } } m = ""; if (words[t] !== "") { r(words[t].toLowerCase(), ""); if (mes.length === 1) m = mes[0]; else { sl = []; for (var i in mes) { if (IsInDict(mes[i].slice(-1) === "." ? mes[i].substring(0, mes[i].length - 1) : mes[i])) { sl.push(mes[i]) } } if (sl.length > 0) m = sl[0]; else m = mes[0] } if (res === "") { m = cap(m) } if (res.slice(-1) === ".") { m = cap(m) } } res += " " + m; if (ex !== "") res += ex } return res.trim() } function cap(e) { return e.charAt(0).toUpperCase() + e.slice(1) } tokens = { $: ["s"], "(": ["c"], 5: ["s"], "@": ["a"], 4: ["a", "for"], 3: ["e"], "+": ["t"], "#": ["h"], teh: ["the"], "'d": ["ed"], pwnd: ["pwned"], pwnt: ["pwned"], k: ["ok"], kk: ["ok"], 0: ["o"], y: ["why"], txt: ["text"], dafuq: ["what the f**k"], "/\\": ["a"], "^": ["a"], "\\/": ["v"], d00d: ["dude"], n00b: ["newbie"], "\\/\\/": ["w"], 8: ["b", "ate"], "|_|": ["u"], "|-|": ["h"], "Я": ["r"], j00: ["you"], joo: ["you"], vv: ["w"], tomoz: ["tomorrow"], "|<": ["k"], "[)": ["d"], "|)": ["d"], "<3": ["love"], "><": ["x"], 10100111001: ["leet"], 2: ["to", "too"], ur: ["your", "you're"], u: ["you"], x: ["ks", "cks"], z: ["s"], 1: ["i", "l"], "!": ["i"], c: ["see", "sea"], b: ["be", "bee"], "&": ["and", "anned", "ant"], 7: ["and", "anned", "ant", "t"] }; et = { eno: "!", nevele: "!!", 1: "!", "!": "!" }
Ungolfed:
tokens={
'$':['s'],'(':['c'],'5':['s'],'@':['a'],'4':['a','for'],'3':['e'],'+':['t'],'#':['h'],'teh':['the'],"'d":['ed'],'pwnd':['pwned'],
'pwnt':['pwned'],'k':['ok'],'kk':['ok'],'0':['o'],'y':['why'],'txt':['text'],'dafuq':['what the f**k'],
'/\\':['a'],'^':['a'],'\\/':['v'],'d00d':['dude'],'n00b':['newbie'],
'\\/\\/':['w'],'8':['b','ate'],'|_|':['u'],'|-|':['h'],'Я':['r'],'j00':['you'],
'joo':['you'],'vv':['w'],'tomoz':['tomorrow'],'|<':['k'],'[)':['d'],'|)':['d'],'<3':['love'],
'><':['x'],'10100111001':['leet'],'2':['to','too'],'ur':["your","you're"],
'u':['you'],'x':['ks','cks'],'z':['s'],'1':['i','l'],'!':['i'],'c':['see','sea'],
'b':['be','bee'],'&':['and','anned','ant'],'7':['and','anned','ant','t']}
var excTokens = {'eno':'!','nevele':'!!','1':'!','!':'!'}
function IsInDict(word)
{
return (W[word]);
}
function translate(input) {
var words = input.split(" ");
var result = "";
for (var i in words) {
var exclamations = "";
function parseExclamations(s, prev, exclamationFound) {
var matchFound = false;
for (var j = 1; j <= s.length; j++) {
var hasExclamation = (s.substring(0, j) === "!") || exclamationFound;
var currentToken = excTokens[s.substring(0, j)];
var remaining = s.substring(j);
if (currentToken) {
matchFound = true;
if (remaining.length === 0) {
if (hasExclamation) {
exclamations = prev + currentToken;
words[i] = "";//word only had exclamations in it so dont parse the rest of it
}
}
else
parseExclamations(remaining, prev + currentToken, hasExclamation);
}
}
if (exclamationFound && !matchFound && prev) {
exclamations = prev;
words[i] = s.split("").reverse().join("");//reverse back again
}
}
var reverseWord = words[i].split("").reverse().join("");
parseExclamations(reverseWord, "", false);
var matches = []
function parse(s, prev) {
for (var j = 1; j <= s.length; j++) {
var currentTokenArray = tokens[s.substring(0, j)];
var remaining = s.substring(j);
if (currentTokenArray) {
matchFound = true;
if (remaining.length === 0)
for (var k in currentTokenArray) {
matches.push(prev + currentTokenArray[k]);
}
else
for (var k in currentTokenArray)
parse(remaining, prev + currentTokenArray[k]);
}
}
if (s.length > 1)
parse(s.substring(1), prev + s.substring(0, 1));
else {
matches.push(prev + s);
}
}
var match = "";
if (words[i] !== "") {
parse(words[i].toLowerCase(), "");
//check the dictionary
if (matches.length === 1)
match = matches[0];
else {
var shortlist = [];
for (var j in matches) {
//check dictionary. allow for a full stop at the end of the word
var isInDict = IsInDict(matches[j].slice(-1) === "." ? matches[j].substring(0, matches[j].length - 1) : matches[j]);
if (isInDict) {
shortlist.push(matches[j]);
}
}
if (shortlist.length > 0)
match = shortlist[0];
else
match = matches[0];
}
if (result === "") {
match = cap(match);
}
if (result.slice(-1) === ".") {
match = cap(match);
}
}
result += " " + match;
if (exclamations !== "")
result += exclamations;
}
return result.trim();
}
function cap(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
Test results:
- |-|3 15 $|_|(# @ n00b ====> He is such a newbie
- @1//4Y5 p0$+ ur n3VV qu35710nz 1n teh $&80x ====> Always post your new questions in the sandbox
- !!!1!!1 ====> !!!!!!!
- !!!one! ====> !!!!!
- !eleven ====> !!!
- teh!!!1!!1 ====> The!!!!!!!
- teh!!!one! ====> The!!!!!
- teh!eleven ====> The!!!
- teh !!!1!!1 ====> The !!!!!!!
- qu35710nz!1! ====> Questions!!!
- +357 +357. 735+ ====> Test test. Test
- & 31!73 #4(KER$ WR0+3 83773R L!K3 +#!5 7#@N 2D@Y ====> And elite hackers wrote better like this than today
Notes:
The dictionary is a seperate javascript file with an object called W containing all of the words. This just contains the words I needed for running the relevant tests.
Haskell - Score 1.421421421 : (37 items + (21 bonuses (capitalization) * 5)) * 10 / (999 bytes)
This is my final answer.
import System.Environment
import Text.Parsec
import Text.Parsec.String
s=string
r=return
t=try
o=oneOf
(>|)=(<|>)
a p l u=b[p]l u
b (p:q) l u=e(foldl(>|)(s p)$map(s)q)l u
c p l u=e(o p)l u
d p q=t$s p>>r q
e p l u=t$do{p;r l}>|do{s". ";p;r$". "++u}
f p q=t$do{between(t$s" ")(t$s" ")(o p);r q}
g::Parser String
g=do{s<-many$c"$5""s""S">|c"@4^""a""A">|c"3""e""E">|c"7+""t""T">|c"#""h""H">|d"teh""the">|d"'d""ed">|d"pwnd""pwned">|d"pwnt""pwned">|c"kK""ok""OK">|d"kk""OK">|d"n00b""newbie">|f"yY""why">|d"4""for">|d"txt""text">|d"dafuq""what the f**k">|b["\\/\\/","vv","VV"]"w""W">|a"/\\""a""A">|d"d00d""dude">|c"0""o""O">|a"\\/""v""V">|c"8""b""B">|a"|_|""u""U">|a"|-|""h""H">|c"Я""r""R">|b["j00","joo"]"you""you">|d"tomoz""tomorrow">|a"|<""k""K">|b["[)","|)"]"d""D">|d"<3""love">|a"><""x""X">|c"1!""i""I">|d"10100111001""leet">|c"2""too""to">|d"ur""your">|d"UR""you're">|f"uU""you">|c"xX""ks""cks">|d"&""and">|do{c<-anyChar;return [c]};return$concat s}
main=getArgs>>=putStrLn.show.(parse g"").concat
Tests
When the program is compiled to a file named min-lt
, you can write the following shell script
#!/bin/bash
./min-lt "|-|3 15 $|_|(# @ n00b"
./min-lt "\$#!+"
./min-lt "\/\/ 1 |< 1 P 3 [) 1 A"
./min-lt "H4X0R"
./min-lt "@1\/\/4Y5 p0$+ ur n3VV qu35710nz 1n teh $&80x"
./min-lt "+357 +357. 735+"
which will print this
Right "he is su(h a newbie"
Right "shit"
Right "w i k i P e d i A"
Right "HaksoR"
Right "aiwaYs post your new questionz in the sandboks"
Right "test test. Test"
Extended BrainFuck : 0.6757
{a[-])&d}{c(-(-}{d)$t(-}:r:i:t$t,(-$i+$r+)$i($t 6+(-$i 5--)+$i 3+(--&c-(--
(3-(5-&c&c&c-&c--5-((-&d)$r.&d|"A"&a|"B"&a|"T"&a|"S"&a|"A"&a|"E"&a|"TO"&a
|"L"&a|"O"&a|"T"&a|"C"&a|"AND"&a|"S"&a|"H"&a|"I"(-))$i(-)$r(-)$t,(-$i+$r+))
So this does 15 translations "$(5@437+#0821!&" , no bonuses and it has 222 bytes (uneccesary linefeeds not included). 15*10/222 = 0.6757
Usage:
%> beef ebf.bf < leet.ebf > leet.bf
%> echo '& 31337 #4(KER$ WR0+3 83773R L!K3 +#!5 7#@N 2D@Y' | beef leet.bf
AND ELEET HACKERS WROTE BETTER LIKE THIS THAN TODAY
%>
EBF isn't really made for golfing but it's rle feature, macros and print string feature makes it somewhat easier to compress than BrainFuck. The end compiled BrainFuck binary looks like this:
>>,[-<+<+>>]<[>++++++[-<------>]+<+++[--[-[--[--[---[-----[-[-[-[-[-[--[-[--------[[-]
>[-]<<.>]>[->++++++++[-<++++++++>]<+.[-]]<]>[->++++++++[-<++++++++>]<++.[-]]<]>[->++++
+++++[-<+++++++++>]<+++.[-]]<]>[->+++++++++[-<+++++++++>]<++.[-]]<]>[->++++++++[-<++++
++++>]<+.[-]]<]>[->++++++++[-<+++++++++>]<---.[-]]<]>[->+++++++++[-<+++++++++>]<+++.--
---.[-]]<]>[->++++++++[-<+++++++++>]<++++.[-]]<]>[->+++++++++[-<+++++++++>]<--.[-]]<]>
[->+++++++++[-<+++++++++>]<+++.[-]]<]>[->++++++++[-<++++++++>]<+++.[-]]<]>[->++++++++[
-<++++++++>]<+.+++++++++++++.----------.[-]]<]>[->+++++++++[-<+++++++++>]<++.[-]]<]>[-
>++++++++[-<+++++++++>]<.[-]]<]>[->++++++++[-<+++++++++>]<+.[-]]<[-]<[-]>>,[-<+<+>>]<]