예제 #1
0
 private static void testParser(Parser parser, List<Tree<String>> testTrees) {
   EnglishPennTreebankParseEvaluator.LabeledConstituentEval<String> eval =
       new EnglishPennTreebankParseEvaluator.LabeledConstituentEval<String>(
           Collections.singleton("ROOT"),
           new HashSet<String>(Arrays.asList(new String[] {"''", "``", ".", ":", ","})));
   for (Tree<String> testTree : testTrees) {
     List<String> testSentence = testTree.getYield();
     if (testSentence.size() > MAX_LENGTH) continue;
     Tree<String> guessedTree = parser.getBestParse(testSentence);
     System.out.println("Guess:\n" + Trees.PennTreeRenderer.render(guessedTree));
     System.out.println("Gold:\n" + Trees.PennTreeRenderer.render(testTree));
     eval.evaluate(guessedTree, testTree);
   }
   eval.display(true);
 }
예제 #2
0
    private static Tree<String> binarizeTree(Tree<String> tree) {
      String label = tree.getLabel();
      if (tree.isLeaf()) return new Tree<String>(label);
      if (tree.getChildren().size() == 1) {
        return new Tree<String>(
            label, Collections.singletonList(binarizeTree(tree.getChildren().get(0))));
      }
      // I think it tries to binarize a binary tree. This is silly. Just binarize the subtrees.
      if (tree.getChildren().size() == 2) {

        List<Tree<String>> children = new ArrayList<Tree<String>>();
        children.add(binarizeTree(tree.getChildren().get(0)));
        children.add(binarizeTree(tree.getChildren().get(1)));
        return new Tree<String>(label, children);
      }

      // otherwise, it's a TERNARY-or-more local tree,
      // so decompose it into a sequence of binary and unary trees.
      String intermediateLabel = "@" + label + "->";
      Tree<String> intermediateTree = binarizeTreeHelper(tree, 0, intermediateLabel);
      return new Tree<String>(label, intermediateTree.getChildren());
    }
예제 #3
0
 private Tree<String> buildTagTree(List<String> words, List<String> tags, int currentPosition) {
   Tree<String> leafTree = new Tree<String>(words.get(currentPosition));
   Tree<String> tagTree =
       new Tree<String>(tags.get(currentPosition), Collections.singletonList(leafTree));
   return tagTree;
 }
예제 #4
0
 private Tree<String> addRoot(Tree<String> tree) {
   return new Tree<String>("ROOT", Collections.singletonList(tree));
 }
