Javascript面向对象翻译器
我需要用javascript制作一个面向对象的海盗翻译器来完成我的期末考试,我有一个想法,有一些代码,但无法在我的页面上正确输出。我已经设置好了,所以你在一个框中输入英语短语,然后在另一个框中它会吐出盗版版本。我不会单步遍历我的对象数组。我会发布代码。谢谢你对我的帮助Javascript面向对象翻译器,javascript,Javascript,我需要用javascript制作一个面向对象的海盗翻译器来完成我的期末考试,我有一个想法,有一些代码,但无法在我的页面上正确输出。我已经设置好了,所以你在一个框中输入英语短语,然后在另一个框中它会吐出盗版版本。我不会单步遍历我的对象数组。我会发布代码。谢谢你对我的帮助 var $ = function(id) { return document.getElementById(id); } var Dictionary = [{ Greetings: { hello:
var $ = function(id) { return document.getElementById(id); }
var Dictionary = [{
Greetings: {
hello:"ahoy",
hi:"yohoho",
pardon_me:"avast",
excuse_me:"arrr"
},
People: {
stranger:"scurvy",
sir:"matey",
madam:"proud beauty",
miss:"comely wench",
officer:"foul blaggart",
enemy:"scallywag"
},
Questions: {
where_is:"whar be",
can_you_help_me_find:"know ye",
is_that:"be that",
how_far_is_it_to:"how many leagues to"
},
Articles: {
the:"th",
a:"a briny",
any:"some godforsaken",
nearby:"broadside",
my:"me",
your:"yer"
},
Adjectives: {
old:"barnacle-covered",
attractive:"comely",
happy:"grog-filled"
},
Places: {
restroom:"head",
restaurant:"galley",
hotel:"fleabag inn",
mall:"market",
pub:"Skull & Scuppers",
bank:"buried trasure"
},
Desires: {
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"
},
Actions: {
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)
// Returns: a copy of text with English phrases replaced by piratey equivalents
{
for (var i = 0; i < Dictionary.length; i++) {
var toReplace = new RegExp("\\b"+Dictionary[i][0]+"\\b", "i");
var index = text.search(toReplace);
while (index != -1) {
text = text.replace(toReplace, Dictionary[x][y]);
index = text.search(toReplace);
}
}
text = text.replace(/\./g, function() {
return Math.random() < 0.5 ? ". arrrrrrrrr" : "."
});
return text.charAt(0).toUpperCase() + text.substring(1);
}
var clear_click = function() {
$("output1").value = "";
$("output2").value = "";
}
window.onload = function() {
$("clear").onclick = clear_click;
}
/*for (var x in Dictionary) {
for (var y in Dictionary[x])
console.log (y, Dictionary[x][y]);
}*/
var$=function(id){return document.getElementById(id);}
变量字典=[{
问候:{
你好:“喂”,
嗨:“yohoho”,
请原谅我:“阿瓦斯特”,
对不起,“啊”
},
人民:{
陌生人:“坏血病”,
先生:“伙计”,
夫人:“骄傲的美女”,
小姐:“漂亮的丫头”,
警官:“肮脏的布拉加特”,
敌人:“scallywag”
},
问题:{
_的位置是:“whar be”,
你能帮我找到“认识你”吗,
是不是:“就是这样”,
距离有多远:“距离有多少联赛?”
},
文章:{
“th”,
a:“咸水”,
任何:“被上帝抛弃的人”,
附近:“舷侧”,
我的:“我”,
你的:“耶”
},
形容词:{
老:“藤壶盖”,
漂亮:“漂亮”,
快乐:“充满了激情”
},
地点:{
洗手间:“头”,
餐厅:“厨房”,
酒店:“弗利巴格酒店”,
商场:“市场”,
酒吧:“骷髅和排水口”,
银行:“被埋葬的trasure”
},
愿望:{
我想说:“我需要”,
我渴望:“我肚子里有烈火”,
我希望我知道该怎么做:“我渴望”,
我妈妈告诉我:“我亲爱的老妈妈,保佑她黑色的灵魂,告诉我”,
我的同伴会说:“我的朋友,老家伙,想要”
},
行动:{
查找:“遇到”,
小睡一会儿:“躺一会儿”,
撤退:“抓住你所有的敌人”,
喝鸡尾酒:“喝一两品脱烈性酒”,
杀了他:“把他打倒”,
皱眉:“挂三角帆”,
徒步旅行:“走木板”
},
}];
函数翻译(文本)
//返回:文本的副本,其中英文短语替换为盗版等价物
{
for(var i=0;i
HTML:
盗版翻译人员
雅各布的海盗翻译
只需单击按钮即可翻译
从英语到海盗语的单词和/或短语。
英语
海盗
EDIT2:我修改了你的字典。如果你按照我的字典的格式,你可以根据你的需要深入数组。它应该会起作用。我以各种不同的顺序和字典中找不到的文本测试了你的例子。请随便试用
为了增加挑战,我制作了递归函数:D无for/while循环
如果您想进一步使用字典,下面是一个深度数组的示例:
...
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"
}]
],
...
当然我还没有试过,但是如果你真的需要它的话,你可以试一下。祝你好运
编辑:此代码的目的是显示如何访问列表。您似乎没有在代码中使用类别,那么为什么要使用它们呢
对于简单的翻译,您的列表看起来有点复杂。上次我检查过,字典没有分类。。别开玩笑了,我已经简化了你的清单
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:
翻译
我已经简化了代码(很多),所以我可以得到一个简单的工作模型
工作JSFIDLE:您如何知道它不会迭代您的数据?你做了什么来调试这个问题?您在控制台中有任何错误吗?“``在那里肯定是无效的。它确实会迭代我的数据,在我的js文件的底部,你可以看到我有一个for in循环,它会打印到控制台,它会打印出我所有的对象,我只是不知道我在文本区域中试图获得正确的输出时哪里出错了。正确的输出是什么?你目前得到的输出是什么?对不起,我想这只是一个措词不好的问题?所以我试图让它成为这样,当有人键入一个英语短语,如“你好”,然后点击翻译按钮,其他文本区域将显示盗版翻译,如“喂”谢谢,我想我是想说得有点太具体了。我欣赏它的简单。哈哈,我似乎总是看得比需要更多。你帮了大忙!没问题,请确保将此标记为正确答案。祝你好运Dso我正在尝试使用户能够在框中键入多个术语并让其输出一系列盗版术语来实现这一点。例如,如果他们键入“hello陌生人”,我希望它立即输出“ahoy scurvy”,如果您键入多个术语,它将输出undefined.Updated。我改变了很多事情。检查我的编辑以查看修改。花了我几个小时。我喜欢挑战。
...
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"
}]
],
...
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) };
<input id="english" type="text" placeholder="english"/>
<input id="pirate" placeholder="pirate"/>
<button id="translate">Translate</button>