Search in sources :

Example 1 with TreeView

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

the class GetParseLeftSibling method transform.

@Override
public List<Constituent> transform(Constituent input) {
    TextAnnotation ta = input.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    List<Constituent> siblings = new ArrayList<>();
    try {
        Constituent phrase = parse.getParsePhrase(input);
        List<Relation> in = phrase.getIncomingRelations();
        if (in.size() > 0) {
            Constituent prev = null;
            Relation relation = in.get(0);
            List<Relation> outgoingRelations = relation.getSource().getOutgoingRelations();
            for (Relation r : outgoingRelations) {
                if (r.getTarget() == phrase) {
                    break;
                }
                prev = r.getTarget();
            }
            if (prev != null)
                siblings.add(prev);
        }
    } catch (EdisonException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return siblings;
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) ArrayList(java.util.ArrayList) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 2 with TreeView

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

the class PPFeatures method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    Set<Feature> feats = new HashSet<>();
    try {
        Constituent phrase = parse.getParsePhrase(c);
        // if the phrase is a PP, then the head word of its
        // rightmost NP child.
        List<Relation> rels = phrase.getOutgoingRelations();
        for (int i = rels.size() - 1; i >= 0; i--) {
            Relation relation = rels.get(i);
            if (relation == null)
                continue;
            Constituent target = relation.getTarget();
            if (ParseTreeProperties.isNominal(target.getLabel())) {
                int head = CollinsHeadFinder.getInstance().getHeadWordPosition(phrase);
                feats.add(DiscreteFeature.create("np-head:" + ta.getToken(head).toLowerCase()));
                feats.add(DiscreteFeature.create("np-head-pos:" + WordHelpers.getPOS(ta, head)));
                break;
            }
        }
        // if the phrase's parent is a PP, then the head of that PP.
        Constituent parent = phrase.getIncomingRelations().get(0).getSource();
        if (parent.getLabel().equals("PP")) {
            int head = CollinsHeadFinder.getInstance().getHeadWordPosition(phrase);
            feats.add(DiscreteFeature.create("p-head:" + ta.getToken(head).toLowerCase()));
        }
    } catch (EdisonException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return feats;
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) Feature(edu.illinois.cs.cogcomp.edison.features.Feature) DiscreteFeature(edu.illinois.cs.cogcomp.edison.features.DiscreteFeature) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) HashSet(java.util.HashSet)

Example 3 with TreeView

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

the class ProjectedPath method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    Set<Feature> feats = new HashSet<>();
    // Clone this to avoid concurrency problems
    Constituent c2 = null;
    try {
        c2 = parse.getParsePhrase(c).cloneForNewView("");
    } catch (Exception e) {
        e.printStackTrace();
    }
    assert c2 != null;
    if (!c2.getLabel().equals("VP"))
        return feats;
    boolean found = false;
    boolean done = false;
    while (!done) {
        List<Relation> rels = c2.getIncomingRelations();
        if (rels.size() == 0)
            done = true;
        else {
            Constituent parent = rels.get(0).getSource();
            if (parent.getLabel().equals("VP")) {
                found = true;
                c2 = parent;
            } else {
                done = true;
            }
        }
    }
    if (found) {
        // Clone this to avoid concurrency problems
        Constituent c1 = null;
        try {
            c1 = parse.getParsePhrase(c.getIncomingRelations().get(0).getSource()).cloneForNewView("");
        } catch (Exception e) {
            e.printStackTrace();
        }
        assert c1 != null;
        String path = PathFeatureHelper.getFullParsePathString(c1, c2, 400);
        feats.add(DiscreteFeature.create(path));
    }
    return feats;
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) DiscreteFeature(edu.illinois.cs.cogcomp.edison.features.DiscreteFeature) Feature(edu.illinois.cs.cogcomp.edison.features.Feature) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) HashSet(java.util.HashSet)

Example 4 with TreeView

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

the class GetParseRightSibling method transform.

@Override
public List<Constituent> transform(Constituent input) {
    TextAnnotation ta = input.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    List<Constituent> siblings = new ArrayList<>();
    try {
        Constituent phrase = parse.getParsePhrase(input);
        List<Relation> in = phrase.getIncomingRelations();
        if (in.size() > 0) {
            List<Relation> outgoingRelations = in.get(0).getSource().getOutgoingRelations();
            int id = -1;
            for (int i = 0; i < outgoingRelations.size(); i++) {
                Relation r = outgoingRelations.get(i);
                if (r.getTarget() == phrase) {
                    id = i;
                    break;
                }
            }
            if (id >= 0 && id + 1 < outgoingRelations.size())
                siblings.add(outgoingRelations.get(id + 1).getTarget());
        }
    } catch (EdisonException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return siblings;
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) ArrayList(java.util.ArrayList) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 5 with TreeView

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

the class DependencyPath method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(dependencyViewName);
    Constituent c1 = parse.getConstituentsCoveringToken(c.getIncomingRelations().get(0).getSource().getStartSpan()).get(0);
    Constituent c2 = parse.getConstituentsCoveringToken(c.getStartSpan()).get(0);
    Pair<List<Constituent>, List<Constituent>> paths = PathFeatureHelper.getPathsToCommonAncestor(c1, c2, 400);
    int length = paths.getFirst().size() + paths.getSecond().size() - 1;
    StringBuilder path = new StringBuilder();
    StringBuilder pos = new StringBuilder();
    for (int i = 0; i < paths.getFirst().size() - 1; i++) {
        Constituent cc = paths.getFirst().get(i);
        path.append(cc.getIncomingRelations().get(0).getRelationName()).append(PathFeatureHelper.PATH_UP_STRING);
        pos.append(WordHelpers.getPOS(ta, cc.getStartSpan()));
        pos.append(cc.getIncomingRelations().get(0).getRelationName()).append(PathFeatureHelper.PATH_UP_STRING);
    }
    Constituent top = paths.getFirst().get(paths.getFirst().size() - 1);
    pos.append(WordHelpers.getPOS(ta, top.getStartSpan()));
    pos.append("*");
    path.append("*");
    if (paths.getSecond().size() > 1) {
        for (int i = paths.getSecond().size() - 2; i >= 0; i--) {
            Constituent cc = paths.getSecond().get(i);
            pos.append(WordHelpers.getPOS(ta, cc.getStartSpan()));
            pos.append(PathFeatureHelper.PATH_DOWN_STRING);
            path.append(PathFeatureHelper.PATH_DOWN_STRING);
        }
    }
    Set<Feature> features = new LinkedHashSet<>();
    features.add(DiscreteFeature.create(path.toString()));
    features.add(DiscreteFeature.create("pos" + pos.toString()));
    features.add(RealFeature.create("l", length));
    return features;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) List(java.util.List) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Feature(edu.illinois.cs.cogcomp.edison.features.Feature) DiscreteFeature(edu.illinois.cs.cogcomp.edison.features.DiscreteFeature) RealFeature(edu.illinois.cs.cogcomp.edison.features.RealFeature) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Aggregations

TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)32 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)27 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)20 EdisonException (edu.illinois.cs.cogcomp.edison.utilities.EdisonException)13 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)12 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)11 LinkedHashSet (java.util.LinkedHashSet)11 Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)9 Test (org.junit.Test)7 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)6 ArrayList (java.util.ArrayList)6 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)5 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)3 List (java.util.List)3 RealFeature (edu.illinois.cs.cogcomp.edison.features.RealFeature)2 POSFromParse (edu.illinois.cs.cogcomp.nlp.utilities.POSFromParse)2 Annotation (edu.stanford.nlp.pipeline.Annotation)2 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 HashSet (java.util.HashSet)2