Search in sources :

Example 21 with Relation

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

the class TestChunkFeatures method testFex.

private void testFex(FeatureExtractor fex, boolean printBoth, String... viewNames) throws EdisonException {
    for (TextAnnotation ta : tas) {
        for (String viewName : viewNames) if (ta.hasView(viewName))
            logger.info(ta.getView(viewName).toString());
        if (!ta.hasView(ViewNames.SRL_VERB))
            continue;
        PredicateArgumentView pav = (PredicateArgumentView) ta.getView(ViewNames.SRL_VERB);
        for (Constituent predicate : pav.getPredicates()) {
            Constituent p = predicate.cloneForNewView("dummy");
            for (Relation argument : pav.getArguments(predicate)) {
                Constituent c = argument.getTarget().cloneForNewView("dummy");
                Relation r = new Relation("", p, c, 1);
                logger.info((printBoth ? r : c) + "\t" + fex.getFeatures(c));
            }
        }
    }
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) PredicateArgumentView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 22 with Relation

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

the class CollinsHeadDependencyParser method getDependencyTree.

public Tree<String> getDependencyTree(Constituent parseTreeRoot) {
    if (TreeView.isLeaf(parseTreeRoot))
        return new Tree<>(parseTreeRoot.getTokenizedSurfaceForm());
    Constituent headWord = headFinder.getHeadWord(parseTreeRoot);
    Constituent headChild = headFinder.getHeadChild(parseTreeRoot);
    Tree<String> rootTree = new Tree<>(headWord.getTokenizedSurfaceForm());
    List<Tree<String>> dependentTrees = new ArrayList<>();
    for (Relation childEdge : parseTreeRoot.getOutgoingRelations()) {
        Constituent child = childEdge.getTarget();
        if (child.equals(headChild)) {
            rootTree = getDependencyTree(child);
        } else {
            dependentTrees.add(getDependencyTree(child));
        }
    }
    rootTree.addSubtrees(dependentTrees);
    return rootTree;
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) ArrayList(java.util.ArrayList) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 23 with Relation

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

the class CollinsHeadDependencyParser method makeDepTree.

private Tree<Pair<String, Integer>> makeDepTree(Constituent parseTreeRoot) {
    if (TreeView.isLeaf(parseTreeRoot)) {
        int position = parseTreeRoot.getStartSpan();
        return new Tree<>(new Pair<>(parseTreeRoot.getLabel(), position));
    }
    Constituent headChild = headFinder.getHeadChild(parseTreeRoot);
    Tree<Pair<String, Integer>> rootTree = null;
    List<Tree<Pair<String, Integer>>> dependentTrees = new ArrayList<>();
    List<Pair<String, Integer>> edgeLabels = new ArrayList<>();
    int conjunction = -1;
    for (Relation childEdge : parseTreeRoot.getOutgoingRelations()) {
        Constituent child = childEdge.getTarget();
        if (child == headChild) {
            rootTree = makeDepTree(child);
        } else {
            dependentTrees.add(makeDepTree(child));
            edgeLabels.add(getEdgeLabel(parseTreeRoot, headChild.getLabel(), child));
            if (child.getLabel().equals("CC")) {
                conjunction = dependentTrees.size() - 1;
            }
        }
    }
    if (conjunction >= 0) {
        return doConjunctionHack(parseTreeRoot, headChild, rootTree, dependentTrees, edgeLabels, conjunction);
    } else {
        for (int i = 0; i < dependentTrees.size(); i++) {
            rootTree.addSubtree(dependentTrees.get(i), edgeLabels.get(i));
        }
        return rootTree;
    }
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) ArrayList(java.util.ArrayList) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 24 with Relation

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

the class ProtobufSerializerTest method testSerializerWithCharOffsets.

