大佬教程收集整理的这篇文章主要介绍了节点 - 测试字符串是否包含数组元素用相同数组的随机元素替换他(同义词),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我测试字符串(输入)是否包含数组的一个元素(单词)。如果是真的,我想用同一个列表的元素随机替换它。
var input = "This is an amazing text blob where this word amazing is replaced by a random word from List_of_words. Isn't this amazing!";
words_synonym = ["amazing","formIDable","great","smart"];
// first condition --> true if "input" contain one element of "words_synonym"
input = input.tolowerCase();
console.log(words_synonym.some(word => input.includes(word)));
之后,我想用同一数组的随机元素(words_synonym)替换验证条件的“元素”。 但我无法选择此元素。我只是对或错
var random_word = words_synonym[Math.floor(Math.random() * (words_synonym.length))]
input = input.replace(element,random_word,0)
谢谢
按照您现在的方式,您正在检查是否有任何同义词与任何单词匹配(通过 words_synonym.some(word => input.includes(word))
)。为了做你想做的事,你需要目标词和新词的位置,你现在都没有。为此,您需要拆分嵌套循环。
代码 words_synonym.some(word => input.includes(word))
等价于:
let has_synonym = false;
for (word of words_synonym) { // this is a loop
if (input.includes(word)) { // this is also a loop
has_synonym = true;
break;
}
}
console.log(has_synonym);
因此,要解决您的主要问题,只需将 includes
替换为 indexOf
。
为了处理替换所有令牌的情况,我建议在循环外跟踪您已替换的令牌,否则您最终会多次替换每个令牌,这可能会变得非常昂贵。为此,只需跟踪循环外的起始位置,并使用替换词的结束索引增加它。 indexOf
已经为这个用例采用了 start
参数!
const input = "This is an amazing text blob where this word amazing is replaced by a random word from list_of_words. Isn't this amazing!";
const words_synonym = ["amazing","formidable","great","smart"];
let output = input;
let start = 0; // index of the end of the last replaced token
for (word of words_synonym) {
let index = output.indexOf(word,start);
while (index >= 0) {
const new_word = words_synonym[Math.floor(Math.random() * (words_synonym.length))];
output = output.substr(0,indeX) + new_word + output.substr(index + word.length,output.length);
start = index + new_word.length + 1; // increment the start
index = output.indexOf(word,start);
}
}
console.log("input: ",input);
console.log("output: ",output);
,
您可以使用方法find
:
words_synonym.find(word => input.includes(word))
哪个返回
数组中满足第一个元素的值 提供测试功能。否则返回 undefined。
来自文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
,我修改了 dantiston 的答案,并且包含一个循环以更改所有匹配“words_synonym”的单词。
但是有一个问题。程序不会检查“words_synonym”的所有单词,而只检查第一个带有 indexof 的单词。
var input = "This is an amazing text blob where this word amazing is replaced by a random word from list_of_words. Isn't this amazing!";
words_synonym = ["amazing","smart"];
let output = input;
for (word of words_synonym) {
let index = output.indexOf(word);
if (index >= 0) {
console.log(word);
var indexes = [],i = -1;
while ((i = output.indexOf(word,i+1)) != -1){
index=output.indexOf(word,i);
var new_word = words_synonym[Math.floor(Math.random() * (words_synonym.length))];
output = output.substr(0,output.length);
}
}
}
console.log("input: ",output);
以上是大佬教程为你收集整理的节点 - 测试字符串是否包含数组元素用相同数组的随机元素替换他(同义词)全部内容,希望文章能够帮你解决节点 - 测试字符串是否包含数组元素用相同数组的随机元素替换他(同义词)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。