diff options
Diffstat (limited to 'RGens/src/main/java/bjc')
9 files changed, 138 insertions, 101 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java index edd7e80..d71578b 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java @@ -108,7 +108,7 @@ public class RGrammarBuilder { throw new IllegalStateException("Must start a rule before finishing one"); } - rules.put(currRule.ruleName, currRule); + rules.put(currRule.name, currRule); } /** diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarFormatter.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarFormatter.java index 231e191..202d9ef 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarFormatter.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarFormatter.java @@ -37,13 +37,13 @@ public class RGrammarFormatter { } for(Rule rule : rules.values()) { - if(!processedRules.contains(rule.ruleName)) { + if(!processedRules.contains(rule.name)) { sb.append("\n\n"); processRule(rule, sb); } - processedRules.add(rule.ruleName); + processedRules.add(rule.name); } return sb.toString().trim(); @@ -54,7 +54,7 @@ public class RGrammarFormatter { StringBuilder ruleBuilder = new StringBuilder(); - ruleBuilder.append(rule.ruleName); + ruleBuilder.append(rule.name); ruleBuilder.append(" \u2192 "); int markerPos = ruleBuilder.length(); diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java index cb76946..2f64b75 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java @@ -1,8 +1,8 @@ package bjc.rgens.newparser; import bjc.utils.funcutils.TriConsumer; -import bjc.utils.parserutils.BlockReader; -import bjc.utils.parserutils.BlockReader.Block; +import bjc.utils.ioutils.Block; +import bjc.utils.ioutils.BlockReader; import java.io.InputStream; import java.io.InputStreamReader; diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java index d86b18c..608697a 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java @@ -73,8 +73,8 @@ public class RGrammarSet { grammars.put(grammarName, gram); for(Rule export : gram.getExportedRules()) { - exportedRules.put(export.ruleName, gram); - exportFrom.put(export.ruleName, grammarName); + exportedRules.put(export.name, gram); + exportFrom.put(export.name, grammarName); } gram.setImportedRules(exportedRules); diff --git a/RGens/src/main/java/bjc/rgens/newparser/Rule.java b/RGens/src/main/java/bjc/rgens/newparser/Rule.java index eca856f..0197860 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/Rule.java +++ b/RGens/src/main/java/bjc/rgens/newparser/Rule.java @@ -15,9 +15,9 @@ public class Rule { /** * The name of this grammar rule. */ - public final String ruleName; + public final String name; - private IList<RuleCase> ruleCases; + private IList<RuleCase> cases; /** * Create a new grammar rule. @@ -35,9 +35,9 @@ public class Rule { throw new IllegalArgumentException("The empty string is not a valid rule name"); } - this.ruleName = ruleName; + name = ruleName; - ruleCases = new FunctionalList<>(); + cases = new FunctionalList<>(); } /** @@ -51,7 +51,7 @@ public class Rule { throw new NullPointerException("Case must not be null"); } - ruleCases.add(cse); + cases.add(cse); } /** @@ -60,7 +60,7 @@ public class Rule { * @return A random case from this rule. */ public RuleCase getCase() { - return ruleCases.randItem(); + return cases.randItem(); } /** @@ -72,7 +72,7 @@ public class Rule { * @return A random case from this rule. */ public RuleCase getCase(Random rnd) { - return ruleCases.randItem(rnd::nextInt); + return cases.randItem(rnd::nextInt); } /** @@ -81,7 +81,7 @@ public class Rule { * @return All the cases in this rule. */ public IList<RuleCase> getCases() { - return ruleCases; + return cases; } @Override @@ -89,8 +89,8 @@ public class Rule { final int prime = 31; int result = 1; - result = prime * result + ((ruleCases == null) ? 0 : ruleCases.hashCode()); - result = prime * result + ((ruleName == null) ? 0 : ruleName.hashCode()); + result = prime * result + ((cases == null) ? 0 : cases.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @@ -103,19 +103,19 @@ public class Rule { Rule other = (Rule) obj; - if(ruleCases == null) { - if(other.ruleCases != null) return false; - } else if(!ruleCases.equals(other.ruleCases)) return false; + if(cases == null) { + if(other.cases != null) return false; + } else if(!cases.equals(other.cases)) return false; - if(ruleName == null) { - if(other.ruleName != null) return false; - } else if(!ruleName.equals(other.ruleName)) return false; + if(name == null) { + if(other.name != null) return false; + } else if(!name.equals(other.name)) return false; return true; } @Override public String toString() { - return String.format("Rule [ruleName='%s', ruleCases=%s]", ruleName, ruleCases); + return String.format("Rule [ruleName='%s', ruleCases=%s]", name, cases); } }
\ No newline at end of file diff --git a/RGens/src/main/java/bjc/rgens/text/markov/Markov.java b/RGens/src/main/java/bjc/rgens/text/markov/Markov.java index a07f44e..9eb4134 100755 --- a/RGens/src/main/java/bjc/rgens/text/markov/Markov.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/Markov.java @@ -4,26 +4,26 @@ import java.util.Map.Entry; import java.util.*; /** - * Represents a k-character substring. Can give a pseudo-random suffix - * character based on probability. + * Represents a k-character substring. Can give a pseudo-random suffix character + * based on probability. * * @author Daniel Friedman (Fall 2011) * */ public class Markov { - String substring; - int count = 0; + String substring; + int count = 0; - TreeMap<Character, Integer> map; + TreeMap<Character, Integer> map; /** * Constructs a Markov object from a given substring. * - * @param substring - * the given substring. + * @param substr + * the given substring. */ - public Markov(String substring) { - this.substring = substring; + public Markov(String substr) { + this.substring = substr; map = new TreeMap<>(); @@ -34,13 +34,13 @@ public class Markov { * Constructs a Markov object from a given substring and suffix * character. Suffix characters are stored in a TreeMap. * - * @param substring - * the specified substring. + * @param substr + * the specified substring. * @param suffix - * the specified suffix. + * the specified suffix. */ - public Markov(String substring, Character suffix) { - this.substring = substring; + public Markov(String substr, Character suffix) { + this.substring = substr; map = new TreeMap<>(); @@ -59,7 +59,7 @@ public class Markov { * Adds a suffix character to the TreeMap. * * @param c - * the suffix character to be added. + * the suffix character to be added. */ public void add(char c) { add(); @@ -67,8 +67,7 @@ public class Markov { if (map.containsKey(c)) { int frequency = map.get(c); map.put(c, frequency + 1); - } else - map.put(c, 1); + } else map.put(c, 1); } /** @@ -76,7 +75,7 @@ public class Markov { * of times the specified suffix follows the substring in a text. * * @param c - * the specified suffix. + * the specified suffix. * @return the frequency count. */ public int getFrequencyCount(char c) { @@ -106,7 +105,7 @@ public class Markov { * Finds whether or not the given suffix is in the TreeMap. * * @param c - * the given suffix. + * the given suffix. * @return True if the suffix exists in the TreeMap, false otherwise. */ public boolean containsChar(char c) { @@ -138,8 +137,8 @@ public class Markov { /** * Using probability, returns a pseudo-random character to follow the * substring. Character possibilities are added to an ArrayList - * (duplicates allowed), and a random number from 0 to the last index - * in the ArrayList is picked. Since more common suffixes occupy more + * (duplicates allowed), and a random number from 0 to the last index in + * the ArrayList is picked. Since more common suffixes occupy more * indices in the ArrayList, the probability of getting a more common * suffix is greater than the probability of getting a less common * suffix. diff --git a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java index bc31847..8934daa 100644 --- a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java @@ -2,39 +2,66 @@ package bjc.rgens.text.markov; import java.util.Map; +/** + * A standalone Markov generator. + * + * @author bjculkin + * + */ public class StandaloneMarkov { - private int k; + private int ord; - private Map<String, Markov> markovHash; - private String firstSub; + private Map<String, Markov> hash; + private String first; - public StandaloneMarkov(int k, Map<String, Markov> markovHash, String firstSub) { - this.k = k; - this.markovHash = markovHash; - this.firstSub = firstSub; + /** + * Create a new standalone Markov generator. + * + * @param order + * The order of this generator. + * + * @param markovHash + * The generators to use. + * + * @param firstSub + * The string to start out with. + */ + public StandaloneMarkov(int order, Map<String, Markov> markovHash, String firstSub) { + ord = order; + hash = markovHash; + first = firstSub; } - public String generateTextFromMarkov(int M) { + /** + * Generate random text from the markov generator. + * + * @param charsToGenerate + * The number of characters of text to generate. + * + * @return The randomly generate text. + */ + public String generateTextFromMarkov(int charsToGenerate) { StringBuilder text = new StringBuilder(); - for (int i = k; i < M; i++) { - if (i == k) { - text.append(firstSub); - if (text.length() > k) i = text.length(); + for (int i = ord; i < charsToGenerate; i++) { + if (i == ord) { + text.append(first); + + if (text.length() > ord) i = text.length(); } - String sub = text.substring((i - k), (i)); - Markov tmp = markovHash.get(sub); + String sub = text.substring(i - ord, i); + Markov tmp = hash.get(sub); if (tmp != null) { Character nextChar = tmp.random(); + text.append(nextChar); } else { - i = k - 1; + i = ord - 1; } } return text.toString(); } - } diff --git a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java index 92bc653..17cd670 100644 --- a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneTextGenerator.java @@ -5,19 +5,25 @@ 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 + * Build a markov generator from a provided source. + * + * @param order + * The markov order to use. * - * @param k - * The markov order to use * @param reader - * The source to seed the generator from - * @return The markov generator for the provided text + * The source to seed the generator from. + * + * @return The markov generator for the provided text. */ - public static StandaloneMarkov generateMarkovMap(int k, - Reader reader) { + public static StandaloneMarkov generateMarkovMap(int order, Reader reader) { Map<String, Markov> hash = new HashMap<>(); Character next = null; @@ -25,9 +31,10 @@ public class StandaloneTextGenerator { try { next = (char) reader.read(); } catch (IOException e1) { - System.out - .println("IOException in stepping through the reader"); + System.out.println("IOException in stepping through the reader"); + e1.printStackTrace(); + System.exit(1); } @@ -40,19 +47,19 @@ public class StandaloneTextGenerator { try { next = (char) reader.read(); } catch (IOException e) { - System.out.println( - "IOException in stepping through the reader"); + System.out.println("IOException in stepping through the reader"); + e.printStackTrace(); } } String origFile = origFileBuffer.toString(); - String firstSub = origFile.substring(0, k); + String firstSub = origFile.substring(0, order); - for (int i = 0; i < origFile.length() - k; i++) { - String sub = origFile.substring(i, i + k); - Character suffix = origFile.charAt(i + k); + 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); @@ -64,7 +71,6 @@ public class StandaloneTextGenerator { } } - return new StandaloneMarkov(k, hash, firstSub); + return new StandaloneMarkov(order, hash, firstSub); } - } diff --git a/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java b/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java index 770acd9..7d20d79 100755 --- a/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/TextGenerator.java @@ -10,16 +10,19 @@ import java.io.*; */ 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 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. + * 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; @@ -31,38 +34,40 @@ public class TextGenerator { 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.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); + markov = StandaloneTextGenerator.generateMarkovMap(k, reader); + + String generatedText = markov.generateTextFromMarkov(M); + String desiredText = generatedText.substring(0, Math.min(M, text.length())); - System.out.println(markov.generateTextFromMarkov(M) - .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); } } |