@Test
public void testSerializerWithCharOffsets() throws Exception {
    View rhymeView = new View("rhyme", "test", ta, 0.4);
    Map<String, Double> newLabelsToScores = new TreeMap<String, Double>();
    String[] labels = { "eeny", "meeny", "miny", "mo" };
    double[] scores = { 0.15, 0.15, 0.3, 0.4 };
    for (int i = 0; i < labels.length; ++i) newLabelsToScores.put(labels[i], scores[i]);
    Constituent first = new Constituent(newLabelsToScores, "rhyme", ta, 2, 4);
    rhymeView.addConstituent(first);
    /**
         * no constraint on scores -- don't have to sum to 1.0
         */
    for (int i = labels.length - 1; i > 0; --i) newLabelsToScores.put(labels[i], scores[3 - i]);
    Constituent second = new Constituent(newLabelsToScores, "rhyme", ta, 2, 4);
    rhymeView.addConstituent(second);
    Map<String, Double> relLabelsToScores = new TreeMap<>();
    relLabelsToScores.put("Yes", 0.8);
    relLabelsToScores.put("No", 0.2);
    Relation rel = new Relation(relLabelsToScores, first, second);
    rhymeView.addRelation(rel);
    ta.addView("rhyme", rhymeView);
    // Serialize to protocol buffers format
    TextAnnotationImpl.TextAnnotationProto textAnnotationProto = ProtobufSerializer.writeTextAnnotation(ta);
    byte[] protoSerializedData = textAnnotationProto.toByteArray();
    TextAnnotationImpl.TextAnnotationProto protoRead = TextAnnotationImpl.TextAnnotationProto.parseFrom(protoSerializedData);
    TextAnnotation parsedTA = ProtobufSerializer.readTextAnnotation(protoRead);
    // Convert to JSON and verify content.
    String taJson = SerializationHelper.serializeToJson(parsedTA, true);
    JsonObject jobj = (JsonObject) new JsonParser().parse(taJson);
    JsonSerializerTest.verifySerializedJSONObject(jobj, ta);
}
Also used : JsonObject(com.google.gson.JsonObject) View(edu.illinois.cs.cogcomp.core.datastructures.textannotation.View) TextAnnotationImpl(edu.illinois.cs.cogcomp.core.utilities.protobuf.TextAnnotationImpl) Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 25 with Relation

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

the class StanfordRelationsHandler method addView.

@Override
protected void addView(TextAnnotation ta) throws AnnotatorException {
    Annotation document = new Annotation(ta.text);
    pipeline.annotate(document);
    SpanLabelView vu = new SpanLabelView(viewName, ta);
    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
        for (RelationMention rm : sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class)) {
            if (rm.getType().equals("_NR"))
                continue;
            Map<String, Double> scores = new HashMap<>();
            for (String label : rm.getTypeProbabilities().keySet()) scores.put(label, rm.getTypeProbabilities().getCount(label));
            Constituent c1 = createConstituentGivenMention(rm.getEntityMentionArgs().get(0), ta);
            Constituent c2 = createConstituentGivenMention(rm.getEntityMentionArgs().get(1), ta);
            Relation r = new Relation(scores, c1, c2);
            vu.addRelation(r);
            if (!vu.containsConstituent(c1))
                vu.addConstituent(c1);
            if (!vu.containsConstituent(c2))
                vu.addConstituent(c2);
        }
    }
    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
        for (EntityMention rm : sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class)) {
            Constituent c = createConstituentGivenMention(rm, ta);
            if (!vu.containsConstituent(c))
                vu.addConstituent(c);
        }
    }
    ta.addView(viewName, vu);
}
Also used : RelationMention(edu.stanford.nlp.ie.machinereading.structure.RelationMention) SpanLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Annotation(edu.stanford.nlp.pipeline.Annotation) MachineReadingAnnotations(edu.stanford.nlp.ie.machinereading.structure.MachineReadingAnnotations) Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) EntityMention(edu.stanford.nlp.ie.machinereading.structure.EntityMention) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

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