battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionNeuro Sama persistent question module source code EmptyNeuro Sama persistent question module source code

more_horiz

Code:


package AXJava;

import java.util.Hashtable;

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);
    }
}


main:

Code:

       PersistantQuestion persistantQuestion = new PersistantQuestion();
        persistantQuestion.addPath("yes",new DrawRnd("I love you", "do you love me?","please do you love me","you love me don't you ?"));
        persistantQuestion.addPath("no",new DrawRnd("you're annoying", "I'm leaving","good bye","you love me don't you ?"));
        persistantQuestion.activate();
        for (int i = 0; i < 10; i++) {
            System.out.println(persistantQuestion.process(""));
        }
        persistantQuestion.activate();
        System.out.println(persistantQuestion.process(""));
        System.out.println("answering no:");
        System.out.println(persistantQuestion.process("no"));
        persistantQuestion.activate();
        for (int i = 0; i < 10; i++) {
            System.out.println(persistantQuestion.process(""));
        }
    }


output:

you love me don't you ?

I love you

do you love me?

please do you love me

i see

do you love me?
answering no:
okay

I'm leaving

good bye

you're annoying

you love me don't you ?

i see

Process finished with exit code 0
:s43:

Last edited by Moti Barski on Fri Jun 09, 2023 5:22 pm; edited 3 times in total (Reason for editing : code buff)

descriptionNeuro Sama persistent question module source code EmptyRe: Neuro Sama persistent question module source code

more_horiz

descriptionNeuro Sama persistent question module source code EmptyRe: Neuro Sama persistent question module source code

more_horiz
I've updated the module with a log func. unlike process, it can log any answer.
sometimes an answer is yes no maybe, sometimes it's some parameter like a food ingredient, so you can
use process() or log()

Code:

package AXJava;

import java.util.Hashtable;

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) {
        this.mode = mode;
    }
}
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply