-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
68 lines (54 loc) · 1.75 KB
/
Copy pathMain.java
File metadata and controls
68 lines (54 loc) · 1.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import javax.print.DocFlavor.STRING;
class Main {
public static void main(String[] args) {
// testCharacters("hello", 10);
testWords(new String[]{"hello"}, 100);
}
public static void testWords(String[] kernel, int length){
MarkovChain<String> mark = new MarkovChain<>();
File f = new File("text.txt");
Scanner s;
String str = "";
try {
s = new Scanner(f);
while (s.hasNextLine()) {
str += " \n" + s.nextLine();
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mark.fit(str.split(" "));
String genStr = "";
for (String string : mark.forward(kernel, length)){
genStr += " " + string;
}
System.out.println(genStr);
}
public static void testCharacters(String kernel, int length){
MarkovChain<Character> mark = new MarkovChain<>();
File f = new File("text.txt");
Scanner s;
String str = "";
try {
s = new Scanner(f);
while (s.hasNextLine()) {
str += " \n" + s.nextLine();
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mark.fit(str.chars().mapToObj(c -> (char)c).toArray(Character[]::new));
String genStr = "";
for (Character c : mark.forward(kernel.chars().mapToObj(c -> (char)c).toArray(Character[]::new), length)){
genStr += c;
}
System.out.println(genStr);
}
}