👨‍💻 dev python chat modules porting

development

owly

闇の伝説
Staff member
戦闘 コーダー
project progress at 100%
 
0% 100%


+ChatBot
+PercentDripper
+AXNPC
+AXTimeContextResponder
+UniqueItemsPriorityQue*Q peak()
+depracate perchance, perchance test

@fukurou
 
Last edited by a moderator:

fukurou

the supreme coder
ADMIN
Python:
class UniqueItemsPriorityQue(LGFIFO):
    # a priority queue without repeating elements
    # override
    def insert(self, data):
        if not self.queue.__contains__(data):
            self.queue.append(data)

    # override
    def peak(self)->str:
        # returns string
        temp = super().peak()
        if temp is None:
            return ""
        return temp
 

fukurou

the supreme coder
ADMIN
Java:
public class AXTimeContextResponder {
    // output reply based on the part of day as context
    private PlayGround pl = new PlayGround();
    public Responder morning = new Responder();
    public Responder afternoon = new Responder();
    public Responder evening = new Responder();
    public Responder night = new Responder();
    protected Hashtable<String, Responder> responders = new Hashtable<>();

    public AXTimeContextResponder() {
        responders.put("morning", morning);
        responders.put("afternoon", afternoon);
        responders.put("evening", evening);
        responders.put("night", night);
    }

    public String respond(){
        return responders.get(pl.partOfDay()).getAResponse();
    }
}
 

fukurou

the supreme coder
ADMIN
Python:
class AXTimeContextResponder:
    # output reply based on the part of day as context
    def __init__(self):
        self._pl: PlayGround = PlayGround()
        self.morning: Responder = Responder()
        self.afternoon: Responder = Responder()
        self.evening: Responder = Responder()
        self.night: Responder = Responder()
        self._responders: dict[str, Responder] = {"morning": self.morning, "afternoon": self.afternoon,
                                                  "evening": self.evening, "night": self.night}

    def respond(self) -> str:
        return self._responders[self._pl.partOfDay()].getAResponse()

overfapped today gotta restabit
 

fukurou

the supreme coder
ADMIN
Java:
public class PercentDripper {
    private DrawRnd dr = new DrawRnd();
    private int limis = 35;

    public void setLimis(int limis) {
        this.limis = limis;
    }
    public boolean drip(){
        return dr.getSimpleRNDNum(100) < limis;
    }
    public boolean dripPlus(int plus){
        return dr.getSimpleRNDNum(100) < limis + plus;
    }
}
 

fukurou

the supreme coder
ADMIN
Python:
class PercentDripper:
    def __init__(self):
        self.__dr: DrawRnd = DrawRnd()
        self.__limis: int = 35

    def setLimit(self, limis):
        self.__limis = limis

    def drip(self) -> bool:
        return self.__dr.getSimpleRNDNum(100) < self.__limis

    def dripPlus(self, plus: int) -> bool:
        return self.__dr.getSimpleRNDNum(100) < self.__limis + plus
 

fukurou

the supreme coder
ADMIN
Java:
public class AXNPC {
    public Responder responder = new Responder();
    public PercentDripper dripper = new PercentDripper();
    public String respond(){
        if (dripper.drip()){
            return responder.getAResponse();
        }
        return "";
    }
    public String respondPlus(int plus){
        // increase rate of output
        if (dripper.dripPlus(plus)){
            return responder.getAResponse();
        }
        return "";
    }
}
 

fukurou

the supreme coder
ADMIN
Python:
class AXNPC:
    def __init__(self):
        self.responder = Responder()
        self.dripper = PercentDripper()

    def respond(self) -> str:
        if self.dripper.drip():
            return self.responder.getAResponse()
        return ""

    def respondPlus(self, plus) -> str:
        if self.dripper.dripPlus(plus):
            return self.responder.getAResponse()
        return ""
 

fukurou

