-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomText.java
More file actions
95 lines (86 loc) · 2.75 KB
/
RandomText.java
File metadata and controls
95 lines (86 loc) · 2.75 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
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import javafx.util.Pair;
import java.util.Scanner;
public class RandomText {
final private Map<Pair<String,String>,Map<String,Integer>> data;
public RandomText() {
data = new HashMap<>();
}
public void readFile(String fname) {
try{
String prefix1 = "I";
String prefix2 = "am";
Scanner keyboard = new Scanner("StarTrek.txt");
while (true) {
Pair<String,String> pair = new Pair(prefix1, prefix2);
if (!data.containsKey(pair)) {
data.put(pair, new HashMap<>());
}
Map<String,Integer> map = data.get(pair);
String suffix = keyboard.next();
map.put(suffix, map.containsKey(suffix) ? map.get(suffix) + 1 : 1);
// prefix1 = prefix2;
// prefix2 = suffix;
//Pair<String,String> pair = new Pair(prefix1, prefix2);
// if (!data.containsKey(pair)) {
// data.put(pair, new HashMap<>());
// }
// Map<String,Integer> map = data.get(pair);
map.put("", 1);
// keyboard.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String generate() {
return generate(0);
}
public String generate(int limit) {
Random random = new Random();
String res = "";
String prefix1 = "";
String prefix2 = "";
int words = 0;
while (true) {
Pair<String,String> pair = new Pair(prefix1, prefix2);
String suffix = "";
if (data.containsKey(pair)) {
Map<String,Integer> map = data.get(pair);
int n = 0;
for (int w : map.values()) {
n += w;
}
int i = random.nextInt(n);
for (Entry<String,Integer> entry : map.entrySet()) {
i -= entry.getValue();
if (i < 0) {
suffix = entry.getKey();
break;
}
}
}
if (suffix.equals("")) {
break;
}
res += res.equals("") ? suffix : " " + suffix;
words++;
if (limit > 0 && words == limit) {
break;
}
prefix1 = prefix2;
prefix2 = suffix;
}
return res;
}
public static void main(String[] args) {
RandomText randomText = new RandomText();
randomText.readFile("StarTrek.txt");
System.out.println(randomText.generate());
}
}