From 5e1e3767b335479040e48ff57f1eeff8a8f0d0ef Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 07:17:38 -0500 Subject: Rearrange some things This moves some documentation to a specific docs folder, as well as creating a new examples folder, and putting the two pieces of example code into there --- .../java/bjc/rgens/parser/RGrammarTest.java | 144 +++++++++++++++++++++ .../java/bjc/rgens/text/markov/TextGenerator.java | 75 +++++++++++ 2 files changed, 219 insertions(+) create mode 100755 src/example/java/bjc/rgens/parser/RGrammarTest.java create mode 100755 src/example/java/bjc/rgens/text/markov/TextGenerator.java (limited to 'src/example') diff --git a/src/example/java/bjc/rgens/parser/RGrammarTest.java b/src/example/java/bjc/rgens/parser/RGrammarTest.java new file mode 100755 index 0000000..605fc5b --- /dev/null +++ b/src/example/java/bjc/rgens/parser/RGrammarTest.java @@ -0,0 +1,144 @@ +package bjc.rgens.parser; + +import static bjc.rgens.parser.RGrammarLogging.error; +import static bjc.rgens.parser.RGrammarLogging.perf; + +import bjc.data.Tree; +import bjc.data.SimpleTree; + +import java.io.IOException; + +import java.net.URISyntaxException; +import java.net.URL; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import java.util.Random; + +import bjc.rgens.parser.templates.GrammarTemplate; + +/** + * Test for new grammar syntax. + * + * @author EVE + */ +public class RGrammarTest { + /** + * Main method. + * + * @param args + * Unused CLI args. + */ + public static void main(String[] args) { + URL rsc = RGrammarTest.class.getResource("/server-config-sample.gcfg"); + + try { + LoadOptions lopts = new LoadOptions(); + + // Set up load options + lopts.doPerf = true; + lopts.doDebug = false; + lopts.doTrace = false; + + lopts.defName = "default"; + + /* Load a grammar set. */ + Path cfgPath = Paths.get(rsc.toURI()); + + String msg = String.format("INFO: Loading config file %s", cfgPath); + Tree errTree = new SimpleTree<>(msg); + + ConfigSet cfgSet = ConfigLoader.fromConfigFile(cfgPath, lopts, errTree); + + System.err.print(errTree); + + for(RGrammarSet gramSet : cfgSet.grammars.values()) { + testGrammarSet(gramSet); + } + + for(GrammarTemplate template : cfgSet.templates.values()) { + testTemplate(template, cfgSet.grammars.get("default")); + } + } catch (IOException ioex) { + ioex.printStackTrace(); + } catch (URISyntaxException urisex) { + urisex.printStackTrace(); + } + } + + private static void testTemplate(GrammarTemplate template, RGrammarSet set) { + System.out.printf("Generating for template %s\n", template); + + Random rnd = new Random(); + + for(int i = 0; i < 10; i++) { + GenerationState state = GenerationState.fromGrammar(rnd, set.exportGrammar); + + template.generate(state); + + String res = state.getContents(); + + if(res.length() > 120) { + System.out.printf("\t\n\tContents: %s\n\t\n", res); + } else { + System.out.printf("\tContents: %s\n", res); + } + } + } + + private static void testGrammarSet(RGrammarSet gramSet) { + /* Generate rule suggestions for all the grammars in the set. */ + for (String gramName : gramSet.getGrammars()) { + long startSuggTime = System.nanoTime(); + + gramSet.getGrammar(gramName).generateSuggestions(); + + long endSuggTime = System.nanoTime(); + + long suggDur = endSuggTime - startSuggTime; + + perf("Generated rule suggestions for %s in %d ns (%f s)", gramName, suggDur, suggDur / 1000000000.0); + } + + System.err.printf("\n\n"); + + /* Generate for each exported rule. */ + for (String exportName : gramSet.getExportedRules()) { + /* Where we loaded the rule from. */ + String loadSrc = gramSet.loadedFrom(gramSet.exportedFrom(exportName)); + + System.out.println(); + System.out.printf("Generating for exported rule '%s' from file '%s'\n", exportName, loadSrc); + + RGrammar grammar = gramSet.getExportSource(exportName); + long startGenTime = System.nanoTime(); + for (int i = 0; i < 100; i++) { + try { + String res = grammar.generate(exportName); + if(exportName.contains("+")) res = res.replaceAll("\\s+", ""); + + if(res.length() > 120) { + System.out.printf("\t\n\tContents: %s\n\t\n", res); + } else { + System.out.printf("\tContents: %s\n", res); + } + } catch (GrammarException gex) { + /* Print out errors with generation. */ + String fmt = "Exported rule %s from %s failed (loaded from '%s')"; + + System.out.printf("ERROR: " + fmt, exportName, grammar.name, loadSrc); + System.out.println(); + System.out.println(); + + error(gex, fmt, exportName, grammar.name, loadSrc); + } + } + long endGenTime = System.nanoTime(); + + long genDur = endGenTime - startGenTime; + + perf("Generated %s 100 times in %d ns (%f s)", exportName, genDur, genDur / 1000000000.0); + } + } +} diff --git a/src/example/java/bjc/rgens/text/markov/TextGenerator.java b/src/example/java/bjc/rgens/text/markov/TextGenerator.java new file mode 100755 index 0000000..22af52e --- /dev/null +++ b/src/example/java/bjc/rgens/text/markov/TextGenerator.java @@ -0,0 +1,75 @@ +package bjc.rgens.text.markov; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +/** + * Generate text from a markov model of an input text + * + * @author ben + * + */ +public class TextGenerator { + /** + * Main method. + * + * @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("\nUsage: 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 = StandaloneMarkov.generateMarkovMap(k, reader); + + String generatedText = markov.generateTextFromMarkov(M); + String desiredText = generatedText.substring(0, Math.min(M, text.length())); + + System.out.println(desiredText); + } 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); + } + } +} -- cgit v1.2.3