1

I have created a script to generate random sentences from words and phrases in an array. This works.

I want to make a paragraph from these random sentences. What I have done though is repeating the same sentence, rather than running the function again and again to create new sentences.

I think my error is in this part of the code.

const randParagraph = (len, end, words, wordLen) =>
[...Array(len)].map(() =>  addCommaAfter(fullSentWithEnd,sentLength(5,12)))
.join(' ') + (end || ' ');

fullSentWithEnd is the final function in generating the sentences.

const fullSentWithEnd = randSentence(ipsumText, sentLength(5,12), '.')

and addAfterComma is splitting the sentence to add a comma.

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(" ");

}

I thought in randParagraph the new array was saying run addCommaAfter and pass in fullSentWithEnd, and tell it to run a random number of times between 5 and 12. But now I am wondering if it is actually saying that, or if that is what is telling it to repeat the same result.

Would love some thoughts.

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)));

2
  • can you give code that can be run? Commented Sep 5, 2016 at 13:29
  • just added a snippet above @SufianSaory Commented Sep 5, 2016 at 13:34

2 Answers 2

1

I don't understand what exactly your code does, but I've got this little framework for generating random texts:

ucfirst = s => s[0].toUpperCase() + s.slice(1);

rand = (min, max) => min + Math.floor(Math.random() * (max - min));

sample = a => a[rand(0, a.length)];

times = (n, fn) => [...Array(n)].map(fn);

seq = (min, max, fn, sep) => times(rand(min, max), fn).join(sep);

// this will use random "words"

char = () => sample("abcdefghjiklmnopqrstuwvxyz");
word = seq(1, 10, char, '');

// this will use an array of predefined words

words = [
    'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'I', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do',
    'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all',
    'would', 'there', 'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make',
    'can', 'like', 'time', 'no', 'just', 'him', 'know', 'take', 'person', 'into', 'year', 'your', 'good', 'some', 'could',
    'them', 'see', 'other', 'than', 'then', 'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also', 'back', 'after',
    'use', 'two', 'how', 'our', 'work', 'first', 'well', 'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give',
    'day', 'most', 'us'];

word = () => sample(words)

phrase = () => seq(3, 10, word, ' ');

sent = () => seq(1, 10, phrase, ', ');

sentence = () => ucfirst(sent()) + sample('.?!');

paragraph = () => seq(1, 10, sentence, ' ');

text = () => seq(2, 20, paragraph, '\n\n');

console.log(text())

Sign up to request clarification or add additional context in comments.

4 Comments

my code creates sentences out of an assortment of phrases and words I have stored in an array. The idea is to generate ipsum text that has a flavour or suits a theme. Your code creates its own words from what I can see, using the letters of the alphabet randomly. My problem though is I haven't managed to get the sentences to be random through the paragraph. They repeat.
Just replace word in the above code with your own implementation (e.g. sample(array-of-words).
sorry @georg I don't follow. I'm still new at this. I'm not sure how you're suggesting I add it. I tried changing it for my array, but obviously didn't get it right
well, that is a much neater way to achieve a similar result. I'm looking through what you've done to understand it. Thanks for sharing. The only difference I see is that you have very few full stops. I'm looking to see how I would change that.
0

you are seeding the addCommaAfter func from randParagraph func with same sentence. instead change the seeding with random sentences like following, it will create paragraph with random sentences.

const randParagraph = (len, end, words, wordLen) =>
    [...Array(len)].map(() => addCommaAfter(randSentence(words, sentLength(5, 12), '.'), sentLength(5, 12)))
        .join(' ') + (end || ' ');

Full code:

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)));

3 Comments

right, I can see that now @sufian. Thanks. There a few "undefined" coming up. Would that just be because a parameter has nothing passed in to it.
yes, due to out of index issue in addCommaAfter function undefined was coming. now corrected.@garrethwills
I can see you added the if statement, so that if the random integer determining the length of the split was longer than the sentence, it becomes one less than the sentence length. Great,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.