Search in sources :

Example 6 with PredicateArgumentView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView in project cogcomp-nlp by CogComp.

the class GoldLabel method addAnnotation.

private void addAnnotation(TextAnnotation ta) {
    Tree<String> tree = ParseUtils.getParseTree(ViewNames.PARSE_GOLD, ta, 0);
    Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree);
    List<Tree<Pair<String, IntPair>>> yield = spanLabeledTree.getYield();
    PredicateArgumentView pav = new PredicateArgumentView(srlViewName, "AnnotatedTreebank", ta, 1.0);
    Set<Integer> predicates = new HashSet<>();
    for (Fields fields : goldFields.get(ta.getId())) {
        Constituent predicate = fields.createPredicate(ta, srlViewName, yield);
        if (predicates.contains(predicate.getStartSpan()))
            continue;
        predicates.add(predicate.getStartSpan());
        List<Constituent> args = new ArrayList<>();
        List<String> labels = new ArrayList<>();
        List<Double> scores = new ArrayList<>();
        // We need to make sure that the One-Argument-Per-Span constraint is
        // respected. Yes sir, we do, even if the data says otherwise!
        Set<IntPair> seenSpans = new HashSet<>();
        for (GoldLabel arg : fields.getGoldLabels()) {
            List<Constituent> aa = arg.getArgument(ta, srlViewName, yield, mergeContiguousCArgs);
            List<Constituent> filtered = new ArrayList<>();
            for (Constituent possibleArg : aa) {
                if (seenSpans.contains(possibleArg.getSpan()))
                    continue;
                seenSpans.add(possibleArg.getSpan());
                filtered.add(possibleArg);
            }
            addArguments(ta, predicate, args, labels, scores, arg, filtered);
        }
        // for each arg
        pav.addPredicateArguments(predicate, args, labels.toArray(new String[labels.size()]), ArrayUtilities.asDoubleArray(scores));
    }
    if (pav.getPredicates().size() > 0)
        ta.addView(srlViewName, pav);
}
Also used : IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) PredicateArgumentView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 7 with PredicateArgumentView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView in project cogcomp-nlp by CogComp.

the class PathLSTMHandler method getSRL.

private PredicateArgumentView getSRL(TextAnnotation ta) throws Exception {
    log.debug("Input: {}", ta.getText());
    PredicateArgumentView pav = new PredicateArgumentView(viewName, "PathLSTMGenerator", ta, 1.0);
    List<String> words = new LinkedList<String>();
    // dummy ROOT token
    words.add("<ROOT>");
    // pre-tokenized text
    words.addAll(Arrays.asList(ta.getTokens()));
    // run SRL
    Sentence parsed = SRLpipeline.parse(words);
    for (Predicate p : parsed.getPredicates()) {
        // skip nominal predicates
        if (p.getPOS().startsWith("N"))
            continue;
        IntPair predicateSpan = new IntPair(p.getIdx() - 1, p.getIdx());
        String predicateLemma = p.getLemma();
        Constituent predicate = new Constituent("Predicate", viewName, ta, predicateSpan.getFirst(), predicateSpan.getSecond());
        predicate.addAttribute(PredicateArgumentView.LemmaIdentifier, predicateLemma);
        String sense = p.getSense();
        predicate.addAttribute(PredicateArgumentView.SenseIdentifer, sense);
        List<Constituent> args = new ArrayList<>();
        List<String> relations = new ArrayList<>();
        for (Word a : p.getArgMap().keySet()) {
            Set<Word> singleton = new TreeSet<Word>();
            String label = p.getArgumentTag(a);
            Yield y = a.getYield(p, label, singleton);
            IntPair span = new IntPair(y.first().getIdx() - 1, y.last().getIdx());
            assert span.getFirst() <= span.getSecond() : ta;
            args.add(new Constituent(label, viewName, ta, span.getFirst(), span.getSecond()));
            relations.add(label);
        }
        pav.addPredicateArguments(predicate, args, relations.toArray(new String[relations.size()]), new double[relations.size()]);
    }
    return pav;
}
Also used : Word(se.lth.cs.srl.corpus.Word) ArrayList(java.util.ArrayList) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) PredicateArgumentView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView) LinkedList(java.util.LinkedList) Predicate(se.lth.cs.srl.corpus.Predicate) TreeSet(java.util.TreeSet) Yield(se.lth.cs.srl.corpus.Yield) Sentence(se.lth.cs.srl.corpus.Sentence) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 8 with PredicateArgumentView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView in project cogcomp-nlp by CogComp.

the class CommaLabeler method addView.

@Override
public void addView(TextAnnotation ta) throws AnnotatorException {
    // Check that we have the required views
    for (String requiredView : requiredViews) {
        if (!ta.hasView(requiredView))
            throw new AnnotatorException("Missing required view " + requiredView);
    }
    // Create the Comma structure
    CommaSRLSentence sentence = new CommaSRLSentence(ta, ta);
    PredicateArgumentView srlView = new PredicateArgumentView(viewName, "illinois-comma", ta, 1.0d);
    for (Comma comma : sentence.getCommas()) {
        String label = classifier.discreteValue(comma);
        int position = comma.getPosition();
        Constituent predicate = new Constituent("Predicate:" + label, viewName, ta, position, position + 1);
        predicate.addAttribute(PredicateArgumentView.SenseIdentifer, label);
        srlView.addConstituent(predicate);
        Constituent leftArg = comma.getPhraseToLeftOfComma(1);
        if (leftArg != null) {
            Constituent leftArgConst = new Constituent(leftArg.getLabel(), viewName, ta, leftArg.getStartSpan(), leftArg.getEndSpan());
            srlView.addConstituent(leftArgConst);
            srlView.addRelation(new Relation("LeftOf" + label, predicate, leftArgConst, 1.0d));
        }
        Constituent rightArg = comma.getPhraseToRightOfComma(1);
        if (rightArg != null) {
            Constituent rightArgConst = new Constituent(rightArg.getLabel(), viewName, ta, rightArg.getStartSpan(), rightArg.getEndSpan());
            srlView.addConstituent(rightArgConst);
            srlView.addRelation(new Relation("RightOf" + label, predicate, rightArgConst, 1.0d));
        }
    }
    ta.addView(viewName, srlView);
}
Also used : Comma(edu.illinois.cs.cogcomp.comma.datastructures.Comma) Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) CommaSRLSentence(edu.illinois.cs.cogcomp.comma.datastructures.CommaSRLSentence) PredicateArgumentView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Aggregations

PredicateArgumentView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView)8 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)7 Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)5 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)5 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)2 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)2 ArrayList (java.util.ArrayList)2 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)1 Comma (edu.illinois.cs.cogcomp.comma.datastructures.Comma)1 CommaSRLSentence (edu.illinois.cs.cogcomp.comma.datastructures.CommaSRLSentence)1 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)1 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Predicate (se.lth.cs.srl.corpus.Predicate)1 Sentence (se.lth.cs.srl.corpus.Sentence)1 Word (se.lth.cs.srl.corpus.Word)1 Yield (se.lth.cs.srl.corpus.Yield)1