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. --- .../java/bjc/rgens/text/markov/TextGenerator.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java (limited to 'RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java') diff --git a/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java b/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java new file mode 100755 index 0000000..770acd9 --- /dev/null +++ b/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java @@ -0,0 +1,69 @@ +package bjc.rgens.text.markov; + +import java.io.*; + +/** + * Generate text from a markov model of an input text + * + * @author ben + * + */ +public class TextGenerator { + /** + * @param args + * when used with three arguments, the first represents the + * k-order of the Markov objects. The second represents the + * number of characters to print out. The third represents + * the file to be read. + * + * When used with two arguments, the first represents the + * k-order of the Markov objects, and the second represents + * the file to be read. The generated text will be the same + * number of characters as the original file. + */ + public static void main(String[] args) { + int k = 0; + int M = 0; + + String file = ""; + StringBuilder text = new StringBuilder(); + + if (args.length == 3) { + k = Integer.parseInt(args[0]); + M = Integer.parseInt(args[1]); + file = args[2]; + } else if (args.length == 2) { + k = Integer.parseInt(args[0]); + file = args[1]; + } else { + System.out + .println("\n" + "Usage: java TextGenerator k M file"); + System.out.println( + "where k is the markov order, M is the number"); + System.out.println( + "of characters to be printed, and file is the"); + System.out.println( + "name of the file to print from. M may be left out." + + "\n"); + System.exit(1); + } + + StandaloneMarkov markov = null; + + try (FileReader reader = new FileReader(file)) { + markov = StandaloneTextGenerator.generateMarkovMap(k, + reader); + + System.out.println(markov.generateTextFromMarkov(M) + .substring(0, Math.min(M, text.length()))); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + e.printStackTrace(); + System.exit(1); + } catch (IOException ioex) { + System.out.println("IOException"); + ioex.printStackTrace(); + System.exit(1); + } + } +} \ No newline at end of file -- cgit v1.2.3