예제 #5
0
    public Tree<String> getParseTreeOld(
        List<String> sentence,
        HashMap<String, Triplet<Integer, String, String>> backHash,
        int begin,
        int end,
        String parent) {
      // Start from the root and keep going down till you reach the leafs.
      // System.out.println("In recursion!!");
      if (begin == end - 1) {

        if ((begin + " " + end).equals("0 1")) {
          // System.out.println("CounterMap");
          // System.out.println(parseScores.getCounter(begin+" "+end).toString());
          // backHash.get(begin+ " " + end+ " "+parent);
        }

        // String parent = parseScores.getCounter(begin+" "+end).argMax();

        // System.out.println("Terminal cond :"+begin+ " "+ end+ " "+parent);
        Triplet<Integer, String, String> triplet = backHash.get(begin + " " + end + " " + parent);
        int split = -1;
        if (triplet != null) {
          split = triplet.getFirst();
        }
        if ((begin + " " + end).equals("0 1")) {
          // System.out.println("CounterMap");
          // System.out.println(parseScores.getCounter(begin+" "+end).toString());
          // System.out.println(backHash.get(begin+ " " + end+ " "+parent).toString());
        }

        Tree<String> topTree = new Tree<String>(parent);
        Tree<String> tree = topTree;
        while (triplet != null && split == -1) {

          Tree<String> singleTree = new Tree<String>(triplet.getSecond());
          tree.setChildren(Collections.singletonList(singleTree));

          triplet = backHash.get(begin + " " + end + " " + triplet.getSecond());
          if (triplet != null) {
            split = triplet.getFirst();
          }
          tree = tree.getChildren().get(0);
        }

        // return new Tree<String>(tree.getLabel(), ));
        tree.setChildren(Collections.singletonList(new Tree<String>(sentence.get(begin))));
        return topTree;
      }

      /*if((begin +" " + end).equals("1 5")){
      	System.out.println("CounterMap");
      	System.out.println(parseScores.getCounter(begin+" "+end).toString());
      	//backHash.get(begin+ " " + end+ " "+parent);
      }*/
      // String parent = parseScores.getCounter(begin+" "+end).argMax();
      // System.out.println(parent);
      Triplet<Integer, String, String> triplet = backHash.get(begin + " " + end + " " + parent);
      // System.out.println(triplet.getSecond() + "  " + triplet.getFirst());

      if ((begin + " " + end).equals("0 6")) {
        // System.out.println("CounterMap");
        // System.out.println(parent);
        // System.out.println(backHash.get(begin+ " " + end+ " "+parent).toString());
      }

      if (triplet == null) {
        System.out.println(begin + " " + end + " " + parent);
      }
      int split = triplet.getFirst();
      Tree<String> topTree = new Tree<String>(parent);
      Tree<String> tree = topTree;
      // System.out.println("parent : " +parent);
      while (split == -1) {
        // System.out.println(tree.toString());
        Tree<String> singleTree = new Tree<String>(triplet.getSecond());
        // System.out.println(triplet.getSecond());
        tree.setChildren(Collections.singletonList(singleTree));
        // System.out.println(tree.toString());
        // System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXxx");
        // System.out.println(triplet.getSecond());
        triplet = backHash.get(begin + " " + end + " " + triplet.getSecond());
        if (triplet != null) {
          split = triplet.getFirst();
        }
        tree = tree.getChildren().get(0);
      }
      // System.out.println(tree.toString());

      Tree<String> leftTree =
          getParseTreeOld(sentence, backHash, begin, split, triplet.getSecond());
      Tree<String> rightTree = getParseTreeOld(sentence, backHash, split, end, triplet.getThird());
      // System.out.println("leftTree: "+ leftTree.toString());
      // System.out.println("rightTree :" +rightTree.toString());
      // System.out.println("topTree :"+topTree.toString());
      List<Tree<String>> children = new ArrayList<Tree<String>>();
      children.add(leftTree);
      children.add(rightTree);
      tree.setChildren(children);
      return topTree;
    }
예제 #6
0
    public Tree<String> getParseTree(
        List<String> sentence,
        HashMap<Triplet<Integer, Integer, String>, Triplet<Integer, String, String>> backHash,
        int begin,
        int end,
        String parent) {
      // Start from the root and keep going down till you reach the leafs.
      if (begin == end - 1) {
        Triplet<Integer, String, String> triplet =
            backHash.get(new Triplet<Integer, Integer, String>(begin, end, parent));
        int split = -1;
        if (triplet != null) {
          split = triplet.getFirst();
        }

        Tree<String> topTree = new Tree<String>(parent);
        Tree<String> tree = topTree;
        while (triplet != null && split == -1) {

          Tree<String> singleTree = new Tree<String>(triplet.getSecond());
          tree.setChildren(Collections.singletonList(singleTree));

          triplet =
              backHash.get(new Triplet<Integer, Integer, String>(begin, end, triplet.getSecond()));
          if (triplet != null) {
            split = triplet.getFirst();
          }
          tree = tree.getChildren().get(0);
        }

        tree.setChildren(Collections.singletonList(new Tree<String>(sentence.get(begin))));
        return topTree;
      }

      Triplet<Integer, String, String> triplet =
          backHash.get(new Triplet<Integer, Integer, String>(begin, end, parent));

      if (triplet == null) {
        System.out.println(begin + " " + end + " " + parent);
      }
      int split = triplet.getFirst();
      Tree<String> topTree = new Tree<String>(parent);
      Tree<String> tree = topTree;

      while (split == -1) {
        Tree<String> singleTree = new Tree<String>(triplet.getSecond());
        tree.setChildren(Collections.singletonList(singleTree));
        triplet =
            backHash.get(new Triplet<Integer, Integer, String>(begin, end, triplet.getSecond()));
        if (triplet != null) {
          split = triplet.getFirst();
        }
        tree = tree.getChildren().get(0);
      }

      Tree<String> leftTree = getParseTree(sentence, backHash, begin, split, triplet.getSecond());
      Tree<String> rightTree = getParseTree(sentence, backHash, split, end, triplet.getThird());

      List<Tree<String>> children = new ArrayList<Tree<String>>();
      children.add(leftTree);
      children.add(rightTree);
      tree.setChildren(children);
      return topTree;
    }