我创建了一个脚本,用于从数组中的单词和短语生成随机句子。这很有效。
我想用这些随机句子做一个段落。我所做的虽然是重复相同的句子,而不是一次又一次地运行该函数来创建新的句子。
我认为我的错误出现在代码的这一部分。
const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() => addCommaAfter(fullSentWithEnd,sentLength(5,12)))
.join(' ') + (end || ' ');
fullSentWithEnd是生成句子的最终函数。
const fullSentWithEnd = randSentence(ipsumText, sentLength(5,12), '.')
并且addAfterComma正在拆分句子以添加逗号。
const addCommaAfter = (sentence, index) => {
word_split = sentence.split(" ");
word_split[index] = word_split[index]+",";
word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
return word_split.join(" ");
}
我认为在randParagraph中,新数组说运行addCommaAfter并传入fullSentWithEnd,并告诉它在5到12之间运行一次随机次数。但是现在我想知道它是否真的这么说,或者如果是什么告诉它重复相同的结果。
会喜欢一些想法。
const ipsumText = ["adventure", "endless youth", "dust", "iconic landmark", "spontaneous", "carefree", "selvedge","on the road", "open road", "stay true", "free spirit", "urban", "live on the edge", "the true wanderer", "vintage motorcyle", "american lifestyle", "epic landscape", "low slung denim", "naturaL"];
const randInt = (lower, upper) =>
Math.floor(Math.random() * (upper-lower)) + lower
const randWord = (words) => words[randInt(0, words.length)]
const randSentence = (words, len, end) =>
[...Array(len)].map(() => randWord(words)).join(' ') + (end || ' ')
const randWordWithEnd = (end) => randWord(ipsumText) + end
const randWordWithFullStop = randWordWithEnd('. ')
const randWordWithComma = randWordWithEnd(', ')
const sentLength = (min,max) => {return Math.floor(Math.random() * (max - min + 1)) + min;};
const fullSentWithEnd = randSentence(ipsumText, sentLength(5,12), '.')
const fullSentNoEnd = randSentence(ipsumText, sentLength(5,12))
const fullSentComposed = fullSentNoEnd + randWordWithFullStop
const addCommaAfter = (sentence, index) => {
word_split = sentence.split(" ");
word_split[index] = word_split[index]+",";
word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
return word_split.join(" ");
}
console.log(fullSentWithEnd)
console.log(" ");
console.log(addCommaAfter(fullSentWithEnd,sentLength(3,8)));
const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() => addCommaAfter(fullSentWithEnd,sentLength(5,12)))
.join(' ') + (end || ' ');
console.log(randParagraph(sentLength(5,8), '', ipsumText, sentLength(5,12)));
答案 0 :(得分:1)
我不明白你的代码究竟是做什么的,但是我已经有了这个用于生成随机文本的小框架:
tessellator.addVertex

答案 1 :(得分:0)
您正在使用相同的句子 randParagraph func 播种 addCommaAfter func 。而是用随机句子改变播种,如下所示,它将用随机句子创建段落。
const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() => addCommaAfter(randSentence(words, sentLength(5, 12), '.'), sentLength(5, 12)))
.join(' ') + (end || ' ');
完整代码:
const ipsumText = ["adventure", "endless youth", "dust", "iconic landmark", "spontaneous", "carefree", "selvedge", "on the road", "open road", "stay true", "free spirit", "urban", "live on the edge", "the true wanderer", "vintage motorcyle", "american lifestyle", "epic landscape", "low slung denim", "naturaL"];
const randInt = (lower, upper) =>
Math.floor(Math.random() * (upper - lower)) + lower
const randWord = (words) => words[randInt(0, words.length)]
const randSentence = (words, len, end) =>
[...Array(len)].map(() => randWord(words)).join(' ') + (end || ' ')
const randWordWithEnd = (end) => randWord(ipsumText) + end
const randWordWithFullStop = randWordWithEnd('. ')
const randWordWithComma = randWordWithEnd(', ')
const sentLength = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min; };
const fullSentWithEnd = randSentence(ipsumText, sentLength(5, 12), '.')
const fullSentNoEnd = randSentence(ipsumText, sentLength(5, 12))
const fullSentComposed = fullSentNoEnd + randWordWithFullStop
const addCommaAfter = (sentence, index) => {
word_split = sentence.split(" ");
if (index >= word_split.length) {
index = word_split.length - 1;
}
word_split[index] = word_split[index] + ",";
word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1);
return word_split.join(" ");
}
console.log(fullSentWithEnd)
console.log(" ");
console.log(addCommaAfter(fullSentWithEnd, sentLength(3, 8)));
const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() => addCommaAfter(randSentence(words, sentLength(5, 12), '.'), sentLength(5, 12)))
.join(' ') + (end || ' ');
console.log(randParagraph(sentLength(5, 8), '', ipsumText, sentLength(5, 12)));