Javascript object oriented pirate translator

Javascript object oriented pirate translator

EDIT2: Ive modified your Dictionary. If you follow the format of my Dictionary you can go as deep of arrays as you want. It should work. Ive tested with your examples in all different kinds of orders and with text not found in the dictionary. Feel free to try it out.

And to add to the challenge, I made recursive functions. 😀 No for/while loops.

DEMO

var Dictionary = {
    hello: ahoy,
    hi: yohoho,
    pardon: [1, {
        me: avast
    }],
    excuse: [1, {
        me: arrr
    }],
    stranger: scurvy,
    sir: matey,
    madam: proud beauty,
    miss: comely wench,
    officer: foul blaggart,
    enemy: scallywag,
    where: [1, {
        is: whar be
    }],
    can: [4, {
        you_help_me_find: know ye
    }],
    is: [1, {
        that: be that
    }],
    how: [4, {
        far_is_it_to: how many leagues to
    }],
    the: th,
    a: a briny,
    any: some godforsaken,
    nearby: broadside,
    my: me,
    your: yer,
    old: barnacle-covered,
    attractive: comely,
    happy: grog-filled,
    restroom: head,
    restaurant: galley,
    hotel: fleabag inn,
    mall: market,
    pub: Skull & Scuppers,
    bank: buried trasure,
    would: [1, {
        like: be needin
    }],
    I: [
        [1, {
            desire: I have a fierce fire in me belly
        }],
        [5, {
            wish_I_knew_how_to: I be hankerin to
        }]
    ],
    my: [
        [4, {
            mother_told_me_to: me dear ol mum, bless her black soul, tol me to
        }],
        [4, {
            companion_would_like_to: me mate, ol Rumpot, wants to
        }]
    ],
    find: come across,
    take: [2, {
        a_nap: have a bit of a lie down
    }],
    make: [2, {
        a_withdrawal: seize all yer doubloons
    }],
    have: [2, {
        a_cocktail: swill a pint or two of grog
    }],
    kill: [1, {
        him: blow the man down
    }],
    frown: hang the jib,
    take: [2, {
        a_hike: walk the plank
    }]

};

function translate(text) {
    var hop = 1;
    var textToReturn = ;
    //checking if the text is already split, if not we split
    if (typeof text === string) {
        text = text.split( );
    }
    if (text.length > 0) {
        if (typeof Dictionary[text[0]] == undefined || typeof Dictionary[text[0]] === string) {
            textToReturn = (Dictionary[text[0]] || text[0]);
            text = text.slice(hop, text.length);
        } else {
           var info = recursiveCheck(text, Dictionary[text[0]]);
            textToReturn =  (info.hop == 1) ? text[0] : info.text;
            text = text.splice(info.hop, text.length);
        }
        if(text.length > 0)
        {
             textToReturn +=   + translate(text);  
        }

    }
    return textToReturn;
}
function recursiveCheck(text, arr)
{
    var found = {hop:1, text: };
    if(arr.length > 0)
    {
        if(typeof parseInt(arr[0]) === number && text.length-1 >= arr[0])
        {
            var phrase = text.slice(1, arr[0]+1);
            if(arr[1][phrase.join(_)])
            {
                 found.hop = arr[0]+1;
                 found.text = arr[1][phrase.join(_)];
            }
        }
        else
        {
             found = recursiveCheck(text, arr[0] || []);  
             if(found.hop == 1 && arr.length > 1)
             {
                  found = recursiveCheck(text, arr.slice(1, arr.length));
             }
        }
    }
    return found;
}
var tra = document.getElementById(translate);
var pir = document.getElementById(pirate);
pir.disabled = true;
var eng = document.getElementById(english);

eng.onkeyup = function(){
     pir.value = ;   
}
tra.onclick = function () {
    pir.value = translate(eng.value);
};

HERE is an example of a deep array if you want to go further with the dictionary:

...
        I: [
            [1, {
                desire: [
                          [1,{ a: I have a fierce fire in me belly}],
                          [1,{ one: I have one fierce fire in me belly}]

            }],
            [5, {
                wish_I_knew_how_to: I be hankerin to
            }]
        ],
...

Of course I havent tried it yet, but you can if you actually need it to work. Good luck.

EDIT: The point of this code is to show how to access your list. You dont seem to be using categories in your code, so why have them?

Your list looks a bit complex for a simple translation. Last I checked, dictionaries dont have categories.. Joke aside Ive simplified your list.

var Dictionary = {
        hello:ahoy,
        hi:yohoho,
        pardon_me:avast,
        excuse_me:arrr,
        stranger:scurvy,
        sir:matey,
        madam:proud beauty,
        miss:comely wench,
        officer:foul blaggart,
        enemy:scallywag,
        where_is:whar be,
        can_you_help_me_find:know ye,
        is_that:be that,
        how_far_is_it_to:how many leagues to,
        the:th,
        a:a briny,
        any:some godforsaken,
        nearby:broadside,
        my:me,
        your:yer,
        old:barnacle-covered,
        attractive:comely,
        happy:grog-filled,
        restroom:head,
        restaurant:galley,
        hotel:fleabag inn,
        mall:market,
        pub:Skull & Scuppers,
        bank:buried trasure,
        I_would_like_to:I be needin to,
        I_desire:I have a fierce fire in me belly,
        I_wish_I_knew_how_to:I be hankerin to,
        my_mother_told_me_to:me dear ol mum, bless her black soul, tol me to,
        my_companion_would_like_to:me mate, ol Rumpot, wants to,
        find:come across,
        take_a_nap:have a bit of a lie down,
        make_a_withdrawal:seize all yer doubloons,
        have_a_cocktail:swill a pint or two of grog,
        kill_him:blow the man down,
        frown:hang the jib,
        take_a_hike:walk the plank

};

function translate(text)
{
    pir.value = Dictionary[text.split( ).join(_)] || not found;

}

var tra = document.getElementById(translate);
var pir = document.getElementById(pirate);
pir.disabled = true;
var eng = document.getElementById(english);

tra.onclick = function(){ translate(eng.value) };

html:

<input id=english type=text placeholder=english/>
<input id=pirate placeholder=pirate/>
<button id=translate>Translate</button>

Ive simplified the code (by a lot) so I could get a simple working model.

Working jsfiddle: http://jsfiddle.net/Grimbode/f296h/2/

Javascript object oriented pirate translator

Leave a Reply

Your email address will not be published. Required fields are marked *