diff options
| author | Ben Culkin <scorpress@gmail.com> | 2021-03-13 07:09:38 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2021-03-13 07:09:38 -0500 |
| commit | 768aacdf6e02ad39b8a26a88cb64670235378e1a (patch) | |
| tree | c565d859305a2f4342a2b7fe060247769c863137 | |
| parent | 223dc6d10617d282204c515cf3794d59a31ebf16 (diff) | |
Move factory method from StandaloneTextGenerator
It is now in StandaloneMarkov, which is the produced type anyways. This
also allows the removal of the StandaloneTextGenerator file, as it no
longer contains anything
3 files changed, 66 insertions, 78 deletions
diff --git a/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java b/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java index cebf2bc..78db77f 100755 --- a/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java +++ b/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java @@ -1,6 +1,7 @@ package bjc.rgens.text.markov; -import java.util.Map; +import java.io.*; +import java.util.*; /** * A standalone Markov generator. @@ -67,4 +68,67 @@ public class StandaloneMarkov { return text.toString(); } + + /** + * Build a markov generator from a provided source. + * + * @param order + * 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 order, Reader reader) { + Map<String, Markov> 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, order); + + for (int i = 0; i < origFile.length() - order; i++) { + String sub = origFile.substring(i, i + order); + Character suffix = origFile.charAt(i + order); + + 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(order, hash, firstSub); + } } diff --git a/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java b/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java deleted file mode 100755 index 339e8d5..0000000 --- a/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java +++ /dev/null @@ -1,76 +0,0 @@ -package bjc.rgens.text.markov; - -import java.io.IOException; -import java.io.Reader; -import java.util.HashMap; -import java.util.Map; - -/** - * Create a Markov generate from a provided source. - * - * @author bjculkin - */ -public class StandaloneTextGenerator { - /** - * Build a markov generator from a provided source. - * - * @param order - * 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 order, Reader reader) { - Map<String, Markov> 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, order); - - for (int i = 0; i < origFile.length() - order; i++) { - String sub = origFile.substring(i, i + order); - Character suffix = origFile.charAt(i + order); - - 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(order, hash, firstSub); - } -} diff --git a/src/main/java/bjc/rgens/text/markov/TextGenerator.java b/src/main/java/bjc/rgens/text/markov/TextGenerator.java index 9ec7c47..22af52e 100755 --- a/src/main/java/bjc/rgens/text/markov/TextGenerator.java +++ b/src/main/java/bjc/rgens/text/markov/TextGenerator.java @@ -52,7 +52,7 @@ public class TextGenerator { StandaloneMarkov markov = null; try (FileReader reader = new FileReader(file)) { - markov = StandaloneTextGenerator.generateMarkovMap(k, reader); + markov = StandaloneMarkov.generateMarkovMap(k, reader); String generatedText = markov.generateTextFromMarkov(M); String desiredText = generatedText.substring(0, Math.min(M, text.length())); |
