-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextGenerator.java
265 lines (248 loc) · 7.68 KB
/
TextGenerator.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/** Implementation of Text Generator.
* @author: Rafayel Mkrtchyan
* @mm/dd/yyyy: 03/11/2015
**/
import java.io.File;
import java.util.Random;
import java.util.Scanner;
import java.util.HashMap;
import java.util.TreeMap;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
public class TextGenerator extends Generator {
TextGenerator(String path) {
this._path = path;
}
@Override
public TreeMap<String, ArrayList<WordTuple>> saveToMap()
throws IOException {
File file = new File(this._path);
TreeMap<String, ArrayList<WordTuple>> data =
new TreeMap<String, ArrayList<WordTuple>>();
Scanner scannedFile = new Scanner(new FileReader(file));
BufferedReader br = new BufferedReader(new FileReader(this._path));
if (br.readLine() == null) {
System.out.printf("EROROR 001: The given file is empty.\n");
return null;
}
String predecessor;
predecessor = scannedFile.next();
if (!scannedFile.hasNext()) {
ArrayList<WordTuple> uniqueArray = new ArrayList<WordTuple>();
WordTuple onlyTuple = new WordTuple(null, 1);
uniqueArray.add(onlyTuple);
data.put(predecessor, uniqueArray);
return data;
}
while(scannedFile.hasNext()) {
String successor;
successor = scannedFile.next();
_lastWord = successor;
WordTuple currentTuple = new WordTuple(successor, 1);
if (!data.containsKey(predecessor)) {
ArrayList<WordTuple> currentArray = new ArrayList<WordTuple>();
currentArray.add(currentTuple);
data.put(predecessor, currentArray);
} else {
boolean checker = true;
ArrayList<WordTuple> currentList = data.get(predecessor);
for (WordTuple tup : currentList) {
if (tup.getword().equals(successor)) {
tup.setNumber(tup.getNumber() + 1);
checker = false;
break;
}
}
if (checker) {
data.get(predecessor).add(currentTuple);
}
}
predecessor = successor;
}
return data;
}
@Override
public ArrayList<Triple> saveToList() throws IOException {
ArrayList<Triple> sortedData= new ArrayList<Triple>();
TreeMap<String, ArrayList<WordTuple>> currentMap = this.saveToMap();
if (currentMap == null) {
System.out.printf("EROROR 002: The given TreeMap is empty.\n");
return null;
}
for (String item : currentMap.keySet()) {
ArrayList<WordTuple> currentList = currentMap.get(item);
Double overallCount = 0.0;
for (WordTuple tup : currentList) {
Double num = tup.getNumber() / 1.0;
overallCount += num;
}
for (WordTuple tup : currentList) {
String successor = tup.getword();
Double num = tup.getNumber() / 1.0;
Double percentage;
percentage = this.round((num * 100.0) / overallCount, 2);
Triple currentTriple = new Triple(item, successor, percentage);
sortedData.add(currentTriple);
}
}
return sortedData;
}
/** Rounds number NUMBER to POSITIONS decimal points. **/
private static double round(double number, int positions) {
if (positions < 0) {
throw new IllegalArgumentException();
} else {
BigDecimal num = new BigDecimal(number);
num = num.setScale(positions, RoundingMode.HALF_UP);
return num.doubleValue();
}
}
/** Random number generator that generates number between lower_bound
* and upper_bound inclusivelu. **/
private int randomNum(int lower_bound, int upper_bound) {
Random number = new Random();
int result = number.nextInt((upper_bound - lower_bound) + 1) + lower_bound;
return result;
}
@Override
public TreeMap<String, Stats> textToData() throws IOException {
TreeMap<String, Stats> data = new TreeMap<String, Stats>();
if (this.saveToMap() == null) {
return null;
}
File file = new File(this._path);
Scanner scannedFile = new Scanner(new FileReader(file));
if (scannedFile.hasNext()) {
String firstWord = scannedFile.next();
WordTuple currentTuple = new WordTuple(firstWord, 0);
ArrayList<WordTuple> current = new ArrayList<WordTuple>();
current.add(currentTuple);
Stats curreStats = new Stats(current, 1);
data.put("$", curreStats);
}
TreeMap<String, ArrayList<WordTuple>> map = this.saveToMap();
int maxValue = 0;
int minValue = 10000;
for (String item : map.keySet()) {
String key = item;
ArrayList<WordTuple> myList = map.get(item);
int sigma = 0;
int value = 0;
ArrayList<WordTuple> newList = new ArrayList<WordTuple>();
for (WordTuple tup : myList) {
sigma += tup.getNumber();
String predecessor = tup.getword();
int counter = tup.getNumber();
if (counter > maxValue) {
_mostCommonWord = predecessor;
maxValue = counter;
}
if (counter < minValue) {
_leastCommonWord = predecessor;
minValue = counter;
}
while (counter > 0) {
WordTuple update = new WordTuple(predecessor, value);
newList.add(update);
value += 1;
counter -= 1;
}
}
Stats currentStats = new Stats(newList, sigma);
data.put(key, currentStats);
}
ArrayList<WordTuple> finalList = new ArrayList<WordTuple>();
finalList.add(new WordTuple(".", 1));
Stats newStats = new Stats(finalList, 1);
data.put(_lastWord,newStats);
return data;
}
@Override
public void generateText(long length) throws IOException {
clearContent();
TreeMap<String, Stats> data = this.textToData();
File file = new File(_path);
Scanner scanned = new Scanner(new FileReader(file));
String currentWord;
if (length < 0) {
System.out.println("Error 004: length cannot be less than 0");
} else {
long lengthCounter = 1;
try {
String res = _mostCommonWord.substring(0, 1).toUpperCase() +
_mostCommonWord.substring(1);
writeToFile(res, OUTFILE);
} catch (IOException exp) { }
while (scanned.hasNext()) {
if (lengthCounter == length) {
break;
}
currentWord = scanned.next();
Stats currentStats = data.get(currentWord);
int minimum_bound = 0;
int sigma = currentStats.getSigma();
int random = randomNum(0, sigma - 1);
ArrayList<WordTuple> currentArray = currentStats.getArray();
String finalString = "";
for (WordTuple tup : currentArray) {
long number = tup.getNumber();
if (number == random) {
finalString = tup.getword();
}
}
String outputtedWord = " " + finalString;
try {
writeToFile(outputtedWord, OUTFILE);
} catch (IOException exp) { }
lengthCounter += 1;
}
if (lengthCounter < length) {
try {
writeToFile(_leastCommonWord, OUTFILE);
} catch (IOException exp) { }
}
}
}
/** Clears the content of the output file for the next call. **/
private void clearContent() {
try {
PrintWriter writer = new PrintWriter(OUTFILE);
writer.print("");
writer.close();
} catch (java.io.FileNotFoundException exp) {
}
}
/** Writes the given string TEXT into a given file-path FILENAME. **/
private void writeToFile(String text, String filename) throws IOException {
BufferedWriter output = null;
try {
FileWriter stream = new FileWriter(OUTFILE, true);
output = new BufferedWriter(stream);
output.write(text);
} catch (IOException exp) {
System.err.printf("Error 003: Output file does not exist.");
} finally {
if (output != null) {
output.close();
}
}
}
/** Contains information about the input file.**/
private String _path;
/** Contains the name of the output file. **/
private final String OUTFILE = "output.txt";
/** Saves the last word of the given text. **/
private String _lastWord;
/** Saves the most commond word in the text. **/
private String _mostCommonWord;
/** Saves the least common word in the text. **/
private String _leastCommonWord;
}