From a2c7425458f645802a352abc4783e0afc73dba13 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:22:35 -0500 Subject: Adapt to esodata changes --- .../main/java/bjc/utils/gen/WeightedGrammar.java | 74 +++++++++++----------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'base/src/main/java/bjc/utils/gen/WeightedGrammar.java') diff --git a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java index 540efbc..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 { protected String initialRule; /** The rules currently in this grammar */ - protected IMap>> rules; + protected MapEx>> rules; /** The random number generator used for random numbers */ private Random rng; /** All of the subgrammars of this grammar */ - protected IMap> subgrammars; + protected MapEx> subgrammars; /** Rules that require special handling */ - private IMap>> specialRules; + private MapEx>> specialRules; /** Predicate for marking special tokens */ private Predicate specialMarker; /** Action for special tokens */ - private BiFunction, IList> specialAction; + private BiFunction, ListEx> specialAction; /** Create a new weighted grammar. */ public WeightedGrammar() { @@ -75,7 +75,7 @@ public class WeightedGrammar { * The action to take on those tokens. */ public void configureSpecial(final Predicate marker, - final BiFunction, IList> action) { + final BiFunction, ListEx> action) { specialMarker = marker; specialAction = action; } @@ -89,7 +89,7 @@ public class WeightedGrammar { * @param cse * The case for the rule. */ - public void addSpecialRule(final E ruleName, final Supplier> cse) { + public void addSpecialRule(final E ruleName, final Supplier> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { @@ -111,7 +111,7 @@ public class WeightedGrammar { * @param cse * The case being added. */ - public void addCase(final E ruleName, final int probability, final IList cse) { + public void addCase(final E ruleName, final int probability, final ListEx cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -180,7 +180,7 @@ public class WeightedGrammar { * * @return Whether or not the rule was succesfully added. */ - public boolean addRule(final E name, final WeightedRandom> cases) { + public boolean addRule(final E name, final WeightedRandom> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -255,13 +255,13 @@ public class WeightedGrammar { * * @return A set of sentences generated by the specified rule. */ - public IList> generateDebugValues(final E ruleName) { + public ListEx> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - final IList> returnedList = new FunctionalList<>(); + final ListEx> returnedList = new FunctionalList<>(); - final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); + final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -288,7 +288,7 @@ public class WeightedGrammar { * * @return A randomly generated sentence from the specified initial rule. */ - public IList generateGenericValues(final E initRules, + public ListEx generateGenericValues(final E initRules, final Function tokenTransformer, final T spacer) { if (initRules == null) { throw new NullPointerException("Initial rule must not be null"); @@ -298,9 +298,9 @@ public class WeightedGrammar { throw new NullPointerException("Spacer must not be null"); } - final IList returnedList = new FunctionalList<>(); + final ListEx returnedList = new FunctionalList<>(); - IList genRules = new FunctionalList<>(initRules); + ListEx genRules = new FunctionalList<>(initRules); if (specialMarker != null) { if (specialMarker.test(initRules)) { @@ -371,8 +371,8 @@ public class WeightedGrammar { * * @return A list of random grammar elements generated by the specified rule. */ - public IList generateListValues(final E initRule, final E spacer) { - final IList retList + public ListEx generateListValues(final E initRule, final E spacer) { + final ListEx retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; @@ -401,8 +401,8 @@ public class WeightedGrammar { * * @return The set of all rule names in this grammar. */ - public IList getRuleNames() { - final IList ruleNames = new FunctionalList<>(); + public ListEx getRuleNames() { + final ListEx ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -471,19 +471,19 @@ public class WeightedGrammar { throw new IllegalArgumentException( "Number of times to prefix must be positive."); - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); /* * @NOTE Can this be simplified? */ rule.getValues().forEach(pair -> { - final IList> newRule = new FunctionalList<>(); + final ListEx> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -502,7 +502,7 @@ public class WeightedGrammar { 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 { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(pair -> { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -551,7 +551,7 @@ public class WeightedGrammar { 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 { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(par -> { - final IList newCase = par.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = par.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -606,7 +606,7 @@ public class WeightedGrammar { newCase.add(suffixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( par.merge((left, right) -> left) + additionalProbability, newCase)); }); -- cgit v1.2.3