Example usage for edu.stanford.nlp.trees Tree valueOf

List of usage examples for edu.stanford.nlp.trees Tree valueOf

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Tree valueOf.

Prototype

public static Tree valueOf(String str) 

Source Link

Document

This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString() , pennPrint() , or as in the Penn Treebank).

Usage

From source file:pltag.parser.semantics.SemanticsOraclePerformance.java

License:Open Source License

@Override
public double[] add(SemanticsWidget nbestAnalyses, SemanticsWidget goldStandard, String name) {
    Tree evalGold = treeCollinizer.transformTree(Tree.valueOf(goldStandard.getNBestTrees()[0]));
    int maxPos = 0, curPos = 0;
    double curF1, maxF1 = Double.NEGATIVE_INFINITY;
    String nBestAnalysis = null;/*  w ww  .  j a  v  a2 s.  c o m*/

    for (String analysis : nbestAnalyses.getNBestTrees()) {
        Tree curEvalGuess = treeCollinizer.transformTree(Tree.valueOf(analysis));
        curEvalb.evaluate(curEvalGuess, evalGold, name);
        curF1 = curEvalb.getLastF1();
        if (curF1 > maxF1) {
            maxF1 = curF1;
            maxPos = curPos;
            nBestAnalysis = analysis;
        }
        curPos++;
    }
    if (nBestAnalysis != null) {
        accuracy += maxPos == 0 ? 1 : 0;
        nbestAnalyses.getNBestTrees()[0] = nBestAnalysis; // hack: set the best analysis as the top-most in the list and call super-method to compute rest metrics        
        double[] results = Arrays.copyOf(super.add(nbestAnalyses, goldStandard, name), 3);
        results[2] = maxPos;
        return results;
    } else {
        double[] results = Arrays.copyOf(super.add(nbestAnalyses, goldStandard, name), 3);
        results[2] = -1;
        return results;
    }
}

From source file:pltag.parser.semantics.SemanticsPerformance.java

License:Open Source License

