Search in sources :

Example 16 with TreeView

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

the class ParseLabelIdentifier method getFeatures.

@Override
public Set<Feature> getFeatures(Constituent c) throws EdisonException {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    String l;
    try {
        l = parse.getParsePhrase(c).getLabel();
    } catch (Exception e) {
        throw new EdisonException(e);
    }
    boolean found = isLabelValid(l);
    Set<Feature> features = new LinkedHashSet<>();
    if (found) {
        features.add(DiscreteFeature.create(label));
    }
    return features;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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) DiscreteFeature(edu.illinois.cs.cogcomp.edison.features.DiscreteFeature) Feature(edu.illinois.cs.cogcomp.edison.features.Feature) EdisonException(edu.illinois.cs.cogcomp.edison.utilities.EdisonException)

Example 17 with TreeView

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

Example 18 with TreeView

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

the class ClauseViewGeneratorTest method testClauseViewGenerator.

//    public void setUp() throws Exception {
//        super.setUp();
//    }
@Test
public final void testClauseViewGenerator() {
    String text = "Freeport-McMoRan Inc. said it will convert its Freeport-McMoRan Energy Partners Ltd. " + "partnership into a publicly traded company through the exchange of units of the partnership " + "for common shares .";
    TextAnnotation ta = TextAnnotationUtilities.createFromTokenizedString(text);
    Tree<String> tree = TreeParserFactory.getStringTreeParser().parse("(S1 (S (NP-SBJ (NNP Freeport-McMoRan)               (NNP Inc.))       (VP (VBD said)" + "           (SBAR (-NONE- 0)                 (S (NP-SBJ (PRP it))                    " + "(VP (MD will)                        (VP (VB convert)                            " + "(NP (PRP$ its)                                (NNP Freeport-McMoRan) " + "                               (NNP Energy)                                (NNPS Partners)" + "                                (NNP Ltd.)                                (NN partnership)) " + "                           (PP-CLR (IN into)                                    (NP (DT a)" + "                                        (ADJP (RB publicly)" + "                                              (VBN traded))" + "                                        (NN company))) " + "                           (PP-MNR (IN through) " + "                                   (NP (NP (DT the)    " + "                                        (NN exchange))    " + "                                    (PP (IN of)            " + "                                (NP (NP (NNS units))           " + "                                     (PP (IN of)                   " + "                                 (NP (DT the)                           " + "                             (NN partnership)))))                            " + "            (PP (IN for)                                            (NP (JJ common) " + "                                               (NNS shares))))))))))       (. .)))");
    TreeView parse = new TreeView("", ta);
    parse.setParseTree(0, tree);
    ta.addView(ViewNames.PARSE_GOLD, parse);
    ClauseViewGenerator clg = new ClauseViewGenerator(ViewNames.PARSE_GOLD, "clauses");
    try {
        ta.addView(clg);
    } catch (AnnotatorException e) {
        fail(e.getMessage());
    }
    logger.info(ta.getView("clauses").toString());
}
Also used : AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Test(org.junit.Test)

Example 19 with TreeView

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

the class TestHeadFinderDependencyHelper method testHeadFinderDependencyHelper.

