class InputFilter:
"""filter out non-relevant input
or filter in relevant data"""
def input(self, ear: str, skin: str, eye: str) -> str:
# override me
pass
def filter(self, ear: str) -> AXKeyValuePair:
# override me : key = context/category, value: param
return AXKeyValuePair()
public class PerChance {/*
* extend me and add sentences and lists for parameters in the sentences in the
* sub classes c'tor.
replicate speech paterns, generate movie scripts or books and enjoy
*/
protected ArrayList<String> sentences = new ArrayList<String>();
protected Hashtable<String, UniqueItemSizeLimitedPriorityQueue> wordToList = new Hashtable<>();
protected Random rand = new Random();
private RegexUtil regexUtil = new RegexUtil();
public PerChance() {
super();
}
public String generateJoke() {
int x = rand.nextInt(sentences.size());
String result = sentences.get(x);
return clearRecursion(result);
}
private String clearRecursion(String result) {
int x;
ArrayList<String> params = new ArrayList<String>();
params = regexUtil.extractAllRegexes("(\\w+)(?= #)", result);
for (String strI : params) {
UniqueItemSizeLimitedPriorityQueue temp = wordToList.get(strI);
String s1 = temp.getRNDElement();
result = result.replace(strI + " #", s1);
}
if (!result.contains("#")) {
return result;
} else {
return clearRecursion(result);
}
}
public void addParam(String category, String value){
if(wordToList.containsKey(category)){
wordToList.get(category).add(value);
}
}
public void addParam(AXKeyValuePair kv){
if(wordToList.containsKey(kv.getKey())){
wordToList.get(kv.getKey()).add(kv.getValue());
}
}
}
public class PerChanceTest extends PerChance{
public PerChanceTest() {
super();
sentences.add("here is a salad vegi1 #, vegi2 # and herb #.");
sentences.add("how about this salad vegi1 #, vegi2 # and herb #. it goes well with tuna fish");
UniqueItemSizeLimitedPriorityQueue temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("vegi1", temp);
temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("vegi2", temp);
temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("herb", temp);
}
}
class PerChance:
'''extend me and add sentences and lists for parameters in the sentences in the
sub classes c'tor.
replicate speech paterns, generate movie scripts or books and enjoy'''
def __init__(self):
self.sentences: list[str] = []
self.wordToList: dict[str, UniqueItemSizeLimitedPriorityQueue] = {}
self.regexUtil: RegexUtil = RegexUtil()
def _clearRecursion(self, result: str) -> str:
while "#" in result:
t:str = self.regexUtil.extractRegex("(\\w+)(?= #)", result)
temp: UniqueItemSizeLimitedPriorityQueue = self.wordToList[t]
s1 = temp.getRNDElement()
result = result.replace(t + " #", s1)
return result
def generateJoke(self) -> str:
# select random element in arraylist code example:
result: str = random.choice(self.sentences)
return self._clearRecursion(result)
def addParam(self, category: str, value: str):
if category in self.wordToList:
self.wordToList[category].insert(value)
def addParam(self, kv: AXKeyValuePair):
if kv.key in self.wordToList:
self.wordToList[kv.key].insert(kv.value)
class PerChanceTest(PerChance):
def __init__(self):
super().__init__()
self.sentences.append("here is a salad vegi1 #, vegi2 # and herb #.")
self.sentences.append("how about this salad vegi1 #, vegi2 # and herb #. it goes well with tuna fish")
temp: UniqueItemSizeLimitedPriorityQueue = UniqueItemSizeLimitedPriorityQueue(3)
temp.insert("tomato")
temp.insert("sherry tomato")
temp.insert("pickle")
self.wordToList["vegi1"] = temp
temp = UniqueItemSizeLimitedPriorityQueue(3)
temp.insert("carrot")
temp.insert("onion")
temp.insert("radish")
self.wordToList["vegi2"] = temp
temp = UniqueItemSizeLimitedPriorityQueue(3)
temp.insert("dill")
temp.insert("parcely")
temp.insert("coriander")
self.wordToList["herb"] = temp
public class Strategy {
private UniqueItemSizeLimitedPriorityQueue activeStrategy; // active strategic options
private DrawRnd allStrategies; // bank of all strategies. out of this pool active strategies are pulled
public Strategy(DrawRnd allStrategies) {
// create the strategy Object with a bank of options
this.allStrategies = allStrategies;
this.activeStrategy = new UniqueItemSizeLimitedPriorityQueue();
}
public void evolveStrategies(int strategiesLimit){
// add N strategic options to the active strategies bank, from the total strategy bank
activeStrategy.setLimit(strategiesLimit);
String temp = allStrategies.draw();
for (int i = 0; i < strategiesLimit; i++) {
if(temp.isEmpty()){
break;
}
activeStrategy.add(temp);
temp = allStrategies.draw();
}
allStrategies.reset();
}
public String getStrategy(){
return this.activeStrategy.getRNDElement();
}
}
class Strategy:
def __init__(self, allStrategies: DrawRnd, strategiesLim: int):
# bank of all strategies. out of this pool active strategies are pulled
self._allStrategies: DrawRnd = allStrategies
self._strategiesLim = strategiesLim
# active strategic options
self._activeStrategy: UniqueItemSizeLimitedPriorityQueue = UniqueItemSizeLimitedPriorityQueue(strategiesLim)
def evolveStrategies(self):
# add N strategic options to the active strategies bank, from the total strategy bank
temp: str = self._allStrategies.drawAndRemove()
for i in range(0, self._strategiesLim):
if temp == "":
break
self._activeStrategy.insert(temp)
temp = self._allStrategies.drawAndRemove()
self._allStrategies.reset()
def getStrategy(self) -> str:
return self._activeStrategy.getRNDElement()
public class AXStrategy {
/* this auxiliary module is used to output strategies based on context
can be used for battles, and games
upon pain/lose use the evolve methode to update to different new active strategies
check for battle state end externaly (opponent state/hits on rival counter)
a dictionary of strategies*/
private int lim;
private Hashtable<String,Strategy> strategies = new Hashtable<>();
public AXStrategy(int lim) {
// limit of active strategies (pulled from all available strategies)
this.lim = lim;
}
public void addStrategy(String context, DrawRnd techniques){
// add strategies per context
Strategy temp = new Strategy(techniques);
temp.evolveStrategies(lim);
this.strategies.put(context,temp);
}
public void evolve(){
// replace active strategies
Enumeration<String> e = this.strategies.keys();
String key = "";
while (e.hasMoreElements()){
key = e.nextElement();
this.strategies.get(key).evolveStrategies(lim);
}
}
public String process(String context){
// process input, return action based on game context now
if(this.strategies.containsKey(context)){
return this.strategies.get(context).getStrategy();
}
return "";
}
}
class AXStrategy:
'''this auxiliary module is used to output strategies based on context
can be used for battles, and games
upon pain/lose use the evolve methode to update to different new active strategies
check for battle state end externaly (opponent state/hits on rival counter)
a dictionary of strategies'''
def __init__(self):
self.strategies: dict[str, Strategy] = {}
def addStrategy(self, context: str, techniques: DrawRnd, lim: int):
# add strategies per context
# lim is the limit of active strategies, it should be less than
# the total strategies in techniques
temp: Strategy = Strategy(techniques, lim)
self.strategies[context] = temp
def evolve(self):
# replace active strategies
key: str = ""
for k in self.strategies.keys():
self.strategies[k].evolveStrategies()
def process(self, context: str) -> str:
# process input, return action based on game context now
if context in self.strategies:
return self.strategies[context].getStrategy()
return ""
public class PersistantQuestion {
private Boolean isActive = false;
private String mode = "yes"; // key mode
private Hashtable<String, DrawRnd>dic = new Hashtable<>();
private OutputDripper outputDripper = new OutputDripper(1);
private String loggedAnswer = ""; // only used in log() which replaces process()
public String getLoggedAnswer() {
return loggedAnswer;
}
public void setLoggedAnswer(String loggedAnswer) {
this.loggedAnswer = loggedAnswer;
}
public void addPath(String answer, DrawRnd nags){
dic.put(answer,nags);
}
public void activate(){isActive = true;}
public void deActivate(){
isActive = false;
dic.get(mode).reset();
}
public String process(String inp){
// got answer?
if (dic.containsKey(inp)){
mode = inp;
isActive = false;
dic.get(mode).reset();
return "okay"; // can extend code to reply key, rnd finalizer
}
// nag for answer
if (!outputDripper.drip()) {return "";}
String result = dic.get(mode).draw();
if (!result.isEmpty()){return result;}
else {dic.get(mode).reset();isActive = false; return "i see";}
}
public String log(String inp){
// got answer?
if (dic.containsKey(inp)){
mode = inp;
loggedAnswer = inp;
isActive = false;
dic.get(mode).reset();
return "okay"; // can extend code to reply key, rnd finalizer
}
if (!inp.isEmpty()){
loggedAnswer = inp;
isActive = false;
dic.get(mode).reset();
return "okay"; // can extend code to reply key, rnd finalizer
}
// nag for answer
if (!outputDripper.drip()) {return "";}
String result = dic.get(mode).draw();
if (!result.isEmpty()){return result;}
else {dic.get(mode).reset();isActive = false; return "i see";}
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
if (dic.containsKey(mode)){
this.mode = mode;}
}
public void setPause(int pause){
// set pause between question to wait for answer
this.outputDripper.setLimit(pause);
}
}
class PersistentQuestion:
def __init__(self):
self._isActive: bool = False
self._mode = "yes" # answer as context for question phrasing
self.dic: dict[str, DrawRnd] = {}
self._outputDripper: OutputDripper = OutputDripper(1)
self.loggedAnswer: str = "" # only used in log() which replaces process()
def addPath(self, answer: str, nags: DrawRnd):
self.dic[answer] = nags
def activate(self):
self._isActive = True
def isActive(self):
return self._isActive
def deActivate(self):
self._isActive = False
self.dic[self._mode].reset()
def process(self, inp: str) -> str:
# got answer?
if inp in self.dic:
self._mode = inp
self._isActive = False
self.dic[self._mode].reset()
return "okay" # can extend code to reply key, rnd finalizer
# nag for answer
if not self._outputDripper.drip():
return ""
result: str = self.dic[self._mode].drawAndRemove()
if not result == "":
return result
self.dic[self._mode].reset()
self._isActive = False
return "i see"
def log(self, inp: str) -> str:
# got answer?
if inp in self.dic:
self._mode = inp
self.loggedAnswer = inp
self._isActive = False
self.dic[self._mode].reset()
return "okay" # can extend code to reply key, rnd finalizer
if not inp == "":
self.loggedAnswer = inp
self._isActive = False
self.dic[self._mode].reset()
return "okay" # can extend code to reply key, rnd finalizer
# nag for answer
if not self._outputDripper.drip():
return ""
result: str = self.dic[self._mode].drawAndRemove()
if not result == "":
return result
self.dic[self._mode].reset()
self._isActive = False
return "i see"
def getMode(self):
return self._mode
def setMode(self, mode: str):
if mode in self.dic:
self._mode = mode
def setPause(self):
# set pause between question to wait for answer
self._outputDripper.setLimit()