-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasicEnglish.java
86 lines (76 loc) · 1.95 KB
/
BasicEnglish.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
class BasicEnglish
{
int bits;
ArrayList<String> n, vi, vt, adj, adv, p, art;
public BasicEnglish(int b) {
bits = b;
try {
n = readFile("word-lists/basic-nouns");
vi = readFile("word-lists/basic-verbs-i");
vt = readFile("word-lists/basic-verbs-t");
adj = readFile("word-lists/basic-adjectives");
adv = readFile("word-lists/basic-adverbs");
p = readFile("word-lists/basic-prepositions");
art = readFile("word-lists/basic-articles");
} catch(Exception e) {
System.err.println(e);
System.exit(1);
}
}
ArrayList<String> readFile(String name) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(name));
ArrayList<String> words = new ArrayList<String>();
String word = in.readLine();
while(word != null) {
words.add(word);
word = in.readLine();
}
in.close();
return words;
}
int rand(int range) {
bits -= (int) (Math.log(range) / Math.log(2));
return (int) (Math.random() *(range));
}
boolean branch() {
if(bits > 0) return rand(2) == 0;
return false;
}
void printWord(ArrayList<String> array) {
System.out.print(array.get(rand(array.size())) + " ");
}
void printNounPhrase() {
printWord(art);
if(branch()) printWord(adj);
printWord(n);
}
void printSentence() {
printNounPhrase(); // Subject
if(branch()) {
printWord(vt); // Transitive verb
printNounPhrase(); // Object of transitive verb
} else {
printWord(vi); // Intransitive verb
}
if(branch()) printWord(adv); // Adverb
if(branch()) {
printWord(p); // Preposition
printNounPhrase(); // Object of preposition
}
System.out.println();
}
void printPoem() {
while(bits > 0) printSentence();
}
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("Usage: BasicEnglish <num_bits>");
return;
}
int b = Integer.parseInt(args[0]);
new BasicEnglish(b).printPoem();
}
}