public Tree transformTree(Tree tree) {
   Label lab = tree.label();
   if (tree.isLeaf()) {
     Tree leaf = tf.newLeaf(lab);
     leaf.setScore(tree.score());
     return leaf;
   }
   String s = lab.value();
   s = treebankLanguagePack().basicCategory(s);
   int numKids = tree.numChildren();
   List<Tree> children = new ArrayList<Tree>(numKids);
   for (int cNum = 0; cNum < numKids; cNum++) {
     Tree child = tree.getChild(cNum);
     Tree newChild = transformTree(child);
     // cdm 2007: for just subcategory stripping, null shouldn't happen
     // if (newChild != null) {
     children.add(newChild);
     // }
   }
   // if (children.isEmpty()) {
   //   return null;
   // }
   CategoryWordTag newLabel = new CategoryWordTag(lab);
   newLabel.setCategory(s);
   if (lab instanceof HasTag) {
     String tag = ((HasTag) lab).tag();
     tag = treebankLanguagePack().basicCategory(tag);
     newLabel.setTag(tag);
   }
   Tree node = tf.newTreeNode(newLabel, children);
   node.setScore(tree.score());
   return node;
 }
    public Tree transformTree(Tree tree) {
      Label lab = tree.label();
      if (tree.isLeaf()) {
        Tree leaf = tf.newLeaf(lab);
        leaf.setScore(tree.score());
        return leaf;
      }
      String s = lab.value();
      s = treebankLanguagePack().basicCategory(s);
      s = treebankLanguagePack().stripGF(s);
      int numKids = tree.numChildren();
      List<Tree> children = new ArrayList<Tree>(numKids);
      for (int cNum = 0; cNum < numKids; cNum++) {
        Tree child = tree.getChild(cNum);
        Tree newChild = transformTree(child);
        children.add(newChild);
      }
      CategoryWordTag newLabel = new CategoryWordTag(lab);
      newLabel.setCategory(s);
      if (lab instanceof HasTag) {
        String tag = ((HasTag) lab).tag();
        tag = treebankLanguagePack().basicCategory(tag);
        tag = treebankLanguagePack().stripGF(tag);

        newLabel.setTag(tag);
      }
      Tree node = tf.newTreeNode(newLabel, children);
      node.setScore(tree.score());
      return node;
    }
 /**
  * Add -TMP when not present within an NP
  *
  * @param tree The tree to add temporal info to.
  */
 private void addTMP9(final Tree tree) {
   // do the head chain under it
   Tree ht = headFinder.determineHead(tree);
   // special fix for possessives! -- make noun before head
   if (ht.value().equals("POS")) {
     int j = tree.objectIndexOf(ht);
     if (j > 0) {
       ht = tree.getChild(j - 1);
     }
   }
   // Note: this next bit changes the tree label, rather
   // than creating a new tree node.  Beware!
   if (ht.isPreTerminal()
       || ht.value().startsWith("NP")
       || ht.value().startsWith("PP")
       || ht.value().startsWith("ADVP")) {
     if (!TmpPattern.matcher(ht.value()).matches()) {
       LabelFactory lf = ht.labelFactory();
       // System.err.println("TMP: Changing " + ht.value() + " to " +
       //                   ht.value() + "-TMP");
       ht.setLabel(lf.newLabel(ht.value() + "-TMP"));
     }
     if (ht.value().startsWith("NP")
         || ht.value().startsWith("PP")
         || ht.value().startsWith("ADVP")) {
       addTMP9(ht);
     }
   }
   // do the NPs under it (which may or may not be the head chain
   Tree[] kidlets = tree.children();
   for (int k = 0; k < kidlets.length; k++) {
     ht = kidlets[k];
     LabelFactory lf;
     if (tree.isPrePreTerminal() && !TmpPattern.matcher(ht.value()).matches()) {
       // System.err.println("TMP: Changing " + ht.value() + " to " +
       //                   ht.value() + "-TMP");
       lf = ht.labelFactory();
       // Note: this next bit changes the tree label, rather
       // than creating a new tree node.  Beware!
       ht.setLabel(lf.newLabel(ht.value() + "-TMP"));
     } else if (ht.value().startsWith("NP")) {
       // don't add -TMP twice!
       if (!TmpPattern.matcher(ht.value()).matches()) {
         lf = ht.labelFactory();
         // System.err.println("TMP: Changing " + ht.value() + " to " +
         //                   ht.value() + "-TMP");
         // Note: this next bit changes the tree label, rather
         // than creating a new tree node.  Beware!
         ht.setLabel(lf.newLabel(ht.value() + "-TMP"));
       }
       addTMP9(ht);
     }
   }
 }
