-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGenerator.java
More file actions
64 lines (53 loc) · 1.63 KB
/
Copy pathTextGenerator.java
File metadata and controls
64 lines (53 loc) · 1.63 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
import java.io.File;
import java.io.IOException;
/**
* TextGenerator.java. Creates an order K Markov model of the supplied source
* text, and then outputs M characters generated according to the model.
*
* @author Adam Bostwick (azb0071@auburn.edu)
* @author Dean Hendrix (dh@auburn.edu)
* @version 2018-04-17
*
*/
public class TextGenerator {
/** Drives execution. */
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java TextGenerator k length input");
return;
}
// No error checking! You may want some, but it's not necessary.
int K = Integer.parseInt(args[0]);
int M = Integer.parseInt(args[1]);
if ((K < 0) || (M < 0)) {
System.out.println("Error: Both K and M must be non-negative.");
return;
}
File text;
try {
text = new File(args[2]);
if (!text.canRead()) {
throw new Exception();
}
}
catch (Exception e) {
System.out.println("Error: Could not open " + args[2] + ".");
return;
}
// instantiate a MarkovModel with the supplied parameters and
// generate sample output text ...
MarkovModel myMarkov = new MarkovModel(K, text);
String kGram;
String output = myMarkov.getRandomKgram();
for (int i = 0; i < M - 1; i++)
{
kGram = "";
for (int j = i; j < i + K; j++)
{
kGram += output.charAt(j);
}
output += myMarkov.getNextChar(kGram);
}
System.out.println(output);
}
}