diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-05-29 15:35:02 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-05-29 15:35:02 -0300 |
| commit | 0faa5175b6f0de8835ed514615ac64135f406b29 (patch) | |
| tree | 6a1a5a9b01a1175b8ae2203418005f7bc625be9d /src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java | |
| parent | 9356498bebab9342222cd87be669abae9d7ac3a2 (diff) | |
Move files out of folder
Diffstat (limited to 'src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java')
| -rw-r--r-- | src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java b/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java new file mode 100644 index 0000000..339e8d5 --- /dev/null +++ b/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java @@ -0,0 +1,76 @@ +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); + } +} |