@Override
public double[] add(SemanticsWidget predAnalysis, SemanticsWidget goldStandard, String name) {
    // compute evalb F1
    String predTree = predAnalysis.getNBestTrees()[0];
    String goldStandardTree = goldStandard.getNBestTrees()[0];
    Tree evalGuess = treeCollinizer.transformTree(Tree.valueOf(predTree));
    Tree evalGold = treeCollinizer.transformTree(Tree.valueOf(goldStandardTree));
    // in case we couldn't parse the tree at all, then abort, otherwise it is going to be an unfair comparison for SRL
    if (!(evalb.evaluate(evalGuess, evalGold, name) || predAnalysis.isBaseline()))
        return new double[] { Double.NaN, Double.NaN };
    //        totalEvalbF1 = evalb.getEvalbF1();

    // compute SRL F1
    if (goldStandard.getPropositions() == null)
        return new double[] { Double.NaN, Double.NaN };
    Map<Predicate, Proposition> goldProps = goldStandard.getPropositions();
    goldPropsCount += goldProps.size();/*  w  w  w  .  j a v  a2  s .  c  om*/
    Map<Predicate, Proposition> predProps = predAnalysis.getPropositions();
    predPropsCount += predProps.size();
    //        if(goldProps.isEmpty() && predProps.isEmpty()) // no SRLs in the sentence. Return 0, but don't count 
    //            return 0.0;
    EvalResult subResult = new EvalResult();
    // Get the things in common (Predicates)
    List<Predicate> goldPreds = new ArrayList();
    goldPreds.addAll(goldProps.keySet());
    Set<Predicate> predPreds = new HashSet();
    predPreds.addAll(predProps.keySet());
    Iterator<Predicate> iterPreds = goldPreds.iterator();
    while (iterPreds.hasNext()) {
        Predicate goldPred = iterPreds.next();
        if (predPreds.contains(goldPred)) {
            addResult(predResult, subResult, true, true); // score +1 for predicate
            predPreds.remove(goldPred);
            iterPreds.remove();
        } else {
            Predicate predPredIgnoreCase = Predicate.getPredicateIgnoreSense(goldPred, predPreds);
            if (predPredIgnoreCase != null) // found correct predicate with wrong sense
            {
                addResult(predResult, subResult, true, false); // add +1 false negative for missed sense
                predPreds.remove(goldPred);
                iterPreds.remove();
            }
        }
    }
    // Mark the differences between the two maps (entries left)
    for (Predicate goldPred : goldPreds) {
        if (Predicate.getPredicateIgnoreSense(goldPred, predPreds) == null) // in case we miss a predicate entirely (not just its sense)
            addResult(predResult, subResult, true, false); // add +1 false negative for missed predicate                      
    }
    for (Predicate predPred : predPreds) {
        if (Predicate.getPredicateIgnoreSense(predPred, goldPreds) == null)
            addResult(predResult, subResult, false, true); // add +1 false positive for missed predicate (hopefully rare)                    
    }
    // Get things in common (Arguments)
    Iterator<Entry<Predicate, Proposition>> iterProps = goldProps.entrySet().iterator();
    while (iterProps.hasNext()) {
        Entry<Predicate, Proposition> goldEntry = iterProps.next();
        Proposition predProp = predProps.get(goldEntry.getKey());
        if (predProp == null) // if we didn't find the correct predicate
        {
            Predicate predPredIgnoreCase = Predicate.getPredicateIgnoreSense(goldEntry.getKey(),
                    predProps.keySet());
            if (predPredIgnoreCase != null) // found correct predicate with wrong sense
            {
                predProp = predProps.get(predPredIgnoreCase);
            }
        }
        if (predProp != null) // continue with identifying correct arguments
        {
            List<Argument> goldArgs = goldEntry.getValue().getArguments();
            List<Argument> predArgs = predProp.getArguments();
            Iterator<Argument> iterArgs = goldArgs.iterator();
            while (iterArgs.hasNext()) {
                Argument goldArg = iterArgs.next();
                if (predArgs.contains(goldArg)) {
                    addResult(argsResult, subResult, true, true); // score +1 for each correct argument
                    int roleIndex = conllRoleIndexer.getIndex(goldArg.getRole());
                    counts[roleIndex][roleIndex]++;
                    correctCounts[roleIndex]++;
                    goldCounts[roleIndex]++;
                    iterArgs.remove();
                    predArgs.remove(goldArg);
                }
            } //  while
            if (predArgs.isEmpty())
                predProps.remove(goldEntry.getKey());
            if (goldArgs.isEmpty())
                iterProps.remove();
        } // if
    } // while 
      // Mark the differences between the two maps (entries left)
    for (Proposition goldProp : goldProps.values()) {
        for (Argument goldArg : goldProp.getArguments()) {
            addResult(argsResult, subResult, true, false); // add +1 false negative for all missed arguments         
            int roleIndex = conllRoleIndexer.getIndex(goldArg.getRole());
            goldCounts[roleIndex]++;
        }
    }
    for (Proposition predProp : predProps.values()) {
        for (int i = 0; i < predProp.getArguments().size(); i++)
            addResult(argsResult, subResult, false, true); // add +1 false positive for all missed arguments       
    }

    // fill in confusion matrix        
    if (propositionsIsEmpty(goldProps.values())) {
        for (Proposition predProp : predProps.values()) {
            for (Argument predArg : predProp.getArguments()) {
                int predRoleIndex = conllRoleIndexer.getIndex(predArg.getRole());
                counts[numOfRoles][predRoleIndex]++;
            }
        }
    }
    //        else if(propositionsIsEmpty(predProps.values()))
    //        {
    //            for(Proposition goldProp : goldProps.values())
    //            {
    //                for(Argument goldArg : goldProp.getArguments())
    //                {
    //                    int goldRoleIndex = conllRoleIndexer.getIndex(goldArg.getRole());
    //                    counts[goldRoleIndex][numOfRoles]++;
    //                }
    //            }
    //        }
    else {
        for (Proposition goldProp : goldProps.values()) // Heuristic: mark an error on all pairs
        {
            Proposition predProp = predProps.get(goldProp.getPredicate());
            if (predProp != null && !predProp.getArguments().isEmpty()) {
                for (Argument goldArg : goldProp.getArguments()) {
                    int goldRoleIndex = conllRoleIndexer.getIndex(goldArg.getRole());
                    for (Argument predArg : predProp.getArguments()) {
                        int predRoleIndex = conllRoleIndexer.getIndex(predArg.getRole());
                        counts[goldRoleIndex][predRoleIndex]++;
                        if (goldRoleIndex == predRoleIndex) {
                            errorsMap.put(name, goldProp.getPredicate() + ": " + goldArg + " - " + predArg);
                        }
                    }
                } // for
            } // if
            else {
                for (Argument goldArg : goldProp.getArguments()) {
                    int goldRoleIndex = conllRoleIndexer.getIndex(goldArg.getRole());
                    counts[goldRoleIndex][numOfRoles]++;
                }
            }
        } // for
    }
    processedExamplesCount++;
    //confMatrix();        
    return new double[] { evalb.getLastF1(), subResult.f1() };
}

From source file:pltag.util.Utils.java

License:Open Source License

public static Map<Integer, String> computeIncrementalTrees(String treeStr) {
    Map<Integer, String> map = new TreeMap<Integer, String>();
    Tree tree = Tree.valueOf(treeStr);
    List<Tree> leaves = tree.getLeaves();
    Tree firstLeaf = leaves.get(0);//from ww w . ja v  a2  s .co m
    // first prefix tree by default is the sub-tree rooted on the preterminal (don't add, as we compute evalb for words>1)
    //        map.put(0, firstLeaf.parent(tree).toString());
    for (int i = 1; i < leaves.size(); i++) {
        Tree lastLeaf = leaves.get(i);
        map.put(i, getMinimalConnectedStructure(tree, firstLeaf, lastLeaf, i).toString());
    }
    return map;
}