the supreme coder
ADMIN
Java:
public class ChatBot {
    /*
    * chatbot example use in main :
    *         // init
        ChatBot chatbot = new ChatBot(5);
        // set param categories (init stage):
        chatbot.addParam("name","jinpachi");
        chatbot.addParam("name","sakura");
        chatbot.addParam("verb","eat");
        chatbot.addParam("verb","code");
        // learn sentence at init stage(optiona):
        chatbot.addSentence("i can verb #");
        // learning params post init stage (code is running)
        chatbot.learnParam("ryu is a name");
        chatbot.learnParam("ken is a name");
        chatbot.learnParam("drink is a verb");
        chatbot.learnParam("rest is a verb");
        // learn sentences from input during code run stage
        chatbot.learnV2("hello ryu i like to code");
        chatbot.learnV2("greetings ken");
        for (int i = 1; i < 10; i++) {
            // gen speech
            System.out.println(chatbot.talk());
            // share params for other chatbot to learn
            System.out.println(chatbot.getALoggedParam());
            }
    * */
    protected RefreshQ sentences = new RefreshQ();
    protected Hashtable<String, RefreshQ> wordToList = new Hashtable<>(); // params
    protected Random rand = new Random(); // randomizer
    private RegexUtil regexUtil = new RegexUtil();
    protected Hashtable<String, String> allParamRef = new Hashtable<>();
    protected int paramLim = 5;
    protected RefreshQ loggedParams = new RefreshQ();
    private String conjuration = "is a";

    public ChatBot(int logParamLim) {
        loggedParams.setLimit(logParamLim);
    }

    public void setConjuration(String conjuration) {
        this.conjuration = conjuration;
    }

    public void setSentencesLim(int lim){sentences.setLimit(lim);}

    public void setParamLim(int paramLim) {
        this.paramLim = paramLim;
    }

    public Hashtable<String, RefreshQ> getWordToList() {
        return wordToList;
    }

    public String talk() {
        String result = sentences.getRNDElement();
        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);
        }
    }
    // learn on init:
    public void addParam(String category, String value){
        if(!(wordToList.containsKey(category))){
            RefreshQ temp = new RefreshQ();
            temp.setLimit(paramLim);
            wordToList.put(category, temp);
        }
        wordToList.get(category).add(value);
        allParamRef.put(value,category); // for learnV2
    }
    public void addParam(AXKeyValuePair kv){
        if(!(wordToList.containsKey(kv.getKey()))){
            RefreshQ temp = new RefreshQ();
            temp.setLimit(paramLim);
            wordToList.put(kv.getKey(), temp);
        }
        wordToList.get(kv.getKey()).add(kv.getValue());
        allParamRef.put(kv.getValue(),kv.getKey()); // for learnV2
    }
    // chatbot.addSentence("hello name #");
    public void addSentence(String sentence){
        this.sentences.add(sentence);
    }
    // chatbot.learn("hello name i like to verb");
    public void learn(String s1){
        for (String key : wordToList.keySet()) {
            s1 = s1.replace(key, String.format("%s #", key));
        }
        sentences.add(s1);
    }
    // learn while code is running
    public void learnV2(String s1){
        for (String key : allParamRef.keySet()) {
            s1 = s1.replace(key, String.format("%s #", allParamRef.get(key)));
        }
        sentences.add(s1);
    }
    public void learnParam(String s1){
        if (!s1.contains(conjuration)){return;}
        String category = regexUtil.afterWord(conjuration,s1);
        if(!(wordToList.containsKey(category))){return;}
        String param = s1.replace(conjuration +" "+category,"");
        wordToList.get(category).add(param);
        allParamRef.put(param.trim(),category);
        loggedParams.add(s1);
    }
    public String getALoggedParam(){return  loggedParams.getRNDElement();}
}
 

fukurou

the supreme coder
ADMIN
Python:
import random
import re

class ChatBot:
    
    def __init__(self, logParamLim):
        self.sentences = RefreshQ()
        self.wordToList = {}
        self.rand = random.Random()
        self.regexUtil = RegexUtil()
        self.allParamRef = {}
        self.paramLim = 5
        self.loggedParams = RefreshQ()
        self.conjuration = "is a"
        self.loggedParams.setLimit(logParamLim)
    
    def setConjuration(self, conjuration):
        self.conjuration = conjuration
    
    def setSentencesLim(self, lim):
        self.sentences.setLimit(lim)
    
    def setParamLim(self, paramLim):
        self.paramLim = paramLim
    
    def getWordToList(self):
        return self.wordToList
    
    def talk(self):
        result = self.sentences.getRNDElement()
        return self.clearRecursion(result)
    
    def clearRecursion(self, result):
        params = self.regexUtil.extractAllRegexes("(\\w+)(?= #)", result)
        for strI in params:
            temp = self.wordToList.get(strI)
            s1 = temp.getRNDElement()
            result = result.replace(strI + " #", s1)
        if "#" not in result:
            return result
        else:
            return self.clearRecursion(result)
    
    def addParam(self, category, value):
        if category not in self.wordToList:
            temp = RefreshQ()
            temp.setLimit(self.paramLim)
            self.wordToList[category] = temp
        self.wordToList[category].add(value)
        self.allParamRef[value] = category
    
    def addParam(self, kv):
        if kv.getKey() not in self.wordToList:
            temp = RefreshQ()
            temp.setLimit(self.paramLim)
            self.wordToList[kv.getKey()] = temp
        self.wordToList[kv.getKey()].add(kv.getValue())
        self.allParamRef[kv.getValue()] = kv.getKey()
    
    def addSentence(self, sentence):
        self.sentences.add(sentence)
    
    def learn(self, s1):
        for key in self.wordToList.keys():
            s1 = s1.replace(key, "{} #".format(key))
        self.sentences.add(s1)
    
    def learnV2(self, s1):
        for key in self.allParamRef.keys():
            s1 = s1.replace(key, "{} #".format(self.allParamRef[key]))
        self.sentences.add(s1)
    
    def learnParam(self, s1):
        if self.conjuration not in s1:
            return
        category = self.regexUtil.afterWord(self.conjuration, s1)
        if category not in self.wordToList:
            return
        param = s1.replace("{} {}".format(self.conjuration, category), "")
        self.wordToList[category].add(param)
        self.allParamRef[param.strip()] = category
        self.loggedParams.add(s1)
    
    def getALoggedParam(self):
        return self.loggedParams.getRNDElement()
 

