The link tied to this post is what I have to do. I could do this if I all the answers and questions were already established. My problem is that I don't know how to make the program start from scratch and make it "learn". This is my code as of right now:

    import java.util.*;
    public class assignment8{
      static treeNode root = new treeNode("Does it have legs?", null, null);
      public static void main(String[] args){
        root.left = new treeNode("Is it a cat?", null, null);
        root.right = new treeNode("Is it a snake?", null, null);
        guess();
      }
      static void guess(){
        Scanner scan = null;
        try{
          scan = new Scanner(System.in);
        }
        catch (Exception e){
          System.out.println(e);
        }
        System.out.println(root.data);
        String ques = scan.next();
        if(ques.equals("yes")){
          root = root.left;
          System.out.println(root.data);
          String ques2 = scan.next();
          if(ques2.equals("yes")){
            System.out.println("I win! Continue?");
          }else if(ques2.equals("no")){
            System.out.println("I give up. What is it?");
            String ans = scan.next();
            System.out.println("Please type a question that is yes for " + ans + " and no for cat");
            String newQues = scan.next();
            root.right = new treeNode(newQues, null, null);
            System.out.println("Continue? yes/no");
            String cont =  scan.next();
            if(cont.equals("yes")){
              guess();
            }
          }
        }else if(ques.equals("no")){
          root = root.right;
          System.out.println(root.data);
        }
      }
    }
    class treeNode{
      String data;
      treeNode left;
      treeNode right;

      treeNode(String newdata, treeNode left, treeNode right){
        this.data = newdata;
        this.left = left;
        this.right = right;
      }
    }

I realize that it's all over the place. To be honest I'm quite stuck as to how to make the program "learn" as more people play it. Any help would be appreciated. I know that this is a lot to read and I apologize. Thanks in advance to any that help.

Dani AI

Generated

your program only needs two things to “learn”: (1) when it guesses wrong, replace that leaf with a new question node that separates the old guess from the new answer; (2) save the tree to disk and reload it next time. As said, you can skip ObjectOutputStream and use a plain text format. This is not machine learning in the statistical sense; it is just growing a binary decision tree over time.

A simple file format is preorder lines like: Q|Does it have legs? and A|cat. Save questions with both children; save answers as leaves. Load by reading a line, creating a node, and if it is a question, recursively load its left then right child.

static void save(treeNode n, PrintWriter out) {
  if (n.left == null && n.right == null) out.println("A|" + n.data);
  else { out.println("Q|" + n.data); save(n.left, out); save(n.right, out); }
}

static treeNode load(Scanner in) {
  if (!in.hasNextLine()) return null;
  String line = in.nextLine();
  char type = line.charAt(0);
  String text = line.substring(2); // skip "Q|" or "A|"
  treeNode n = new treeNode(text, null, null);
  if (type == 'Q') { n.left = load(in); n.right = load(in); }
  return n;
}

static treeNode learn(treeNode wrongLeaf, String correct, String question, boolean yesForCorrect) {
  treeNode newAns = new treeNode(correct, null, null);
  return yesForCorrect ? new treeNode(question, newAns, wrongLeaf)
                       : new treeNode(question, wrongLeaf, newAns);
}

Tips: use nextLine() to read full answers/questions; normalize input to yes/no. Do not mutate root as you traverse (eg, root = root.left); instead recurse with a current node and return the possibly-updated subtree, or keep a parent reference. On startup try to load the file; if missing, start with a single A|your first guess. On exit, save the tree.

Recommended Answers

All 5 Replies

I'm not sure what the real question is here, so here's an answer that may or may not hit the target, but at least it may help clarify the question.
As the user enters new questions you add them to your Tree structure(s). At the end of each run you write all that data to a file, ready to read in at the start of the next run. You can use an ObjectOutputStream to write the whole tree structure in simgle method call.

How would I do that? I'm a beginning programmer so I apoplogize if my questions are a bit...well, stupid.

"How would I do that?" is a very big question! Try taking it one small step at a time see how it goes.

ps: There are lots of beginners here, so don't worry about asking about simple things. The only "stupid" questions are ones so vague or incomplete that nobody knows to to start answering them.

I should have been more specific. I meant how would I do the ObjectOutputStream part. That's my bad. Also, I probably should have mentioned this in my last post but we haven't gone over ObjectOutputStream so I don't think I can use that. Any other suggestions?

e you can just print your data to an ordinary text file, then re-construct your data when you read the text back in. Google is your friend, closely followed bythe Oracle Java tutorials and the Java API documentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.