是否可以使用代码转换格式的列表' list':" translation"?

时间:2014-07-05 19:56:16

标签: javascript python html database list

我希望能够更改编码中的单词列表,例如

var words= { 
AFK Away from the keyboard 4U For you B4N By for now BBL Be back later BDAY Birthday CBA Can't be asked  }

更改为以下格式:

var words= {
'AFK': "Away from the keyboard", '4U': "For you", 'B4N': "By for now", 'BBL': "Be back      later", 'BDAY': "Birthday", 'CBA': "Can't be asked", 
  }

无需使用HTML / JavaScript手动将每个单词更改为格式。但是我知道这可能是不可能的,但是我想我是否有人知道如何做到这一点。从我读到的内容看起来好像我将不得不使用Python和数据库,但我不知道任何关于python的事情,所以我真的希望(可能是徒劳)有一些HTML /我还没有看到解决这个问题的JavaScript代码!

我在这里发现了一个类似的问题,但它并不是我想要的,因为它使用了python:[turning data into a list

我想要做的是改变这种格式的所有单词:例如    AFK远离键盘

到格式 ' AFK':"远离键盘",

这段代码的目的是将文本缩写翻译成它已经在做的真正的英语单词,但为了获得相当数量的单词翻译我需要获得上述格式的单词,如果我需要永远单独形成每一个。如果有帮助,这里是其余的代码:

function replacer() {
var text= document.getElementById("textbox1").value;
for (var modifiers in translationwords){ 
    text = text.replace(new RegExp('\\b' + modifiers + '\\b', 'gi'), translationwords[modifiers]); }
document.getElementById("textbox2").value=text;
document.getElementById("add").onclick= function storage() {  
if(!document.cookie) document.cookie = "";   
document.cookie = document.cookie +"<li>"+document.getElementById("textbox1").value+ ":"+"</li>";
document.cookie = document.cookie +"<li>" + document.getElementById("textbox2").value+ "</li>";
document.getElementById("array").innerHTML= document.cookie;
}

}   




function textdelete(x) {
if (x.value=="ENTER TRANSLATION HERE"){
x.value="";
};
}

谢谢

3 个答案:

答案 0 :(得分:1)

如果您的单词以字符串开头,那么可以说它可以按照您想要的方式转换为对象,但它可能不是最准确的翻译。

由于看起来所有缩写都是大写且没有空格,我们可以查看字符串,并将所有大写/数字'单词'设置为属性,并使用以下文本字符串作为值。在javascript中,这样的东西会起作用。

//set words as string
var string = "AFK Away from the keyboard 4U For you B4N By for now BBL Be back later BDAY Birthday CBA Can't be asked";

// create empty dictionary for storage
var dictionary = {};   

// use regex to find all abbreviations and store them in an array
var abbreviations = string.match(/[A-Z0-9]+(?![a-z])\w/g);   
// returns ["AFK", "4U", "B4N", "BBL", "BDAY", "CBA"] 

// use regex to replace all abbreviations with commas... 
englishWords = string.replace(/[A-Z0-9]+(?![a-z])\w/g, ','); 
  // Edit (see below):
englishWords = englishWords.replace(/\W,\W/g,',');
  // End edit 
// then split string into array based on commas
englishWords = englishWords.split(',').slice(1);  

// finally loop over all abbreviations and add them to the dictionary with their meaning.
for(var i = 0; i < abbreviations.length; i++){
    dictionary[abbreviations[i]] = englishWords[i];
}

编辑:上述解决方案仍然可能在每个英文字符串的开头或结尾有空格。您可以在拆分字符串之前添加此行代码以删除空格。

englishWords = englishWords.replace(/\W,\W/g,',');

答案 1 :(得分:0)

假设您将数据作为python中的列表或字符串,或者您可以将其转换为该格式:

>>> lst = 'AFK Away from the keyboard 4U For you B4N By for now BBL Be back later BDAY Birthday CBA Can\'t be asked'.split()
>>> lst
['AFK', 'Away', 'from', 'the', 'keyboard', '4U', 'For', 'you', 'B4N', 'By', 'for', 'now', 'BBL', 'Be', 'back', 'later', 'BDAY', 'Birthday', 'CBA', "Can't", 'be', 'asked']

进一步假设关键字始终为全大写且值永远不是全部大写,并且没有重复的键,您可以创建以下字典:

>>> keys = [x for x in lst if x.upper() == x]
>>> {keys[i]:' '.join(lst[lst.index(keys[i]):lst.index(keys[i+1])]) for i in range(len(keys)-1)}
{'BDAY': 'BDAY Birthday', 'BBL': 'BBL Be back later', '4U': '4U For you', 'B4N': 'B4N By for now', 'AFK': 'AFK Away from the keyboard'}

答案 2 :(得分:0)

我们假设这是您的单词列表:

["AFK","Away","from","the","keyboard","4U","For","you","B4N","By","for","now","BBL","Be","back","later","BDAY","Birthday","CBA","Can't","be","asked"]

现在,我们可以简单地使用常规JavaScript将其更改为对象,而无需任何数据库或后端语言,如Python。

var list = ["AFK","Away","from","the","keyboard","4U","For","you","B4N","By","for","now","BBL","Be","back","later","BDAY","Birthday","CBA","Can't","be","asked"] /*What we have*/;
var code = {} /*What we want*/;
var currKey = null /*Our current key*/, hasBeenSet = false /*Whether or not the property at our current key has been set*/;
for (var i = 0; i < words.length; i++) {
    //If the current word is an abbreviation...
    if (words[i] === words[i].toUpperCase()) {
        currKey = words[i]; //We set currKey to it
        hasBeenSet = false; //We make hasBeenSet false
    }
    //Otherwise, if the property at current key has been set, we add on to it.
    else if (hasBeenSet) code[currKey] += " "+words[i];
    //Otherwise, the property at current key hasn't been set...
    else {
        code[currKey] = words[i]; //We set the property to the current word
        hasBeenSet = true; //We make hasBeenSet true
    }
}
code //{ AFK: "Away from the keyboard", 4U: "For you", B4N: "By for now", BBL: "Be back later", BDAY:"Birthday", CBA:"Can't be asked" }