diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 623c212..463ad8b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -50,6 +50,9 @@ public class WeightedGrammar<E> { /** * Create a new weighted grammar that uses the specified source of * randomness. + * + * @param src + * The source of randomness to use */ public WeightedGrammar(Random src) { this(); @@ -166,7 +169,10 @@ public class WeightedGrammar<E> { /** * Generate a generic sentance from a initial rule. * - * @param initRule + * @param <T> + * The type of the transformed output + * + * @param initRle * The initial rule to start with. * @param f * The function to transform grammar output into something. @@ -175,24 +181,24 @@ public class WeightedGrammar<E> { * @return A randomly generated sentance from the specified initial * rule. */ - public <T> FunctionalList<T> genGeneric(E initRule, Function<E, T> f, + public <T> FunctionalList<T> genGeneric(E initRle, Function<E, T> f, T spacer) { FunctionalList<T> r = new FunctionalList<>(); - if (subgrammars.containsKey(initRule)) { - subgrammars.get(initRule).genGeneric(initRule, f, spacer) + if (subgrammars.containsKey(initRle)) { + subgrammars.get(initRle).genGeneric(initRle, f, spacer) .forEach(rp -> { r.add(rp); r.add(spacer); }); - } else if (rules.containsKey(initRule)) { - rules.get(initRule).genVal().forEach( + } else if (rules.containsKey(initRle)) { + rules.get(initRle).genVal().forEach( rp -> genGeneric(rp, f, spacer).forEach(rp2 -> { r.add(rp2); r.add(spacer); })); } else { - r.add(f.apply(initRule)); + r.add(f.apply(initRle)); r.add(spacer); } @@ -203,17 +209,22 @@ public class WeightedGrammar<E> { * Generate a random list of grammar elements from a given initial * rule. * - * @param initRule + * @param initRle * The initial rule to start with. * @param spacer * The item to use to space the list. * @return A list of random grammar elements generated by the specified * rule. */ - public FunctionalList<E> genList(E initRule, E spacer) { - return genGeneric(initRule, s -> s, spacer); + public FunctionalList<E> genList(E initRle, E spacer) { + return genGeneric(initRle, s -> s, spacer); } + /** + * Get the initial rule of this grammar + * + * @return The initial rule of this grammar + */ public String getInitRule() { return initRule; } @@ -254,15 +265,16 @@ public class WeightedGrammar<E> { int nTimes) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { FunctionalList<FunctionalList<E>> nls = new FunctionalList<>(); // TODO bugtest this. if it works, write multiSuffixWith for (int i = 1; i <= nTimes; i++) { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); for (int j = 1; j <= i; j++) { nl.prepend(prefixToken); @@ -293,11 +305,12 @@ public class WeightedGrammar<E> { public void prefixRule(E rName, E prefixToken, int addProb) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); nl.prepend(prefixToken); newResults.add(new Pair<>( @@ -368,11 +381,12 @@ public class WeightedGrammar<E> { public void suffixRule(E rName, E prefixToken, int addProb) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); nl.add(prefixToken); newResults.add(new Pair<>( |
