-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGladLib.java
164 lines (139 loc) · 3.89 KB
/
GladLib.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import edu.duke.*;
import java.util.*;
public class GladLib {
private ArrayList<String> adjectiveList;
private ArrayList<String> nounList;
private ArrayList<String> colorList;
private ArrayList<String> countryList;
private ArrayList<String> nameList;
private ArrayList<String> animalList;
private ArrayList<String> timeList;
private ArrayList<String> verbList;
private ArrayList<String> fruitList;
private ArrayList<String> usedWords;
private Random myRandom;
private static String dataSourceURL = "http://dukelearntoprogram.com/course3/data";
private static String dataSourceDirectory = "data";
public GladLib(){
initializeFromSource(dataSourceDirectory);
myRandom = new Random();
}
public GladLib(String source){
initializeFromSource(source);
myRandom = new Random();
}
private void initializeFromSource(String source) {
adjectiveList= readIt(source+"/adjective.txt");
nounList = readIt(source+"/noun.txt");
colorList = readIt(source+"/color.txt");
countryList = readIt(source+"/country.txt");
nameList = readIt(source+"/name.txt");
animalList = readIt(source+"/animal.txt");
timeList = readIt(source+"/timeframe.txt");
verbList = readIt(source+ "/verb.txt");
fruitList = readIt(source+ "/fruit.txt");
usedWords = new ArrayList<String>();
}
private ArrayList<String> readIt(String source){
ArrayList<String> list = new ArrayList<String>();
if (source.startsWith("http")) {
URLResource resource = new URLResource(source);
for(String line : resource.lines()){
list.add(line);
}
}
else {
FileResource resource = new FileResource(source);
for(String line : resource.lines()){
list.add(line);
}
}
return list;
}
private String randomFrom(ArrayList<String> source){
int index = myRandom.nextInt(source.size());
return source.get(index);
}
private String getSubstitute(String label) {
if (label.equals("country")) {
return randomFrom(countryList);
}
if (label.equals("color")){
return randomFrom(colorList);
}
if (label.equals("noun")){
return randomFrom(nounList);
}
if (label.equals("name")){
return randomFrom(nameList);
}
if (label.equals("adjective")){
return randomFrom(adjectiveList);
}
if (label.equals("animal")){
return randomFrom(animalList);
}
if (label.equals("timeframe")){
return randomFrom(timeList);
}
if (label.equals("fruit")){
return randomFrom(fruitList);
}
if (label.equals("verb")){
return randomFrom(verbList);
}
if (label.equals("number")){
return ""+myRandom.nextInt(50)+5;
}
return "**UNKNOWN**";
}
private String processWord(String w){
int first = w.indexOf("<");
int last = w.indexOf(">",first);
if (first == -1 || last == -1){
return w;
}
String prefix = w.substring(0,first);
String suffix = w.substring(last+1);
String sub = "";
if (sub == "" || usedWords.indexOf(sub) != -1){
sub = getSubstitute(w.substring(first+1,last));
}
usedWords.add(sub);
String newWord = prefix+sub+suffix;
return newWord;
}
private void printOut(String s, int lineWidth){
int charsWritten = 0;
for(String w : s.split("\\s+")){
if (charsWritten + w.length() > lineWidth){
System.out.println();
charsWritten = 0;
}
System.out.print(w+" ");
charsWritten += w.length() + 1;
}
}
private String fromTemplate(String source){
String story = "";
if (source.startsWith("http")) {
URLResource resource = new URLResource(source);
for(String word : resource.words()){
story = story + processWord(word) + " ";
}
}
else {
FileResource resource = new FileResource(source);
for(String word : resource.words()){
story = story + processWord(word) + " ";
}
}
return story;
}
public void makeStory(){
System.out.println("\n");
String story = fromTemplate("data/madtemplate2.txt");
//System.out.println(story);
printOut(story, 60);
}
}