use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.
the class DepAnnotator method addView.
@Override
public void addView(TextAnnotation ta) throws AnnotatorException {
for (String reqView : requiredViews) if (!ta.hasView(reqView))
throw new AnnotatorException("TextAnnotation must have view: " + reqView);
DepInst sent = new DepInst(ta);
DepStruct deptree;
try {
deptree = (DepStruct) model.infSolver.getBestStructure(model.wv, sent);
} catch (Exception e) {
throw new AnnotatorException("Sentence cannot be parsed");
}
TreeView treeView = new TreeView(ViewNames.DEPENDENCY, ta);
int rootPos = findRoot(deptree);
// All the node positions are -1 to account for the extra <root> node added
Pair<String, Integer> nodePair = new Pair<>(sent.forms[rootPos], rootPos - 1);
Tree<Pair<String, Integer>> tree = new Tree<>(nodePair);
populateChildren(tree, deptree, sent, rootPos);
treeView.setDependencyTree(0, tree);
ta.addView(ViewNames.DEPENDENCY, 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;
}
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;
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.
the class ParseHeadWordFeatureExtractor method getFeatures.
@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
TextAnnotation ta = c.getTextAnnotation();
TreeView tree = (TreeView) ta.getView(parseViewName);
Constituent phrase;
try {
phrase = tree.getParsePhrase(c);
} catch (Exception e) {
throw new EdisonException(e);
}
Set<Feature> features = new LinkedHashSet<>();
int head = CollinsHeadFinder.getInstance().getHeadWordPosition(phrase);
Constituent c1 = new Constituent("", "", ta, head, head + 1);
features.addAll(fex.getFeatures(c1));
return features;
}
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;
}
Aggregations