@Test
public final void testHeadFinderDependencyHelper() {
    String s = "There is no recovery period -- it 's go , go , go .";
    String treeString = "(S1 (S (S (NP (EX There))    (VP (AUX is)        (NP (DT no)            (NN recovery) " + "           (NN period))))    (: --)    (S (NP (PRP it))       (VP (AUX 's)           (S (VP (VB go)  " + "   (, ,)     (VB go)     (, ,)     (VB go)))))    (. .)))";
    TextAnnotation ta = TextAnnotationUtilities.createFromTokenizedString(s);
    TreeView parse = new TreeView(ViewNames.PARSE_CHARNIAK, "", ta, 1.0);
    parse.setParseTree(0, TreeParserFactory.getStringTreeParser().parse(treeString));
    ta.addView(ViewNames.PARSE_CHARNIAK, parse);
    logger.info(ta.getView(ViewNames.PARSE_CHARNIAK).toString());
    HeadFinderDependencyViewGenerator dep = new HeadFinderDependencyViewGenerator(ViewNames.PARSE_CHARNIAK);
    TreeView depTree = null;
    try {
        depTree = (TreeView) dep.getView(ta);
    } catch (AnnotatorException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    logger.info(depTree.toString());
    assertEquals(depTree.getNumberOfConstituents(), ta.size());
}
Also used : AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Test(org.junit.Test)

Example 20 with TreeView

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

the class StanfordDepHandler method addView.

@Override
public void addView(TextAnnotation textAnnotation) throws AnnotatorException {
    // If the sentence is longer than STFRD_MAX_SENTENCE_LENGTH there is no point in trying to
    // parse
    StanfordParseHandler.checkLength(textAnnotation, throwExceptionOnSentenceLengthCheck, maxParseSentenceLength);
    TreeView treeView = new TreeView(ViewNames.DEPENDENCY_STANFORD, "StanfordDepHandler", textAnnotation, 1d);
    // The (tokenized) sentence offset in case we have more than one sentences in the record
    List<CoreMap> sentences = StanfordParseHandler.buildStanfordSentences(textAnnotation);
    Annotation document = new Annotation(sentences);
    posAnnotator.annotate(document);
    parseAnnotator.annotate(document);
    sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    if (sentences.get(0).get(TreeCoreAnnotations.TreeAnnotation.class).nodeString().equals("X")) {
        // This is most like because we ran out of time
        throw new AnnotatorException("Unable to parse TextAnnotation " + textAnnotation.getId() + ". " + "This is most likely due to a timeout.");
    }
    for (int sentenceId = 0; sentenceId < sentences.size(); sentenceId++) {
        boolean runtimeExceptionWasThrown = false;
        CoreMap sentence = sentences.get(sentenceId);
        if (maxParseSentenceLength > 0 && sentence.size() > maxParseSentenceLength) {
            logger.warn(HandlerUtils.getSentenceLengthError(textAnnotation.getId(), sentence.toString(), maxParseSentenceLength));
        } else {
            SemanticGraph depGraph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
            IndexedWord root = null;
            try {
                root = depGraph.getFirstRoot();
            } catch (RuntimeException e) {
                String msg = "ERROR in getting root of dep graph for sentence.  Sentence is:\n" + sentence.toString() + "'\nDependency graph is:\n" + depGraph.toCompactString() + "\nText is:\n" + textAnnotation.getText();
                logger.error(msg);
                System.err.println(msg);
                e.printStackTrace();
                if (throwExceptionOnSentenceLengthCheck)
                    throw e;
                else
                    runtimeExceptionWasThrown = true;
            }
            if (!runtimeExceptionWasThrown) {
                int tokenStart = getNodePosition(textAnnotation, root, sentenceId);
                Pair<String, Integer> nodePair = new Pair<>(root.originalText(), tokenStart);
                Tree<Pair<String, Integer>> tree = new Tree<>(nodePair);
                populateChildren(depGraph, root, tree, textAnnotation, sentenceId);
                treeView.setDependencyTree(sentenceId, tree);
            }
        }
    }
    textAnnotation.addView(getViewName(), treeView);
}
Also used : AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Annotation(edu.stanford.nlp.pipeline.Annotation) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) IndexedWord(edu.stanford.nlp.ling.IndexedWord) CoreMap(edu.stanford.nlp.util.CoreMap) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Aggregations

TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)30 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)25 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)19 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)4 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)3 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)3 List (java.util.List)3 RealFeature (edu.illinois.cs.cogcomp.edison.features.RealFeature)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 Comma (edu.illinois.cs.cogcomp.comma.datastructures.Comma)1