【问题标题】:Is it possible to use code to transform a list in format 'list': "translation"?是否可以使用代码以“列表”格式转换列表:“翻译”?
【发布时间】:2023-03-18 22:06:01
【问题描述】:

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

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="";
};
}

谢谢

【问题讨论】:

  • 你的代码最初是如何以这种形式结束的?
  • 我不确定你为什么认为你需要 Python 和数据库。我什至无法想象数据库会在这里做什么,几乎所有你可以用 Python 做的事情都可以用 Javascript 做。
  • 那不是有效的 Javascript。是什么创造了 words 非对象?
  • 从解释当前数据的来源开始。
  • 我的印象是 [stackoverflow.com/questions/13458430/turning-data-into-a-list] 涉及 Python,我可能错了。

标签: javascript python html database list


【解决方案1】:

假设您将数据作为 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'}

【讨论】:

  • 我试图避免使用 python,因为我对此一无所知。谢谢
【解决方案2】:

假设这是您的单词列表:

["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" }

【讨论】:

    【解决方案3】:

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

    由于看起来所有缩写都是大写且没有空格,我们可以查看字符串,并将所有大写/数字“单词”设置为属性,并将以下文本字符串作为值。在 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,',');
    

    【讨论】:

    • 如何调用代码的结果?例如所以"ABBREVIATION": "translation", 实际在页面上?
    • 代码的结果是一个包含所有翻译的对象。该对象被命名为“字典”,因此您可以将其作为常规 javascript 对象访问,例如使用 dictionary.AFK 或 dictionary['AFK']。 (或AFK以外的一些缩写)
    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    相关资源
    最近更新 更多