forked from DavidMatuszek/SentenceGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentenceGenerator.java
More file actions
executable file
·125 lines (116 loc) · 3.64 KB
/
SentenceGenerator.java
File metadata and controls
executable file
·125 lines (116 loc) · 3.64 KB
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
119
120
121
122
123
124
125
package sentenceGenerator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFileChooser;
/**
* This is my version of a CIT594 assignment to read in a BNF grammar
* and produce sentences from that grammar.
*/
public class SentenceGenerator {
private Grammar grammar;
private Random random = new Random();
/**
* Prompts the user for a file containing a BNF grammar, then
* generates several sentences from that grammar.
*
* @param args Unused.
*/
public static void main(String[] args) {
try {
new SentenceGenerator().run();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Does the work of this class.
*
* @throws IOException If an input exception occurs.
*/
private void run() throws IOException {
BufferedReader readLine = getFileReader();
grammar = new Grammar(readLine);
grammar.print();
for (int j = 0; j < 20; j++){
printAsSentence(generate("<sentence>"));
}
}
/**
* Expands the given term into a list of terminals. If the given
* term is already a terminal, a list containing this single term
* is returned.
*
* @param term A terminal or nonterminal to expand into a list.
* @return A list of terminals.
*/
List<String> generate(String term) {
List<String> result = new ArrayList<String>();
ListOfDefinitions listDef = grammar.getDefinitions(term);
SingleDefinition singleDef = (SingleDefinition) chooseRandomElement(listDef);
if (singleDef.get(0).charAt(0) == ('<')){
for(int i = 0; i < singleDef.size(); i++){
if (singleDef.get(0).charAt(0) == ('<')){
result.addAll(generate(singleDef.get(i)));
}
}
}
else {
result.addAll(singleDef);
}
return result;
}
/**
* Randomly choose and return one element from a list.
*
* @param list The list from which the selection is to be made.
* @return The randomly selected element.
*/
private Object chooseRandomElement(List list) {
return (list.get(random.nextInt(list.size())));
}
/**
* Prints the given list of words as a sentence. The first word is
* capitalized, and a period is printed at the end.
*
* @param list The words to be printed.
*/
void printAsSentence(List<String> list) {
System.out.printf("%s ", list.get(0).substring(0, 1).toUpperCase() + list.get(0).substring(1));
for (int x = 1; x < list.size(); x++){
if (x == list.size() -1){
System.out.printf("%s", list.get(x));
}
else{
System.out.printf("%s ", list.get(x));
}
}
System.out.print(".");
System.out.println("");
}
/**
* Prompts the user to choose a file, which should contain a BNF grammar.
*
* @return The chosen file, or <code>null</code> if none is chosen.
*/
private BufferedReader getFileReader() throws IOException{
BufferedReader reader = null;
String fileName;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
if(file != null){
fileName = file.getCanonicalPath();
reader = new BufferedReader(new FileReader(fileName));
}
}
return reader;
}
}