diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/gen/WeightedGrammar.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedGrammar.java | 86 |
1 files changed, 43 insertions, 43 deletions
diff --git a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java index 324a80c..48cc658 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -6,12 +6,12 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; import bjc.funcdata.FunctionalList; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; /** * A random grammar, where certain rules will come up more often than others. @@ -26,22 +26,22 @@ public class WeightedGrammar<E> { protected String initialRule; /** The rules currently in this grammar */ - protected IMap<E, WeightedRandom<IList<E>>> rules; + protected MapEx<E, WeightedRandom<ListEx<E>>> rules; /** The random number generator used for random numbers */ private Random rng; /** All of the subgrammars of this grammar */ - protected IMap<E, WeightedGrammar<E>> subgrammars; + protected MapEx<E, WeightedGrammar<E>> subgrammars; /** Rules that require special handling */ - private IMap<E, Supplier<IList<E>>> specialRules; + private MapEx<E, Supplier<ListEx<E>>> specialRules; /** Predicate for marking special tokens */ private Predicate<E> specialMarker; /** Action for special tokens */ - private BiFunction<E, WeightedGrammar<E>, IList<E>> specialAction; + private BiFunction<E, WeightedGrammar<E>, ListEx<E>> specialAction; /** Create a new weighted grammar. */ public WeightedGrammar() { @@ -75,7 +75,7 @@ public class WeightedGrammar<E> { * The action to take on those tokens. */ public void configureSpecial(final Predicate<E> marker, - final BiFunction<E, WeightedGrammar<E>, IList<E>> action) { + final BiFunction<E, WeightedGrammar<E>, ListEx<E>> action) { specialMarker = marker; specialAction = action; } @@ -89,7 +89,7 @@ public class WeightedGrammar<E> { * @param cse * The case for the rule. */ - public void addSpecialRule(final E ruleName, final Supplier<IList<E>> cse) { + public void addSpecialRule(final E ruleName, final Supplier<ListEx<E>> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { @@ -111,14 +111,14 @@ public class WeightedGrammar<E> { * @param cse * The case being added. */ - public void addCase(final E ruleName, final int probability, final IList<E> cse) { + public void addCase(final E ruleName, final int probability, final ListEx<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"); } - rules.get(ruleName).addProbability(probability, cse); + rules.get(ruleName).get().addProbability(probability, cse); } /** @@ -143,7 +143,7 @@ public class WeightedGrammar<E> { return false; if (subgrammars.containsKey(name)) { - subgrammars.put(alias, subgrammars.get(name)); + subgrammars.put(alias, subgrammars.get(name).get()); return true; } @@ -180,7 +180,7 @@ public class WeightedGrammar<E> { * * @return Whether or not the rule was succesfully added. */ - public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) { + public boolean addRule(final E name, final WeightedRandom<ListEx<E>> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -255,13 +255,13 @@ public class WeightedGrammar<E> { * * @return A set of sentences generated by the specified rule. */ - public IList<IList<E>> generateDebugValues(final E ruleName) { + public ListEx<ListEx<E>> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - final IList<IList<E>> returnedList = new FunctionalList<>(); + final ListEx<ListEx<E>> returnedList = new FunctionalList<>(); - final WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName); + final WeightedRandom<ListEx<E>> ruleGenerator = rules.get(ruleName).get(); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -288,7 +288,7 @@ public class WeightedGrammar<E> { * * @return A randomly generated sentence from the specified initial rule. */ - public <T> IList<T> generateGenericValues(final E initRules, + public <T> ListEx<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"); @@ -298,9 +298,9 @@ public class WeightedGrammar<E> { throw new NullPointerException("Spacer must not be null"); } - final IList<T> returnedList = new FunctionalList<>(); + final ListEx<T> returnedList = new FunctionalList<>(); - IList<E> genRules = new FunctionalList<>(initRules); + ListEx<E> genRules = new FunctionalList<>(initRules); if (specialMarker != null) { if (specialMarker.test(initRules)) { @@ -313,7 +313,7 @@ public class WeightedGrammar<E> { */ for (final E initRule : genRules.toIterable()) { if (specialRules.containsKey(initRule)) { - for (final E rulePart : specialRules.get(initRule).get().toIterable()) { + for (final E rulePart : specialRules.get(initRule).get().get().toIterable()) { final Iterable<T> generatedRuleParts = generateGenericValues(rulePart, tokenTransformer, spacer) .toIterable(); @@ -324,7 +324,7 @@ public class WeightedGrammar<E> { } } } else if (subgrammars.containsKey(initRule)) { - final Iterable<T> ruleParts = subgrammars.get(initRule) + final Iterable<T> ruleParts = subgrammars.get(initRule).get() .generateGenericValues(initRule, tokenTransformer, spacer) .toIterable(); @@ -334,7 +334,7 @@ public class WeightedGrammar<E> { } } else if (rules.containsKey(initRule)) { final Iterable<E> ruleParts - = rules.get(initRule).generateValue().toIterable(); + = rules.get(initRule).get().generateValue().toIterable(); for (final E rulePart : ruleParts) { final Iterable<T> generatedRuleParts @@ -371,8 +371,8 @@ public class WeightedGrammar<E> { * * @return A list of random grammar elements generated by the specified rule. */ - public IList<E> generateListValues(final E initRule, final E spacer) { - final IList<E> retList + public ListEx<E> generateListValues(final E initRule, final E spacer) { + final ListEx<E> retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; @@ -401,8 +401,8 @@ public class WeightedGrammar<E> { * * @return The set of all rule names in this grammar. */ - public IList<E> getRuleNames() { - final IList<E> ruleNames = new FunctionalList<>(); + public ListEx<E> getRuleNames() { + final ListEx<E> ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -422,7 +422,7 @@ public class WeightedGrammar<E> { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); - return subgrammars.get(name); + return subgrammars.get(name).get(); } /** @@ -471,19 +471,19 @@ public class WeightedGrammar<E> { throw new IllegalArgumentException( "Number of times to prefix must be positive."); - final WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); /* * @NOTE Can this be simplified? */ rule.getValues().forEach(pair -> { - final IList<IList<E>> newRule = new FunctionalList<>(); + final ListEx<ListEx<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - final IList<E> newCase = pair.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = pair.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -502,7 +502,7 @@ public class WeightedGrammar<E> { newRule.forEach((list) -> { final Integer currentProb = pair.merge((left, right) -> left); - newResults.add(new Pair<>(currentProb + additionalProbability, list)); + newResults.add(new SimplePair<>(currentProb + additionalProbability, list)); }); }); @@ -534,13 +534,13 @@ public class WeightedGrammar<E> { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach(pair -> { - final IList<E> newCase = pair.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = pair.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -551,7 +551,7 @@ public class WeightedGrammar<E> { newCase.prepend(prefixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( pair.merge((left, right) -> left) + additionalProbability, newCase)); }); @@ -589,13 +589,13 @@ public class WeightedGrammar<E> { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach(par -> { - final IList<E> newCase = par.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = par.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -606,7 +606,7 @@ public class WeightedGrammar<E> { newCase.add(suffixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( par.merge((left, right) -> left) + additionalProbability, newCase)); }); |
