コード例 #1
0
ファイル: DecisionTree.java プロジェクト: rptiwari/LAITS
  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());
  }
コード例 #2
0
ファイル: DecisionTree.java プロジェクト: rptiwari/LAITS
  /*
   * 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;
  }