예제 #4
0
 List<Tree> prune(List<Tree> treeList, Label label, int start, int end) {
   // get reference tree
   if (treeList.size() == 1) {
     return treeList;
   }
   Tree testTree = treeList.get(0).treeFactory().newTreeNode(label, treeList);
   int goal = Numberer.getGlobalNumberer("states").number(label.value());
   Tree tempTree = parser.extractBestParse(goal, start, end);
   // parser.restoreUnaries(tempTree);
   Tree pcfgTree = debinarizer.transformTree(tempTree);
   Set<Constituent> pcfgConstituents =
       pcfgTree.constituents(new LabeledScoredConstituentFactory());
   // delete child labels that are not in reference but do not cross reference
   List<Tree> prunedChildren = new ArrayList<Tree>();
   int childStart = 0;
   for (int c = 0, numCh = testTree.numChildren(); c < numCh; c++) {
     Tree child = testTree.getChild(c);
     boolean isExtra = true;
     int childEnd = childStart + child.yield().size();
     Constituent childConstituent =
         new LabeledScoredConstituent(childStart, childEnd, child.label(), 0);
     if (pcfgConstituents.contains(childConstituent)) {
       isExtra = false;
     }
     if (childConstituent.crosses(pcfgConstituents)) {
       isExtra = false;
     }
     if (child.isLeaf() || child.isPreTerminal()) {
       isExtra = false;
     }
     if (pcfgTree.yield().size() != testTree.yield().size()) {
       isExtra = false;
     }
     if (!label.value().startsWith("NP^NP")) {
       isExtra = false;
     }
     if (isExtra) {
       System.err.println(
           "Pruning: "
               + child.label()
               + " from "
               + (childStart + start)
               + " to "
               + (childEnd + start));
       System.err.println("Was: " + testTree + " vs " + pcfgTree);
       prunedChildren.addAll(child.getChildrenAsList());
     } else {
       prunedChildren.add(child);
     }
     childStart = childEnd;
   }
   return prunedChildren;
 }
예제 #5
0
 private static void taggedLeafLabels(Tree t, List<CoreLabel> l) {
   if (t.isPreTerminal()) {
     CoreLabel fl = (CoreLabel) t.getChild(0).label();
     fl.set(TagLabelAnnotation.class, t.label());
     l.add(fl);
   } else {
     Tree[] kids = t.children();
     for (int j = 0, n = kids.length; j < n; j++) {
       taggedLeafLabels(kids[j], l);
     }
   }
 }
예제 #6
0
 /** 将决策树输出到标准输出 */
 static void outputDecisionTree(Object obj, int level, Object from) {
   for (int i = 0; i < level; i++) System.out.print("|-----");
   if (from != null) System.out.printf("(%s):", from);
   if (obj instanceof Tree) {
     Tree tree = (Tree) obj;
     String attrName = tree.getAttribute();
     System.out.printf("[%s = ?]\n", attrName);
     for (Object attrValue : tree.getAttributeValues()) {
       Object child = tree.getChild(attrValue);
       outputDecisionTree(child, level + 1, attrName + " = " + attrValue);
     }
   } else {
     System.out.printf("[CATEGORY = %s]\n", obj);
   }
 }