fukurou

the supreme coder
ADMIN
Python:
class ChatBot:
    """
chatbot = ChatBot(5)

chatbot.addParam("name", "jinpachi")
chatbot.addParam("name", "sakura")
chatbot.addParam("verb", "eat")
chatbot.addParam("verb", "code")

chatbot.addSentence("i can verb #")

chatbot.learnParam("ryu is a name")
chatbot.learnParam("ken is a name")
chatbot.learnParam("drink is a verb")
chatbot.learnParam("rest is a verb")

chatbot.learnV2("hello ryu i like to code")
chatbot.learnV2("greetings ken")
for i in range(1, 10):
    print(chatbot.talk())
    print(chatbot.getALoggedParam())
"""
    def __init__(self, logParamLim):
        self.sentences:RefreshQ = RefreshQ(5)
        self.wordToList:dict[str,RefreshQ] = {}
        self.rand = random.Random()
        self.regexUtil:RegexUtil = RegexUtil()
        self.allParamRef:dict[str,str] = {}
        self.paramLim:int = 5
        self.loggedParams:RefreshQ = RefreshQ(5)
        self.conjuration:str = "is a"
        self.loggedParams.setLimit(logParamLim)

    def setConjuration(self, conjuration):
        self.conjuration = conjuration

    def setSentencesLim(self, lim):
        self.sentences.setLimit(lim)

    def setParamLim(self, paramLim):
        self.paramLim = paramLim

    def getWordToList(self):
        return self.wordToList

    def talk(self):
        result = self.sentences.getRNDElement()
        return self.clearRecursion(result)

    def clearRecursion(self, result):
        params = self.regexUtil.extractAllRegexes("(\\w+)(?= #)", result)
        for strI in params:
            temp = self.wordToList.get(strI)
            s1 = temp.getRNDElement()
            result = result.replace(strI + " #", s1)
        if "#" not in result:
            return result
        else:
            return self.clearRecursion(result)

    def addParam(self, category, value):
        if category not in self.wordToList:
            temp = RefreshQ(self.paramLim)
            self.wordToList[category] = temp
        self.wordToList[category].insert(value)
        self.allParamRef[value] = category

    def addKeyValueParam(self, kv):
        if kv.getKey() not in self.wordToList:
            temp = RefreshQ(self.paramLim)
            self.wordToList[kv.getKey()] = temp
        self.wordToList[kv.getKey()].add(kv.getValue())
        self.allParamRef[kv.getValue()] = kv.getKey()

    def addSentence(self, sentence):
        self.sentences.insert(sentence)

    def learn(self, s1):
        for key in self.wordToList.keys():
            s1 = s1.replace(key, "{} #".format(key))
        self.sentences.insert(s1)

    def learnV2(self, s1):
        for key in self.allParamRef.keys():
            s1 = s1.replace(key, "{} #".format(self.allParamRef[key]))
        self.sentences.insert(s1)

    def learnParam(self, s1):
        if self.conjuration not in s1:
            return
        category = self.regexUtil.afterWord(self.conjuration, s1)
        if category not in self.wordToList:
            return
        param = s1.replace("{} {}".format(self.conjuration, category), "")
        self.wordToList[category].insert(param)
        self.allParamRef[param.strip()] = category
        self.loggedParams.insert(s1)

    def getALoggedParam(self):
        return self.loggedParams.getRNDElement()
 
Top