From source file:pltag.util.Utils.java

License:Open Source License

public static String computeIncrementalTree(String treeStr) {
    Tree tree = Tree.valueOf(treeStr);
    List<Tree> leaves = tree.getLeaves();
    leaves.get(leaves.size() - 1);//  ww  w .  j a v  a2 s.c  o m
    return computeIncrementalTree(treeStr, leaves.size() - 1).toString();
}

From source file:pltag.util.Utils.java

License:Open Source License

public static String computeIncrementalTree(String treeStr, int leafNo) {
    Tree tree = Tree.valueOf(treeStr);
    List<Tree> leaves = tree.getLeaves();
    Tree firstLeaf = leaves.get(0);/*from  www  .  j  av a2 s.  co  m*/
    Tree lastLeaf = leaves.get(leafNo);
    return getMinimalConnectedStructure(tree, firstLeaf, lastLeaf, leafNo).toString();
}

From source file:pltag.util.Utils.java

License:Open Source License

public static String removeSubtreesAfterWord(String inputTree, int numOfLeaves) {
    Tree tree = Tree.valueOf(inputTree);
    List<Tree> leaves = tree.getLeaves();
    if (leaves.size() > numOfLeaves) {
        // find common ancestor between last valid leaf and extraneous leaf
        Tree firstLeaf = leaves.get(numOfLeaves - 1);
        Tree lastLeaf = leaves.get(leaves.size() - 1);
        Tree commonAncestorNode = lastLeaf.parent(tree);
        while (!commonAncestorNode.getLeaves().contains(firstLeaf)) {
            commonAncestorNode = commonAncestorNode.parent(tree);
        }/*from w w  w  . j  av  a2s.c  o  m*/
        // found the common ancestor, now we need to chop the children nodes the span of which is outwith the last valid leaf

        Tree p = lastLeaf.parent(tree);
        while (p != commonAncestorNode) {
            int numOfChildren = p.numChildren();
            for (int i = 0; i < numOfChildren; i++)
                p.removeChild(0);
            p = p.parent(tree);
        }
        // remove last leftover parent node of the invalid leaf
        commonAncestorNode.removeChild(commonAncestorNode.numChildren() - 1);
        return tree.toString();
    } else {
        return inputTree;
    }

}

From source file:wtute.parser.EssayParser.java

public void commaSplice(List<HasWord> sentence) {
    System.out.println("SPLICEr");
    Tree sentenceTree = lp.parse(sentence);
    Iterator<Tree> iter = sentenceTree.iterator();
    TregexPattern pattern = TregexPattern
            .compile("@S < (@S $+ (/,/ $+ (@NP $+ @VP))) | < (@NP $+ (@VP $+ (/,/ $+ @VP)))");
    TregexPattern patternTwo = TregexPattern.compile("@VP < (@VP $+ (/,/ $+ @VP))");
    TregexPattern patternThree = TregexPattern.compile("@S < (@S $+ (/,/ $+ @S))");
    TregexMatcher matcher = pattern.matcher(sentenceTree);
    TregexMatcher matcherTwo = patternTwo.matcher(sentenceTree);
    TregexMatcher matcherThree = patternThree.matcher(sentenceTree);
    boolean oneFound, twoFound = false, threeFound = false;
    while ((oneFound = matcher.findNextMatchingNode()) || (twoFound = matcherTwo.findNextMatchingNode())
            || (threeFound = matcherThree.findNextMatchingNode())) {
        Tree match;/*from w  ww . j  a  v a2s . c  o  m*/
        if (oneFound) {
            System.out.println("1");
            match = matcher.getMatch();
        } else if (twoFound) {
            System.out.println("2");
            match = matcherTwo.getMatch();
        } else {
            System.out.println("3");
            match = matcherThree.getMatch();
        }

        System.out.println(Sentence.listToString(match.yield()));
        List<Tree> tl = match.preOrderNodeList();

        String errorSect = tl.get(tl.indexOf(Tree.valueOf("(, ,)")) - 1) + ", "
                + Sentence.listToString(tl.get(tl.indexOf(Tree.valueOf("(, ,)")) + 2).yieldWords());
        System.out.println(errorSect);
        xmlc.addError(errorSect, "Comma splice: You have used a comma to separate two individual sentences",
                null, new String[] { "or", "and", "but", "so", " ; ", " . " }, "grammar", "Consider using a "
                        + "conjunction after the comma, a semi-colon or a full-stop to break the sentence.");

    }
}