예제 #7
0
  // process a statement of the form: connect hostname/port
  private void executeConnect(CommonTree ast) {
    int portNumber = Integer.parseInt(ast.getChild(1).getText());
    Tree idList = ast.getChild(0);

    StringBuilder hostName = new StringBuilder();
    int idCount = idList.getChildCount();
    for (int idx = 0; idx < idCount; idx++) {
      hostName.append(idList.getChild(idx).getText());
    }

    // disconnect current connection, if any.
    // This is a no-op, if you aren't currently connected.
    CliMain.disconnect();

    // now, connect to the newly specified host name and port
    css_.hostName = hostName.toString();
    css_.thriftPort = portNumber;
    CliMain.connect(css_.hostName, css_.thriftPort);
  }
예제 #8
0
  private boolean step(Tree code) {
    if (code.getData().equals(FUN)) return false;

    if (code.getData().equals("")
        && code.getChildren().size() >= 1
        && code.getChild(0).getData().equals(FUN)) {
      // This is an APPLY -- make sure it works.
      Tree fn = code.getChild(0);
      Vector<Tree> fnargs = fn.getChildren();
      Tree body = fnargs.get(1);
      fnargs = fnargs.get(0).getChildren();

      Vector<Tree> inargs = code.getChildren();
      inargs.remove(0);
      for (Tree t : inargs) {
        if (step(t)) return true;
      }

      if (fnargs.size() != inargs.size()) {
        return false;
      }

      for (int i = 0; i < fnargs.size(); i++) {
        replace(body, fnargs.get(i).getData(), inargs.get(i));
      }

      code.removeChildren(false);
      for (Tree t : body.getChildren()) {
        code.addChild(t, false);
      }
      code.setData(body.getData());

      return true;
    } else if (code.getData().equals(IF)) {
      // IF
      Vector<Tree> ifargs = code.getChildren();
      if (step(ifargs.get(0))) return true;

      if (ifargs.get(0).getData().equals("0")) { // "0" is our only false value
        System.out.println("FALSE IF");
        code.removeChildren(false);

        for (Tree kkid : ifargs.get(2).getChildren()) {
          code.addChild(kkid, false);
        }

        code.setData(ifargs.get(2).getData());
      } else {
        System.out.println("TRUE IF: " + ifargs.get(0));
        code.removeChildren(false);

        for (Tree kkid : ifargs.get(1).getChildren()) {
          code.addChild(kkid, false);
        }

        code.setData(ifargs.get(1).getData());
      }
      return true;
    }

    for (Tree kid : code.getChildren()) {
      if (step(kid)) {
        System.out.println("stepped " + kid);
        return true;
      }
    }

    if (code.getData().equals("<")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      if (left < right) {
        code.setData("1");
      } else {
        code.setData("0");
      }

      return true;
    } else if (code.getData().equals(">")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      if (left > right) {
        code.setData("1");
      } else {
        code.setData("0");
      }

      return true;
    } else if (code.getData().equals("=")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      if (left.equals(right)) {
        code.setData("1");
      } else {
        code.setData("0");
      }

      return true;
    } else if (code.getData().equals("+")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      code.setData("" + (left + right));

      return true;
    } else if (code.getData().equals("-")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      code.setData("" + (left - right));

      return true;
    } else if (code.getData().equals("*")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      code.setData("" + (left * right));

      return true;
    } else if (code.getData().equals("/")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      code.setData("" + (left / right));

      return true;
    } else if (code.getData().equals("^")) {
      Vector<Tree> ltargs = code.getChildren();
      Double left = Double.parseDouble(ltargs.get(0).getData());
      Double right = Double.parseDouble(ltargs.get(1).getData());

      code.removeChildren(false);
      code.setData("" + Math.pow(left, right));

      return true;
    }

    System.out.println("Couldn't step: " + code.getData());
    return false;
  }