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));
}
}
}
}
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;
}
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;
}
}
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);
}
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);
}
Aggregations