Search in sources :

Example 16 with TokenLabelView

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

the class CommaTest method setUp.

@Override
public void setUp() throws Exception {
    String[] tokens = "Says Gayle Key , a mathematics teacher , `` Hello world . ''".split("\\s+");
    TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(Collections.singletonList(tokens));
    TokenLabelView tlv = new TokenLabelView(ViewNames.POS, "Test", ta, 1.0);
    tlv.addTokenLabel(0, "VBZ", 1d);
    tlv.addTokenLabel(1, "NNP", 1d);
    tlv.addTokenLabel(2, "NNP", 1d);
    tlv.addTokenLabel(3, ",", 1d);
    tlv.addTokenLabel(4, "DT", 1d);
    tlv.addTokenLabel(5, "NN", 1d);
    tlv.addTokenLabel(6, "NN", 1d);
    tlv.addTokenLabel(7, ",", 1d);
    tlv.addTokenLabel(8, "``", 1d);
    tlv.addTokenLabel(9, "UH", 1d);
    tlv.addTokenLabel(10, "NN", 1d);
    tlv.addTokenLabel(11, ".", 1d);
    tlv.addTokenLabel(12, "''", 1d);
    TreeView parse = new TreeView(ViewNames.PARSE_STANFORD, "Test", ta, 1.0);
    String treeString = "(ROOT" + "  (SINV" + "    (VP (VBZ Says))" + "    (NP (NNP Gayle) (NNP Key))" + "    (, ,)" + "    (S" + "      (NP (DT a) (NNS mathematics))" + "      (VP (VBZ teacher) (, ,) (`` ``)" + "        (NP" + "          (INTJ (UH Hello))" + "          (NP (NN world)))))" + "    (. .) ('' '')))";
    parse.setParseTree(0, TreeParserFactory.getStringTreeParser().parse(treeString));
    SpanLabelView ner = new SpanLabelView(ViewNames.NER_CONLL, "Test", ta, 1.0);
    ner.addSpanLabel(1, 3, "PER", 1.0);
    SpanLabelView shallowParse = new SpanLabelView(ViewNames.SHALLOW_PARSE, "Test", ta, 1.0);
    shallowParse.addSpanLabel(0, 3, "NP", 1.0);
    shallowParse.addSpanLabel(4, 7, "NP", 1.0);
    shallowParse.addSpanLabel(9, 11, "NP", 1.0);
    // TODO dependency parse
    // TODO SRL view
    ta.addView(tlv.getViewName(), tlv);
    ta.addView(parse.getViewName(), parse);
    ta.addView(ner.getViewName(), ner);
    ta.addView(shallowParse.getViewName(), shallowParse);
    List<String> firstCommasRefinedLabels = Collections.singletonList("Substitute");
    List<String> secondCommasRefinedLabels = Arrays.asList("Substitute", "Quotation");
    CommaSRLSentence sentence = new CommaSRLSentence(ta, null, Arrays.asList(firstCommasRefinedLabels, secondCommasRefinedLabels));
    List<Comma> sentenceCommas = sentence.getCommas();
    commas = sentenceCommas.toArray(new Comma[sentenceCommas.size()]);
}
Also used : Comma(edu.illinois.cs.cogcomp.comma.datastructures.Comma) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) CommaSRLSentence(edu.illinois.cs.cogcomp.comma.datastructures.CommaSRLSentence) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) SpanLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)

Example 17 with TokenLabelView

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

the class VerbSensePropbankReader method addAnnotation.

private void addAnnotation(TextAnnotation ta) {
    String goldViewName = SenseManager.getGoldViewName();
    Tree<String> tree = ParseHelper.getParseTree(ViewNames.PARSE_GOLD, ta, 0);
    Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree);
    List<Tree<Pair<String, IntPair>>> yield = spanLabeledTree.getYield();
    TokenLabelView view = new TokenLabelView(goldViewName, "AnnotatedTreebank", ta, 1.0);
    Set<Integer> predicates = new HashSet<>();
    for (PropbankFields fields : goldFields.get(ta.getId())) {
        int start = fields.getPredicateStart(yield);
        if (predicates.contains(start))
            continue;
        predicates.add(start);
        view.addTokenLabel(start, fields.getSense(), 1.0);
        try {
            view.addTokenAttribute(start, LemmaIdentifier, fields.getLemma());
        } catch (Exception e) {
            // XXX Maybe log the exception?
            e.printStackTrace();
        }
    }
    if (view.getConstituents().size() > 0)
        ta.addView(goldViewName, view);
}
Also used : IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) FileNotFoundException(java.io.FileNotFoundException) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 18 with TokenLabelView

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

the class VerbSenseClassifierMain method evaluate.

