├── README.md
└── Markov
├── bin
├── Load.class
├── Word.class
├── Generator.class
├── Manager.class
└── Sentance.class
├── res
└── text.txt
├── src
├── Word.java
├── Sentance.java
├── Load.java
├── Manager.java
└── Generator.java
├── .classpath
├── .project
└── .settings
└── org.eclipse.jdt.core.prefs
/README.md:
--------------------------------------------------------------------------------
1 | # markov-chain-text-generator
2 | Creates random text using a markov chain generator
3 |
--------------------------------------------------------------------------------
/Markov/bin/Load.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/bin/Load.class
--------------------------------------------------------------------------------
/Markov/bin/Word.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/bin/Word.class
--------------------------------------------------------------------------------
/Markov/res/text.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/res/text.txt
--------------------------------------------------------------------------------
/Markov/bin/Generator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/bin/Generator.class
--------------------------------------------------------------------------------
/Markov/bin/Manager.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/bin/Manager.class
--------------------------------------------------------------------------------
/Markov/bin/Sentance.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jake100/markov-chain-text-generator/HEAD/Markov/bin/Sentance.class
--------------------------------------------------------------------------------
/Markov/src/Word.java:
--------------------------------------------------------------------------------
1 |
2 | public class Word {
3 | public String s;
4 | public int num;
5 | public Word(String s)
6 | {
7 | this.s = s;
8 | this.num = 1;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Markov/src/Sentance.java:
--------------------------------------------------------------------------------
1 |
2 | public class Sentance {
3 | public String s;
4 | public Word[] words;
5 | public Sentance(String s, Word[] words)
6 | {
7 | this.s = s;
8 | this.words = words;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Markov/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Markov/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Markov
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Markov/src/Load.java:
--------------------------------------------------------------------------------
1 | import java.io.File;
2 | import java.io.FileNotFoundException;
3 | import java.util.Scanner;
4 |
5 | public class Load {
6 | public static String load(String path)
7 | {
8 | Scanner scanner = null;
9 | try {
10 | scanner = new Scanner(new File(path));
11 | } catch (FileNotFoundException e) {
12 | // TODO Auto-generated catch block
13 | e.printStackTrace();
14 | }
15 | String text = scanner.useDelimiter("\\A").next();
16 | scanner.close();
17 | return text;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Markov/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/Markov/src/Manager.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | public class Manager {
4 | int numSentances = 10;
5 | Generator generator = new Generator();
6 | String[] strings = new String[numSentances];
7 | public Manager()
8 | {
9 | String text = Load.load("res/text.txt");
10 | for (int i = 0; i < numSentances; i++) {
11 | strings[i] = generator.generate(text);
12 | }
13 | manage();
14 | for (int i = 0; i < strings.length; i++) {
15 | System.out.println(strings[i]);
16 | }
17 | }
18 | public void manage()
19 | {
20 | for (int i = 0; i < strings.length; i++) {
21 | String string = strings[i];
22 | if(!(string.endsWith("the") || string.endsWith("and")
23 | || string.endsWith("take") || string.endsWith("your")
24 | || string.endsWith("*is*")|| string.endsWith("each")
25 | || string.endsWith("they")|| string.endsWith("like")
26 | || string.endsWith("in")|| string.endsWith("my")
27 | || string.endsWith("his")|| string.endsWith("our")
28 | || string.endsWith("a")|| string.endsWith("she's")
29 | || string.endsWith("he")|| string.endsWith(" ")
30 | || string.endsWith("she")|| string.endsWith("it")
31 | || string.endsWith("but")|| string.endsWith("you're")
32 | || string.endsWith("on")|| string.endsWith("or")
33 | || string.endsWith("introduce")|| string.endsWith("you")
34 | || string.endsWith("that's")|| string.endsWith("their")
35 | || string.endsWith("ain't")|| string.endsWith("start")
36 | || string.endsWith("a Go")|| string.endsWith("a go")
37 | || string.endsWith("of")|| string.endsWith("just")
38 | || string.endsWith("not")|| string.endsWith("if")
39 | || string.endsWith("than")|| string.endsWith("are")
40 | || string.endsWith("at")|| string.endsWith("to")
41 | || string.endsWith("is")|| string.endsWith(",")
42 | || string.endsWith(":")|| string.endsWith(";")
43 | || string.endsWith("I'll get")|| string.endsWith("You'll get")
44 | || string.endsWith("Don't be")|| string.endsWith("You're totally")
45 | || string.matches("^\\w*[\\.\\?\\!]")) || string.matches("[A-Z]+[a-z']+[\\.\\?\\!]*$"))
46 | {
47 | if(!string.matches("[\\.\\?\\!\\,]$"))
48 | {
49 | string = string + ".";
50 | }
51 | if(string.startsWith(" "))string = string.substring(1);
52 | if(string.startsWith(" "))string = string.substring(1);
53 | if(string.startsWith(" "))string = string.substring(1);
54 | strings[i] = string.substring(0, 1).toUpperCase() + string.substring(1);
55 | }
56 | else
57 | {
58 | strings[i] = "\"" + string +"\"";
59 | }
60 | }
61 | }
62 | public static void main(String[] args)
63 | {
64 | new Manager();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/Markov/src/Generator.java:
--------------------------------------------------------------------------------
1 | import java.io.File;
2 | import java.io.FileNotFoundException;
3 | import java.io.IOException;
4 | import java.nio.charset.Charset;
5 | import java.nio.charset.StandardCharsets;
6 | import java.nio.file.Files;
7 | import java.nio.file.Paths;
8 | import java.util.ArrayList;
9 | import java.util.Enumeration;
10 | import java.util.Hashtable;
11 | import java.util.Map;
12 | import java.util.Map.Entry;
13 | import java.util.Random;
14 | import java.util.Scanner;
15 |
16 | public class Generator {
17 | String previous = "";
18 | String curWord = "";
19 | int numWordsMin = 2;
20 | int numWordsMax = 8;
21 | Random rnd = new Random();
22 | public Hashtable> pairs = new Hashtable>();
23 | public Generator()
24 | {
25 |
26 | }
27 | public String generate(String string)
28 | {
29 | String generatedText = "";
30 | createTable(string);
31 | int numWords = randomizeNumWords();
32 | for (int i = 0; i < numWords; i++) {
33 | if(generatedText.equals(""))
34 | {
35 | ArrayList keys = new ArrayList(pairs.keySet());
36 |
37 | curWord = keys.get(rnd.nextInt(keys.size()));
38 | generatedText += curWord;
39 |
40 | }
41 | else
42 | {
43 | generatedText += " ";
44 | ArrayList words = pairs.get(curWord);
45 | double[] values = new double[words.size()];
46 | for (int j = 0; j < values.length; j++) {
47 | values[j] = words.get(j).num;
48 | }
49 | if(values.length != 0)curWord = words.get(rouletteSelect(values)).s;
50 | else
51 | {
52 | String[] wordValues = (String[]) pairs.values().toArray();
53 | curWord = wordValues[rnd.nextInt(values.length)];
54 | }
55 | generatedText += curWord;
56 | }
57 | }
58 | return generatedText;
59 | }
60 | public static void main(String[] args)
61 | {
62 | new Generator();
63 | }
64 | public int rouletteSelect(double[] weight) {
65 | double weight_sum = 0;
66 | for(int i = 0; i < weight.length; i++) {
67 | weight_sum += weight[i];
68 | }
69 | double value = rnd.nextDouble() * weight_sum;
70 | for(int i = 0; i < weight.length; i++) {
71 | value -= weight[i];
72 | if(value <= 0) return i;
73 | }
74 | return weight.length - 1;
75 | }
76 | public int randomizeNumWords()
77 | {
78 | return rnd.nextInt((numWordsMax - numWordsMin) + 1) + numWordsMin;
79 | }
80 | public void createTable(String string)
81 | {
82 | String[] words = string.split(" ");
83 | for (int i = 0; i < words.length; i++) {
84 | if(!pairs.containsKey(words[i]))
85 | {
86 | pairs.put(words[i], new ArrayList());
87 | }
88 | ArrayList w = new ArrayList();
89 | if(pairs.containsKey(previous))
90 | {
91 | w = pairs.get(previous);
92 | boolean found = false;
93 | for (int j = 0; j < w.size(); j++) {
94 | if(w.get(j).s.equals(words[i])){
95 | w.get(j).num++;
96 | found = true;
97 | }
98 | }
99 | if(!found)w.add(new Word(words[i]));
100 | }
101 | pairs.remove(previous);
102 | pairs.put(previous, w);
103 | previous = words[i];
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------