예제 #1
0
  public void updateNodeName(String oldName, String newName) {
    // Update in the whole Tree
    if (!oldName.isEmpty() && !newName.isEmpty()) {

      Enumeration en = root.depthFirstEnumeration();

      while (en.hasMoreElements()) {
        DecisionTreeNode node = (DecisionTreeNode) en.nextElement();
        if (node.nodeName.compareTo(oldName) == 0) {
          node.nodeName = newName;
        }
      }
    }

    // Update in the added Node List
    ArrayList<String> nodesToBeAdded = new ArrayList<String>(addedNodes);
    addedNodes.clear();

    for (String node : nodesToBeAdded) {
      String[] arr = node.split(":");
      if (arr[1].compareTo(oldName) == 0) {
        addedNodes.add(arr[0] + ":" + newName + ":" + arr[2]);
      } else {
        addedNodes.add(node);
      }
    }

    // Update in the correctNode list
    correctNodes.put(newName, correctNodes.get(oldName));
    correctNodes.remove(oldName);
  }
예제 #2
0
 public DecisionTreeNode cloneTree(DecisionTreeNode subRoot, DecisionTreeNode sourceTree) {
   if (sourceTree == null) {
     return subRoot;
   }
   for (int i = 0; i < sourceTree.getChildCount(); i++) {
     DecisionTreeNode child = (DecisionTreeNode) sourceTree.getChildAt(i);
     DecisionTreeNode clone = new DecisionTreeNode(child); // better than toString()
     subRoot.add(clone);
     cloneTree(clone, child);
   }
   return subRoot;
 }
예제 #3
0
  public void getAll() {
    System.out.println("Printing Tree");
    Enumeration en = root.breadthFirstEnumeration();
    while (en.hasMoreElements()) {
      DecisionTreeNode node = (DecisionTreeNode) en.nextElement();
      System.out.println("" + node.getUserObject() + " => " + node.nodeName);
    }

    System.out.println("\nPrinting Added name list");
    for (String s : addedNodes) {
      System.out.println(s);
    }

    System.out.println("\nPrinting correctNode list");
    System.out.println(correctNodes.toString());
  }
예제 #4
0
  // Method to delete a Node - used when a node from the graph is deleted
  public void deleteLeavesOfNode(String nodeName) {

    // Currently, to delete a node, we basically recreate the whole tree except that node

    /*
    if (!nodeName.isEmpty()) {
        Enumeration en = root.depthFirstEnumeration();
        ArrayList<DecisionTreeNode> ll = new ArrayList();
        while (en.hasMoreElements()) {
            DecisionTreeNode node = (DecisionTreeNode) en.nextElement();
            if (node.nodeName.compareTo(nodeName) == 0) {
                ll.add(node);
            }
        }

        for (DecisionTreeNode i : ll) {
            DecisionTreeNode n = (DecisionTreeNode) i.getParent();
            i.removeFromParent();
            if (n.getChildCount() == 0) {
                n.removeFromParent();
            }
        }
    }*/

    // Make the tree empty
    root.removeAllChildren();
    ArrayList<String> nodesToBeAdded = new ArrayList<String>(addedNodes);
    addedNodes.clear();

    for (String node : nodesToBeAdded) {
      String[] arr = node.split(":");
      if (arr[1].compareTo(nodeName) != 0) {
        add(arr[0], arr[0], Boolean.getBoolean(arr[2]));
      }
    }
  }
