2016-09-05 5 views
1

配列内の単語やフレーズからランダムな文を生成するためのスクリプトを作成しました。これは機能します。関数をjavascriptで無作為にループする

これらのランダムな文章から段落を作りたいと思います。私がやったことは、何度も何度も新しい文章を作成するのではなく、同じ文章を繰り返すことです。

私のエラーはコードのこの部分にあると思います。

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

}

私は新しい配列が実行addCommaAfterを言うとfullSentWithEndに渡したrandParagraphに考え、そして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) => { 
 
\t word_split = sentence.split(" "); 
 
\t word_split[index] = word_split[index]+","; 
 
\t word_split[0] = word_split[0][0].toUpperCase() + word_split[0].slice(1); 
 
\t 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

あなたが実行可能なコードを与えることができますか? –

+0

は@SufianSaoryの上にスニペットを追加しました – garrethwills

答えて

0

あなたは播種していますaddCommaAfter楽しいc randParagraph 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)));

+0

今、私はそれが@sufianであることが分かります。ありがとう。いくつかの "未定義"が登場します。それは単にパラメータに何も渡されていないからです。 – garrethwills

+0

はい、addCommaAfter関数のインデックスが不足しているため、関数undefinedが来ていました。今修正。@ –

+0

は、私は、分割の長さを決定するランダムな整数が文よりも長かった場合、それは文の長さよりも一つ少なくなるようにあなたは、if文を追加して見ることができますgarrethwills。すばらしいです、 – garrethwills

1

私は正確にあなたのコードが何をするのか理解していないが、私は、乱数発生テキストにこの小さなフレームワークを持っている:

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

+0

私のコードは、私が配列に格納したフレーズと単語の組み合わせから文を作成します。そのアイデアは、風味がある、またはテーマに合ったipsumテキストを生成することです。あなたのコードは、アルファベットの文字をランダムに使って、私が見ることのできる単語から自分自身の単語を作成します。私の問題は、私は文章を段落からランダムに得ることができなかったことです。彼らは繰り返す。 – garrethwills

+0

上記のコードの 'word'をあなた自身の実装に置き換えてください(例:' sample(array-of-words) ') – georg

+0

残念です@georg私はまだこれに触れていません。どのように私はそれを追加することを提案しています。私は配列のためにそれを変更しようとしましたが、明らかにそれを得ていない – garrethwills

関連する問題