-
Notifications
You must be signed in to change notification settings - Fork 5
/
Markov.java
109 lines (90 loc) · 2.87 KB
/
Markov.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
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.stream.Collectors;
public class Markov {
private static final Integer MAX_WORDS = 10_000;
public static void main(String[] args) throws IOException {
Chain chain = new Chain();
chain.build(System.in);
chain.generate(MAX_WORDS);
}
}
class Chain {
private static final Integer PREFIX_SIZE = 2;
private static final String NON_WORD = "\n";
private Map<Prefix, Vector<String>> statetab = new HashMap<>();
private Prefix prefix = Prefix.from(PREFIX_SIZE, NON_WORD);
private Random rand = new Random();
public void build(InputStream in) throws IOException {
Reader r = new BufferedReader(new InputStreamReader(in));
StreamTokenizer st = new StreamTokenizer(r);
st.resetSyntax();
st.wordChars(0, Character.MAX_VALUE);
st.whitespaceChars(0, ' ');
while (st.nextToken() != StreamTokenizer.TT_EOF)
this.add(st.sval);
this.add(NON_WORD);
}
public void generate(int words) {
this.prefix = Prefix.from(PREFIX_SIZE, NON_WORD);
for (int i = 0; i < words; i++) {
Vector<String> s = statetab.get(prefix);
Integer r = Math.abs(rand.nextInt() % s.size());
String suf = s.elementAt(r);
if (suf.equals(NON_WORD))
break;
System.out.print(suf + " ");
prefix.pref[0] = prefix.pref[1];
prefix.pref[1] = suf;
}
}
public void add(String word) {
Vector<String> suf = statetab.get(prefix);
if (suf == null) {
suf = new Vector<>();
statetab.put(prefix.clone(), suf);
}
suf.addElement(word);
prefix.pref[0] = prefix.pref[1];
prefix.pref[1] = word;
}
}
class Prefix {
private static final int MULTIPLIER = 31;
public String[] pref;
public static Prefix from(Integer size, String value) {
Prefix p = new Prefix();
p.pref = new String[size];
for (int i = 0; i < size; i++)
p.pref[i] = value;
return p;
}
public Prefix clone() {
Prefix n = new Prefix();
n.pref = this.pref.clone();
return n;
}
public int hashCode() {
int h = 0;
for (int i = 0; i < pref.length; i++)
h = MULTIPLIER * h + pref[i].hashCode();
return h;
}
public boolean equals(Object o) {
Prefix p = (Prefix) o;
for (int i = 0; i < pref.length; i++)
if (!this.pref[i].equals(p.pref[i]))
return false;
return true;
}
}