예제 #5
0
  /*
   * Does the real work of adding a word to the trie
   */
  private int add(DecisionTreeNode root, String word, String nodeName, boolean isCorrect) {
    word = word.trim();
    // Check if parent already have correct child for this node
    if (correctNodes.get(nodeName) != null && correctNodes.get(nodeName) == 1 && isCorrect) {
      return 1; // One represents duplicate Correct description
    }

    if (root.nodeName.length() == 0 && root.getChildCount() == 0) {
      root.add(new DecisionTreeNode(word, nodeName, isCorrect));

      if (isCorrect) {
        correctNodes.put(nodeName, 1);
      }

      return 0;
    }

    DecisionTreeNode targetNode = null;
    String temp = "", match = "";
    Enumeration en = root.children();

    while (en.hasMoreElements()) {
      DecisionTreeNode node = (DecisionTreeNode) en.nextElement();
      temp = StringUtils.getCommonPrefix(new String[] {word, node.getUserObject().toString()});
      if (temp.length() > match.length()) {
        match = temp;
        targetNode = node;
      }
    }

    // Process matched prefix to make sure it doesn't end at a stop word
    match = processMatchWord(match);

    // System.out.println("Found match as: "+match);
    // Check for duplicate entries - word should have something to insert
    if (word.substring(match.length()).length() != 0) {
      // Match is not zero means we need to add on existing root
      if (match.length() > 1 && !isStopWorld(match)) {
        // Now either root will need to be updated or it will be same - if updated we need to create
        // 2 childs
        if (targetNode.getUserObject().toString().length() != match.length()) {

          if (targetNode.getUserObject().toString().substring(match.length() + 1).lastIndexOf(" ")
                  != -1
              && !isStopWorld(targetNode.getUserObject().toString().substring(match.length()))) {
            // Create 2 Nodes
            DecisionTreeNode newRoot = new DecisionTreeNode(match, nodeName, true);
            DecisionTreeNode nodetwo =
                new DecisionTreeNode(word.substring(match.length()).trim(), nodeName, isCorrect);

            // If target node has child, then they should be linked to the nodeone
            newRoot.nodeName = "non-leaf";
            root.insert(newRoot, root.getChildCount());

            targetNode.setUserObject(
                targetNode.getUserObject().toString().substring(match.length()).trim());
            newRoot.add(targetNode);
            newRoot.add(nodetwo);

            // Adding to correct Description hashmap
            if (isCorrect) {
              correctNodes.put(nodeName, 1);
            }

          } else {

            // Create only 1 node - as the word is a stop word

            DecisionTreeNode nodeone =
                new DecisionTreeNode(word.substring(match.length()).trim(), nodeName, isCorrect);
            String residual =
                targetNode.getUserObject().toString().substring(match.length()).trim();

            en = targetNode.children();

            while (en.hasMoreElements()) {
              DecisionTreeNode node = (DecisionTreeNode) en.nextElement();
              node.setUserObject(residual + " " + node.getUserObject().toString());
            }
            targetNode.setUserObject(match);
            targetNode.nodeName = "non-leaf";
            targetNode.add(nodeone);
            if (isCorrect) {
              correctNodes.put(nodeName, 1);
            }
          }
        } else {
          // If node has no children then just update the node
          if (targetNode.isLeaf()) {
            targetNode.setUserObject(word);

            if (targetNode.isCorrect) {
              if (!isCorrect) {
                correctNodes.put(nodeName, correctNodes.get(nodeName) - 1);
              }
            } else {
              if (isCorrect) {
                correctNodes.put(nodeName, 1);
              } else {
                correctNodes.put(nodeName, correctNodes.get(nodeName) - 1);
              }
            }

            targetNode.isCorrect = isCorrect;
            return 0;
          }

          // When current root matches then start searching its children - recursively

          add(targetNode, word.substring(match.length()).trim(), nodeName, isCorrect);
        }
      } else {
        // Create another root at the top level and link it
        root.insert(new DecisionTreeNode(word, nodeName, isCorrect), root.getChildCount());
        if (isCorrect) {
          correctNodes.put(nodeName, 1);
        }
      }
    } else {
      // Check what type of node is being updated - Correct or Incorrect
      if (targetNode.isCorrect != isCorrect) {

        targetNode.isCorrect = isCorrect;
        if (isCorrect) {
          correctNodes.put(nodeName, 1);
        } else {
          correctNodes.put(nodeName, correctNodes.get(nodeName) - 1);
        }
      }
    }
    return 0;
  }