forked from jdarmoni/DuoJuo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar_graph.js
61 lines (53 loc) · 1.77 KB
/
grammar_graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var GrammarGraph = require('grammar-graph')
var grammar = {
Sentence: ['NounPhrase VerbPhrase'],
NounPhrase: ['the Noun',
'the Noun RelativeClause'],
VerbPhrase: ['Verb',
'Verb NounPhrase'],
RelativeClause: ['that VerbPhrase',
'who VerbPhrase',
],
Noun: ['dog',
'cat',
'bird',
'squirrel',
'boy'],
Verb: ['befriended',
'loved',
'ate',
'attacked',
'watched',
'found']
}
function random(guide){
return (Math.floor(Math.random() * ((guide.choices().length - 1) - 0 + 1)) + 0)
}
var graph = new GrammarGraph(grammar)
var guide = graph.createGuide('Sentence')
function createSentence(guide, num=3){
if (guide.construction().length >= num && guide.isComplete()) {
// console.log(guide.construction())
return guide.construction().join(' ')
} else {
guide.choose(guide.choices()[random(guide)])
return createSentence(guide, num)
}
}
console.log(createSentence(guide, 6))
// console.log(guide.choices()[Math.floor(Math.random() * (guide.choices.length - 0 + 1)) + 0] )
// guide.choose( guide.choices()[random(guide)])
// console.log(guide.choices())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// guide.choose(guide.choices()[random(guide)])
// console.log(guide.construction())
// console.log(guide.isComplete())