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 | 137 |
1 files changed, 70 insertions, 67 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 66ee993..7777ad8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -1,5 +1,11 @@ package bjc.utils.gen; +import java.util.Random; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; @@ -7,12 +13,6 @@ import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; -import java.util.Random; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - /** * A random grammar, where certain rules will come up more often than others. * @@ -74,7 +74,7 @@ public class WeightedGrammar<E> { * @param source * The source of randomness to use */ - public WeightedGrammar(Random source) { + public WeightedGrammar(final Random source) { this(); if (source == null) throw new NullPointerException("Source of randomness must be non-null"); @@ -84,28 +84,29 @@ public class WeightedGrammar<E> { /** * Configure the action to perform on special tokens. - * + * * @param marker * The marker to find special tokens. - * + * * @param action * The action to take on those tokens. */ - public void configureSpecial(Predicate<E> marker, BiFunction<E, WeightedGrammar<E>, IList<E>> action) { + public void configureSpecial(final Predicate<E> marker, + final BiFunction<E, WeightedGrammar<E>, IList<E>> action) { specialMarker = marker; specialAction = action; } /** * Adds a special rule to the grammar. - * + * * @param ruleName * The name of the special rule. - * + * * @param cse * The case for the rule. */ - public void addSpecialRule(E ruleName, Supplier<IList<E>> cse) { + public void addSpecialRule(final E ruleName, final Supplier<IList<E>> cse) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (cse == null) throw new NullPointerException("Case must not be null"); @@ -123,7 +124,7 @@ public class WeightedGrammar<E> { * @param cse * The case being added. */ - public void addCase(E ruleName, int probability, IList<E> cse) { + public void addCase(final E ruleName, final int probability, final IList<E> cse) { if (ruleName == null) throw new NullPointerException("Rule name must be not null"); else if (cse == null) throw new NullPointerException("Case body must not be null"); @@ -140,7 +141,7 @@ public class WeightedGrammar<E> { * The alias of the subgrammar * @return Whether the alias was succesfully created */ - public boolean addGrammarAlias(E name, E alias) { + public boolean addGrammarAlias(final E name, final E alias) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); else if (alias == null) throw new NullPointerException("Subgrammar alias must not be null"); @@ -162,7 +163,7 @@ public class WeightedGrammar<E> { * The name of the rule to add. * @return Whether or not the rule was successfully added. */ - public boolean addRule(E name) { + public boolean addRule(final E name) { if (rng == null) { rng = new Random(); } @@ -181,7 +182,7 @@ public class WeightedGrammar<E> { * The set of cases for the rule. * @return Whether or not the rule was succesfully added. */ - public boolean addRule(E name, WeightedRandom<IList<E>> cases) { + public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) { if (name == null) throw new NullPointerException("Name must not be null"); else if (cases == null) throw new NullPointerException("Cases must not be null"); @@ -201,7 +202,7 @@ public class WeightedGrammar<E> { * The subgrammar to add. * @return Whether or not the subgrammar was succesfully added. */ - public boolean addSubgrammar(E name, WeightedGrammar<E> subgrammar) { + public boolean addSubgrammar(final E name, final WeightedGrammar<E> subgrammar) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); else if (subgrammar == null) throw new NullPointerException("Subgrammar must not be null"); @@ -218,7 +219,7 @@ public class WeightedGrammar<E> { * @param name * The name of the rule to remove. */ - public void deleteRule(E name) { + public void deleteRule(final E name) { if (name == null) throw new NullPointerException("Rule name must not be null"); rules.remove(name); @@ -230,7 +231,7 @@ public class WeightedGrammar<E> { * @param name * The name of the subgrammar to remove. */ - public void deleteSubgrammar(E name) { + public void deleteSubgrammar(final E name) { if (name == null) throw new NullPointerException("Rule name must not be null"); subgrammars.remove(name); @@ -245,12 +246,12 @@ public class WeightedGrammar<E> { * The rule to test. * @return A set of sentences generated by the specified rule. */ - public IList<IList<E>> generateDebugValues(E ruleName) { + public IList<IList<E>> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - IList<IList<E>> returnedList = new FunctionalList<>(); + final IList<IList<E>> returnedList = new FunctionalList<>(); - WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName); + final WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -267,25 +268,26 @@ public class WeightedGrammar<E> { * * @param initRules * The initial rule to start with. - * + * * @param tokenTransformer * The function to transform grammar output into * something. - * + * * @param spacer * The spacer element to add in between output tokens. - * + * * @return A randomly generated sentence from the specified initial * rule. */ - public <T> IList<T> generateGenericValues(E initRules, Function<E, T> tokenTransformer, T spacer) { + public <T> IList<T> generateGenericValues(final E initRules, final Function<E, T> tokenTransformer, + final T spacer) { if (initRules == null) throw new NullPointerException("Initial rule must not be null"); else if (tokenTransformer == null) throw new NullPointerException("Transformer must not be null"); else if (spacer == null) throw new NullPointerException("Spacer must not be null"); - IList<T> returnedList = new FunctionalList<>(); + final IList<T> returnedList = new FunctionalList<>(); IList<E> genRules = new FunctionalList<>(initRules); @@ -295,39 +297,39 @@ public class WeightedGrammar<E> { } } - for (E initRule : genRules.toIterable()) { + for (final E initRule : genRules.toIterable()) { if (specialRules.containsKey(initRule)) { - for (E rulePart : specialRules.get(initRule).get().toIterable()) { - Iterable<T> generatedRuleParts = generateGenericValues(rulePart, + for (final E rulePart : specialRules.get(initRule).get().toIterable()) { + final Iterable<T> generatedRuleParts = generateGenericValues(rulePart, tokenTransformer, spacer).toIterable(); - for (T generatedRulePart : generatedRuleParts) { + for (final T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } } } else if (subgrammars.containsKey(initRule)) { - Iterable<T> ruleParts = subgrammars.get(initRule) + final Iterable<T> ruleParts = subgrammars.get(initRule) .generateGenericValues(initRule, tokenTransformer, spacer).toIterable(); - for (T rulePart : ruleParts) { + for (final T rulePart : ruleParts) { returnedList.add(rulePart); returnedList.add(spacer); } } else if (rules.containsKey(initRule)) { - Iterable<E> ruleParts = rules.get(initRule).generateValue().toIterable(); + final Iterable<E> ruleParts = rules.get(initRule).generateValue().toIterable(); - for (E rulePart : ruleParts) { - Iterable<T> generatedRuleParts = generateGenericValues(rulePart, + for (final E rulePart : ruleParts) { + final Iterable<T> generatedRuleParts = generateGenericValues(rulePart, tokenTransformer, spacer).toIterable(); - for (T generatedRulePart : generatedRuleParts) { + for (final T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } } } else { - T transformedToken = tokenTransformer.apply(initRule); + final T transformedToken = tokenTransformer.apply(initRule); if (transformedToken == null) throw new NullPointerException("Transformer created null token"); @@ -350,8 +352,8 @@ public class WeightedGrammar<E> { * @return A list of random grammar elements generated by the specified * rule. */ - public IList<E> generateListValues(E initRule, E spacer) { - IList<E> retList = generateGenericValues(initRule, strang -> strang, spacer); + public IList<E> generateListValues(final E initRule, final E spacer) { + final IList<E> retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; } @@ -380,7 +382,7 @@ public class WeightedGrammar<E> { * @return The set of all rule names in this grammar */ public IList<E> getRuleNames() { - IList<E> ruleNames = new FunctionalList<>(); + final IList<E> ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -395,7 +397,7 @@ public class WeightedGrammar<E> { * The name of the subgrammar to get. * @return The subgrammar with the specified name. */ - public WeightedGrammar<E> getSubgrammar(E name) { + public WeightedGrammar<E> getSubgrammar(final E name) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); return subgrammars.get(name); @@ -412,13 +414,13 @@ public class WeightedGrammar<E> { /** * Check if this grammar has a given rule. - * + * * @param ruleName * The rule to check for. - * + * * @return Whether or not the grammar has a rule by that name. */ - public boolean hasRule(E ruleName) { + public boolean hasRule(final E ruleName) { return rules.containsKey(ruleName) || specialRules.containsKey(ruleName); } @@ -434,7 +436,8 @@ public class WeightedGrammar<E> { * @param numberOfTimes * The number of times to prefix the token */ - public void multiPrefixRule(E ruleName, E prefixToken, int additionalProbability, int numberOfTimes) { + public void multiPrefixRule(final E ruleName, final E prefixToken, final int additionalProbability, + final int numberOfTimes) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (prefixToken == null) @@ -442,18 +445,18 @@ public class WeightedGrammar<E> { else if (numberOfTimes < 1) throw new IllegalArgumentException("Number of times to prefix must be positive."); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - IList<IList<E>> newRule = new FunctionalList<>(); + final IList<IList<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - IList<E> newCase = pair.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = pair.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } @@ -468,7 +471,7 @@ public class WeightedGrammar<E> { } newRule.forEach((list) -> { - Integer currentProb = pair.merge((left, right) -> left); + final Integer currentProb = pair.merge((left, right) -> left); newResults.add(new Pair<>(currentProb + additionalProbability, list)); }); @@ -492,20 +495,20 @@ public class WeightedGrammar<E> { * @param prefixToken * The token to prefix to the rule */ - public void prefixRule(E ruleName, E prefixToken, int additionalProbability) { + public void prefixRule(final E ruleName, final E prefixToken, final int additionalProbability) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (prefixToken == null) throw new NullPointerException("Prefix token must not be null"); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - IList<E> newCase = pair.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = pair.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } @@ -526,7 +529,7 @@ public class WeightedGrammar<E> { * @param initRule * The initial rule of this grammar */ - public void setInitialRule(String initRule) { + public void setInitialRule(final String initRule) { this.initialRule = initRule; } @@ -540,20 +543,20 @@ public class WeightedGrammar<E> { * @param additionalProbability * Additional probability of the prefixed rule */ - public void suffixRule(E ruleName, E suffixToken, int additionalProbability) { + public void suffixRule(final E ruleName, final E suffixToken, final int additionalProbability) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (suffixToken == null) throw new NullPointerException("Prefix token must not be null"); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((par) -> { - IList<E> newCase = par.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = par.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } |
