CS1103 Programming Assignment Unit 2 - Generate random sentences based on a set of syntax rules

Questions

Write a program that generates random sentences based on a set of syntax rules

1. Codes

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* RecursiveSyntax generates random sentences based on a set of syntax rules.
* These rules include:
* <sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]
* <simple_sentence> ::= <noun_phrase> <verb_phrase>
* <noun_phrase> ::= <proper_noun> | <determiner> [ <adjective> ]. <common_noun> [ who <verb_phrase> ]
* <verb_phrase> ::= <intransitive_verb> | <transitive_verb> <noun_phrase> | is <adjective> | believes that <simple_sentence>
* <conjunction> ::= and | or | but | because
* <proper_noun> ::= Fred | Jane | Richard Nixon | Miss America
* <common_noun> ::= man | woman | fish | elephant | unicorn
* <determiner> ::= a | the | every | some
* <adjective> ::= big | tiny | pretty | bald
* <intransitive_verb> ::= runs | jumps | talks | sleeps
* <transitive_verb> ::= loves | hates | sees | knows | looks for | finds
*
* @author Zhiyao He
*/
public class RecursiveSyntax {
/**
* use arrays to implement the last seven rules
*/
static final String[] conjunctions = {"and", "or", "but", "because"};
static final String[] properNouns = {"Fred", "Jane", "Richard Nixon", "Miss America"};
static final String[] commonNouns = {"man", "woman", "fish", "elephant", "unicorn"};
static final String[] determiners = {"a", "the", "every", "some"};
static final String[] adjectives = {"big", "tiny", "pretty", "bald"};
static final String[] intransitiveVerbs = {"runs", "jumps", "talks", "sleeps"};
static final String[] transitiveVerbs = {"loves", "hates", "sees", "knows", "looks for", "finds"};

/**
* <sentence> ::= <simple_sentence> [ <conjunction> <sentence> ]
*/
static void randomSentence() {
randomSimpleSentence();
if (Math.random() > 0.6) {
System.out.print(" " + randomItem(conjunctions) + " ");
randomSentence();
}
}

/**
* <simple_sentence> ::= <noun_phrase> <verb_phrase>
*/
static void randomSimpleSentence() {
randomNounPhrase();
randomVerbPhrase();
}

/**
* <noun_phrase> ::= <proper_noun> | <determiner> [ <adjective> ]. <common_noun> [ who <verb_phrase> ]
* Brackets with dots means a repeated optional element, so "[ <adjective> ]." means a repeated optional adjective.
*/
static void randomNounPhrase() {
if (Math.random() > 0.5) {
System.out.print(randomItem(properNouns));
} else {
System.out.print(randomItem(determiners));

// "[ <adjective> ]." can be represented by a while loop
// The value range of randomRepeatedTimes is: [0, adjectives.length], here is [0, 4]
int randomRepeatedTimes = (int) (Math.random() * (adjectives.length + 1));
while (randomRepeatedTimes > 0) {
System.out.print(" " + adjectives[randomRepeatedTimes - 1]);
randomRepeatedTimes--;
}

System.out.print(" " + randomItem(commonNouns));
if (Math.random() > 0.75) {
System.out.print(" who");
randomVerbPhrase();
}
}
}

/**
* <verb_phrase> ::= <intransitive_verb> | <transitive_verb> <noun_phrase> | is <adjective> | believes that <simple_sentence>
*/
static void randomVerbPhrase() {
if (Math.random() > 0.75) {
System.out.print(" " + randomItem(intransitiveVerbs));
} else if (Math.random() > 0.65) {
System.out.print(" " + randomItem(transitiveVerbs) + " ");
randomNounPhrase();
} else if (Math.random() > 0.15) {
System.out.print(" is");
System.out.print(" " + randomItem(adjectives));
} else {
System.out.print(" believes that ");
randomSimpleSentence();
}
}

/**
* pick a random item from an array of strings
*/
static String randomItem(String[] listOfStrings) {
int i = (int) (Math.random() * listOfStrings.length);
return listOfStrings[i];
}

/**
* Generates and outputs one random sentence every three seconds until
* it is halted (for example, by typing Control-C in the terminal window where it is
* running).
*
* @param args
*/
public static void main(String[] args) {
while (true) {
randomSentence();
System.out.println(".\n\n");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
}
}

2. Outputs

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
Miss America is bald.


some tiny big unicorn who jumps runs but some pretty tiny big woman jumps but Miss America is big.


the bald pretty tiny big elephant is big because Richard Nixon is bald and every big unicorn finds Jane.


some big elephant who runs loves Fred and Fred finds a tiny big fish.


every fish is big.


every tiny big elephant sleeps.


the bald pretty tiny big elephant who is big is pretty.


the tiny big unicorn is bald.


the pretty tiny big woman sleeps.


a big unicorn looks for the woman but every pretty tiny big elephant who is big is big.


every man hates the tiny big elephant or a tiny big fish sleeps but every pretty tiny big man is bald.


Richard Nixon is big.


some bald pretty tiny big unicorn who runs talks.


Fred finds a unicorn who finds every pretty tiny big unicorn.



Process finished with exit code 130

​


This is the ending, thanks for reading.