From 0ea49dd4a52358f053c9be7138c392b16de05899 Mon Sep 17 00:00:00 2001 From: student Date: Fri, 17 Mar 2017 10:49:27 -0400 Subject: Move things around, and start on new parser. --- .../rgens/text/markov/StandaloneTextGenerator.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java (limited to 'RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java') diff --git a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java new file mode 100644 index 0000000..92bc653 --- /dev/null +++ b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java @@ -0,0 +1,70 @@ +package bjc.rgens.text.markov; + +import java.io.IOException; +import java.io.Reader; +import java.util.HashMap; +import java.util.Map; + +public class StandaloneTextGenerator { + + /** + * Build a markov generator from a provided source + * + * @param k + * The markov order to use + * @param reader + * The source to seed the generator from + * @return The markov generator for the provided text + */ + public static StandaloneMarkov generateMarkovMap(int k, + Reader reader) { + Map hash = new HashMap<>(); + + Character next = null; + + try { + next = (char) reader.read(); + } catch (IOException e1) { + System.out + .println("IOException in stepping through the reader"); + e1.printStackTrace(); + System.exit(1); + } + + StringBuilder origFileBuffer = new StringBuilder(); + + while (next != null && Character.isDefined(next)) { + Character.toString(next); + origFileBuffer.append(next); + + try { + next = (char) reader.read(); + } catch (IOException e) { + System.out.println( + "IOException in stepping through the reader"); + e.printStackTrace(); + } + + } + + String origFile = origFileBuffer.toString(); + String firstSub = origFile.substring(0, k); + + for (int i = 0; i < origFile.length() - k; i++) { + String sub = origFile.substring(i, i + k); + Character suffix = origFile.charAt(i + k); + + if (hash.containsKey(sub)) { + Markov marvin = hash.get(sub); + marvin.add(suffix); + hash.put(sub, marvin); + } else { + Markov marvin = new Markov(sub, suffix); + hash.put(sub, marvin); + } + } + + return new StandaloneMarkov(k, hash, firstSub); + } + +} -- cgit v1.2.3