@CommandDescription(description = "Performs evaluation.", usage = "evaluate")
public static void evaluate() throws Exception {
    SenseManager manager = getManager(false);
    Dataset testSet = Dataset.PTBTest;
    ILPSolverFactory solver = new ILPSolverFactory(ILPSolverFactory.SolverType.JLISCuttingPlaneGurobi);
    ClassificationTester senseTester = new ClassificationTester();
    long start = System.currentTimeMillis();
    int count = 0;
    manager.getModelInfo().loadWeightVector();
    IResetableIterator<TextAnnotation> dataset = SentenceDBHandler.instance.getDataset(testSet);
    while (dataset.hasNext()) {
        TextAnnotation ta = dataset.next();
        if (!ta.hasView(SenseManager.getGoldViewName()))
            continue;
        TokenLabelView gold = (TokenLabelView) ta.getView(SenseManager.getGoldViewName());
        ILPInference inference = manager.getInference(solver, gold.getConstituents());
        assert inference != null;
        TokenLabelView prediction = inference.getOutputView();
        evaluateSense(gold, prediction, senseTester);
        count++;
        if (count % 1000 == 0) {
            long end = System.currentTimeMillis();
            log.info(count + " sentences done. Took " + (end - start) + "ms, Micro-F1 so far = " + senseTester.getMicroF1());
        }
    }
    long end = System.currentTimeMillis();
    System.out.println(count + " sentences done. Took " + (end - start) + "ms");
    System.out.println("\n\n* Sense");
    System.out.println(senseTester.getPerformanceTable(false).toOrgTable());
}
Also used : ILPSolverFactory(edu.illinois.cs.cogcomp.infer.ilp.ILPSolverFactory) Dataset(edu.illinois.cs.cogcomp.verbsense.data.Dataset) ClassificationTester(edu.illinois.cs.cogcomp.core.experiments.ClassificationTester) SenseManager(edu.illinois.cs.cogcomp.verbsense.core.SenseManager) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) ILPInference(edu.illinois.cs.cogcomp.verbsense.inference.ILPInference) CommandDescription(edu.illinois.cs.cogcomp.core.utilities.commands.CommandDescription)

Example 19 with TokenLabelView

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

the class VerbSenseLabeler method getPrediction.

public TokenLabelView getPrediction(TextAnnotation ta) throws Exception {
    log.debug("Input: {}", ta.getText());
    List<Constituent> predicates = manager.getPredicateDetector().getPredicates(ta);
    // If there are no verb identified, return an empty TokenLabelView
    if (predicates.isEmpty())
        return new TokenLabelView(SenseManager.getPredictedViewName(), VerbSenseConstants.systemIdentifier, ta, 1.0);
    ILPSolverFactory solver = new ILPSolverFactory(ILPSolverFactory.SolverType.JLISCuttingPlaneGurobi);
    ILPInference inference = manager.getInference(solver, predicates);
    return inference.getOutputView();
}
Also used : ILPSolverFactory(edu.illinois.cs.cogcomp.infer.ilp.ILPSolverFactory) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) ILPInference(edu.illinois.cs.cogcomp.verbsense.inference.ILPInference) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 20 with TokenLabelView

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

the class VerbSenseLabeler method initializeDummySentenceVerb.

protected TextAnnotation initializeDummySentenceVerb() {
    List<String[]> listOfTokens = new ArrayList<>();
    listOfTokens.add(new String[] { "I", "do", "." });
    TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens("", "", listOfTokens);
    TokenLabelView tlv = new TokenLabelView(ViewNames.POS, "Test", ta, 1.0);
    tlv.addTokenLabel(0, "PRP", 1d);
    tlv.addTokenLabel(1, "VBP", 1d);
    tlv.addTokenLabel(2, ".", 1d);
    ta.addView(ViewNames.POS, tlv);
    ta.addView(ViewNames.NER, new SpanLabelView(ViewNames.NER, "test", ta, 1d));
    SpanLabelView chunks = new SpanLabelView(ViewNames.SHALLOW_PARSE, "test", ta, 1d);
    chunks.addSpanLabel(0, 1, "NP", 1d);
    chunks.addSpanLabel(1, 2, "VP", 1d);
    ta.addView(ViewNames.SHALLOW_PARSE, chunks);
    TokenLabelView view = new TokenLabelView(ViewNames.LEMMA, "test", ta, 1d);
    view.addTokenLabel(0, "i", 1d);
    view.addTokenLabel(1, "do", 1d);
    view.addTokenLabel(2, ".", 1d);
    ta.addView(ViewNames.LEMMA, view);
    return ta;
}
Also used : ArrayList(java.util.ArrayList) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) SpanLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)

Aggregations

TokenLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView)22 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)13 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)5 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)5 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)5 LinkedHashSet (java.util.LinkedHashSet)5 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)2 SpanLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)2 ILPSolverFactory (edu.illinois.cs.cogcomp.infer.ilp.ILPSolverFactory)2 ILPInference (edu.illinois.cs.cogcomp.verbsense.inference.ILPInference)2 ArrayList (java.util.ArrayList)2 JWNLException (net.didion.jwnl.JWNLException)2 POS (net.didion.jwnl.data.POS)2 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)1 Comma (edu.illinois.cs.cogcomp.comma.datastructures.Comma)1 CommaSRLSentence (edu.illinois.cs.cogcomp.comma.datastructures.CommaSRLSentence)1 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)1 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)1 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)1 ClassificationTester (edu.illinois.cs.cogcomp.core.experiments.ClassificationTester)1