-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextGenerator.java
116 lines (109 loc) · 3.3 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
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class TextGenerator {
/**
* @param args when used with three arguments, the first represents the k-order of the Markov objects.
* The second represents the number of characters to print out. The third represents the file to be read.
*
* When used with two arguments, the first represents the k-order of the Markov objects, and
* the second represents the file to be read. The generated text will be the same number of characters
* as the original file.
*/
public static void main(String[] args) {
int k = 0;
int M = 0;
String file = "";
StringBuilder text = new StringBuilder();
if (args.length == 3) {
k = Integer.parseInt(args[0]);
M = Integer.parseInt(args[1]);
file = args[2];
}
else if (args.length == 2) {
k = Integer.parseInt(args[0]);
file = args[1];
}
else {
System.out.println("\n"+"Usage: java TextGenerator k M file");
System.out.println("where k is the markov order, M is the number");
System.out.println("of characters to be printed, and file is the");
System.out.println("name of the file to print from. M may be left out."+"\n");
System.exit(1);
}
FileReader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
}
MyHashMap<String, Markov> hash = new MyHashMap<String, Markov>();
Character next = null;
try {
next = (char) reader.read();
} catch (IOException e1) {
System.out.println("IOException in stepping through the file");
e1.printStackTrace();
}
StringBuilder origFileBuffer = new StringBuilder();
while (Character.isDefined(next)) {
Character.toString(next);
origFileBuffer.append(next);
try {
next = (char) reader.read();
} catch (IOException e) {
System.out.println("IOException in stepping through the file");
e.printStackTrace();
}
}
String origFile = origFileBuffer.toString();
String firstSub = origFile.substring(0, k);
for (int i=0; i<origFile.length()-k; i++) {
String sub = origFile.substring(i,i+k);
Character suffix = origFile.charAt(i+k);
if (hash.containsKey(sub)) {
Markov marvin = hash.get(sub);
marvin.add(suffix);
hash.put(sub, marvin);
}
else {
Markov marvin = new Markov(sub, suffix);
hash.put(sub, marvin);
}
}
if (M == 0)
M = origFile.length();
for (int i=k; i<M; i++) {
if (i==k) {
text.append(firstSub);
if (text.length() > k)
i=text.length();
}
String sub = text.substring((i-k),(i));
Markov tmp = hash.get(sub);
if (tmp!=null) {
Character nextChar = tmp.random();
text.append(nextChar);
}
else {
i = k-1;
}
}
if (hash.size() < 100) {
Iterator<String> keys = hash.keys();
while (keys.hasNext()) {
String hashKey = keys.next();
Markov hashValue = hash.get(hashKey);
System.out.print(hashValue.count()+" "+hashKey+":");
for (Entry<Character, Integer> entry : hashValue.getMap().entrySet()) {
char suffix = entry.getKey();
int frequencyCount = entry.getValue();
System.out.print(" "+frequencyCount+" "+suffix);
}
System.out.println();
}
}
System.out.println(text.toString().substring(0, Math.min(M, text.length())));
}
}