Search in sources :

Example 6 with Relation

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation 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 7 with Relation

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation 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 8 with Relation

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

the class ParsePath method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    Set<Feature> features = new LinkedHashSet<>();
    List<Relation> incomingRelations = c.getIncomingRelations();
    if (incomingRelations.size() > 0) {
        Constituent c1, c2;
        try {
            c1 = parse.getParsePhrase(incomingRelations.get(0).getSource());
            c2 = parse.getParsePhrase(c);
        } catch (Exception e) {
            throw new EdisonException(e);
        }
        Pair<List<Constituent>, List<Constituent>> paths = PathFeatureHelper.getPathsToCommonAncestor(c1, c2, 400);
        List<Constituent> list = new ArrayList<>();
        for (int i = 0; i < paths.getFirst().size() - 1; i++) {
            list.add(paths.getFirst().get(i));
        }
        Constituent top = paths.getFirst().get(paths.getFirst().size() - 1);
        list.add(top);
        for (int i = paths.getSecond().size() - 2; i >= 0; i--) {
            list.add(paths.getSecond().get(i));
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paths.getFirst().size() - 1; i++) {
            Constituent cc = paths.getFirst().get(i);
            sb.append(cc.getLabel());
            sb.append(PathFeatureHelper.PATH_UP_STRING);
        }
        String pathToAncestor = sb.toString();
        String pathString = PathFeatureHelper.getPathString(paths, true, false);
        features.add(DiscreteFeature.create(pathString));
        features.add(DiscreteFeature.create(pathToAncestor));
        features.add(RealFeature.create("l", list.size()));
    }
    return features;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) Feature(edu.illinois.cs.cogcomp.edison.features.Feature) DiscreteFeature(edu.illinois.cs.cogcomp.edison.features.DiscreteFeature) RealFeature(edu.illinois.cs.cogcomp.edison.features.RealFeature) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException) Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) ArrayList(java.util.ArrayList) List(java.util.List) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 9 with Relation

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

the class ParseSiblings method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    Constituent phrase;
    try {
        phrase = parse.getParsePhrase(c);
    } catch (Exception e) {
        throw new EdisonException(e);
    }
    Set<Feature> features = new LinkedHashSet<>();
    if (phrase.getIncomingRelations().size() == 0) {
        features.add(DiscreteFeature.create("ONLY_CHILD"));
    } else {
        Relation incomingEdge = phrase.getIncomingRelations().get(0);
        Constituent parent = incomingEdge.getSource();
        int position = -1;
        for (int i = 0; i < parent.getOutgoingRelations().size(); i++) {
            if (parent.getOutgoingRelations().get(i) == incomingEdge) {
                position = i;
                break;
            }
        }
        assert position >= 0;
        if (position == 0)
            features.add(DiscreteFeature.create("FIRST_CHILD"));
        else if (position == parent.getOutgoingRelations().size() - 1)
            features.add(DiscreteFeature.create("LAST_CHILD"));
        if (position != 0) {
            Constituent sibling = parent.getOutgoingRelations().get(position - 1).getTarget();
            String phraseType = sibling.getLabel();
            int headWord = CollinsHeadFinder.getInstance().getHeadWordPosition(sibling);
            String token = ta.getToken(headWord).toLowerCase().trim();
            String pos = WordHelpers.getPOS(ta, headWord);
            features.add(DiscreteFeature.create("lsis.pt:" + phraseType));
            features.add(DiscreteFeature.create("lsis.hw:" + token));
            features.add(DiscreteFeature.create("lsis.hw.pos:" + pos));
        }
        if (position != parent.getOutgoingRelations().size() - 1) {
            Constituent sibling = parent.getOutgoingRelations().get(position + 1).getTarget();
            String phraseType = sibling.getLabel();
            int headWord = CollinsHeadFinder.getInstance().getHeadWordPosition(sibling);
            String token = ta.getToken(headWord).toLowerCase().trim();
            String pos = WordHelpers.getPOS(ta, headWord);
            features.add(DiscreteFeature.create("rsis.pt:" + phraseType));
            features.add(DiscreteFeature.create("rsis.hw:" + token));
            features.add(DiscreteFeature.create("rsis.hw.pos:" + pos));
        }
    }
    return features;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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)

Example 10 with Relation

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

the class SyntacticFrame method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    Set<Feature> features = new LinkedHashSet<>();
    List<Relation> incomingRelations = c.getIncomingRelations();
    if (incomingRelations.size() > 0) {
        Constituent pred = incomingRelations.get(0).getSource();
        TextAnnotation ta = c.getTextAnnotation();
        TreeView parse = (TreeView) ta.getView(parseViewName);
        Constituent predicate, arg;
        try {
            predicate = parse.getParsePhrase(pred);
            arg = parse.getParsePhrase(c);
        } catch (Exception e) {
            throw new EdisonException(e);
        }
        Constituent vp = TreeView.getParent(predicate);
        // go over VP's siblings before it
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        if (!TreeView.isRoot(vp)) {
            Constituent vpParent = TreeView.getParent(vp);
            for (int i = 0; i < vpParent.getOutgoingRelations().size(); i++) {
                Constituent target = vpParent.getOutgoingRelations().get(i).getTarget();
                if (target == vp)
                    break;
                addToFeature(target, arg, sb1, sb2, sb3);
            }
        }
        for (int i = 0; i < vp.getOutgoingRelations().size(); i++) {
            Constituent target = vp.getOutgoingRelations().get(i).getTarget();
            if (target.getSpan().equals(predicate.getSpan())) {
                sb1.append("v-");
                sb2.append("v-");
                sb3.append(WordHelpers.getLemma(ta, target.getStartSpan())).append("-");
            } else {
                addToFeature(target, arg, sb1, sb2, sb3);
            }
        }
        features.add(DiscreteFeature.create(sb1.toString()));
        features.add(DiscreteFeature.create("general:" + sb2.toString()));
        features.add(DiscreteFeature.create("lemma:" + sb3.toString()));
    }
    return features;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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)

Aggregations

Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)26 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)25 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)16 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)10 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)9 EdisonException (edu.illinois.cs.cogcomp.edison.utilities.EdisonException)8 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)7 ArrayList (java.util.ArrayList)7 LinkedHashSet (java.util.LinkedHashSet)7 PredicateArgumentView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView)5 JsonObject (com.google.gson.JsonObject)3 View (edu.illinois.cs.cogcomp.core.datastructures.textannotation.View)3 Test (org.junit.Test)3 JsonParser (com.google.gson.JsonParser)2 SpanLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)2 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)2 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)2 Annotation (edu.stanford.nlp.pipeline.Annotation)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 HashSet (java.util.HashSet)2