Search in sources :

Example 1 with Quantifier

use of edu.illinois.cs.cogcomp.quant.driver.Quantifier in project cogcomp-nlp by CogComp.

the class QuantTest method testQuantifierView.

@Test
public void testQuantifierView() throws Exception {
    Quantifier quantifier = new Quantifier();
    quantifier.doInitialize();
    // sentence 1
    TextAnnotation ta = Quantifier.taBuilder.createTextAnnotation("It was May 2008.");
    quantifier.addView(ta);
    assertTrue(ta.hasView(ViewNames.QUANTITIES));
    assertTrue(isEqual("[= Date(05/XX/2008)]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(0).getLabel()));
    // sentence 2
    ta = Quantifier.taBuilder.createTextAnnotation("This month is the 20th of the year. Everything is above 20$.");
    quantifier.addView(ta);
    assertTrue(ta.hasView(ViewNames.QUANTITIES));
    assertTrue(isEqual("[= 1.0 year]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(0).getLabel()));
    assertTrue(isEqual("[= 20.0 US$]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(1).getLabel()));
    // a long paragraph
    String paragraph = "Super Bowl 50 was an American football game to determine the champion of the National " + "Football League (NFL) for the 2015 season. The American Football Conference (AFC) champion Denver " + "Broncos defeated the National Football Conference (NFC) champion Carolina Panthers 24–10 to earn " + "their third Super Bowl title. The game was played on February 7, 2016, at Levi's Stadium in " + "the San Francisco Bay Area at Santa Clara, California. As this was the 50th Super Bowl, the league " + "emphasized the \"golden anniversary\" with various gold-themed initiatives, as well as temporarily " + "suspending the tradition of naming each Super Bowl game with Roman numerals (under which the game " + "would have been known as \"Super Bowl L\"), so that the logo could prominently feature the Arabic " + "numerals 50.";
    ta = Quantifier.taBuilder.createTextAnnotation(paragraph);
    quantifier.addView(ta);
    assertTrue(ta.hasView(ViewNames.QUANTITIES));
    assertTrue(isEqual("[= 2015.0  season  ]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(0).getLabel()));
    assertTrue(isEqual("[= 24.0  – 10  ]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(1).getLabel()));
    assertTrue(isEqual("[= Date(02/07/XXXX)]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(2).getLabel()));
    assertTrue(isEqual("[daterange[= Date(01/01/2016)][= Date(12/31/2016)]]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(3).getLabel()));
    assertTrue(isEqual("[= 50.0   ]", ta.getView(ViewNames.QUANTITIES).getConstituents().get(4).getLabel()));
    // another long paragraph
    paragraph = "The annual NFL Experience was held at the Moscone Center in San Francisco. In addition, " + "\"Super Bowl City\" opened on January 30 at Justin Herman Plaza on The Embarcadero, featuring games " + "and activities that will highlight the Bay Area's technology, culinary creations, and cultural diversity. " + "More than 1 million people are expected to attend the festivities in San Francisco during Super Bowl Week. San Francisco mayor Ed Lee said of the highly visible homeless presence in this area \"they are going to have to leave\". San Francisco city supervisor Jane Kim unsuccessfully lobbied for the NFL to reimburse San Francisco for city services in the amount of $5 million.";
    ta = Quantifier.taBuilder.createTextAnnotation(paragraph);
    quantifier.addView(ta);
    assertTrue(ta.hasView(ViewNames.QUANTITIES));
// TODO: make sure the extracted quantities are correct.
}
Also used : Quantifier(edu.illinois.cs.cogcomp.quant.driver.Quantifier) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Test(org.junit.Test)

Example 2 with Quantifier

use of edu.illinois.cs.cogcomp.quant.driver.Quantifier in project cogcomp-nlp by CogComp.

the class PipelineFactory method buildAnnotators.

/**
 * instantiate a set of annotators for use in an AnnotatorService object by default, will use
 * lazy initialization where possible -- change this behavior with the
 * {@link PipelineConfigurator#USE_LAZY_INITIALIZATION} property.
 *
 * @param nonDefaultRm ResourceManager with all non-default values for Annotators
 * @return a Map from annotator view name to annotator
 */
private static Map<String, Annotator> buildAnnotators(ResourceManager nonDefaultRm) throws IOException {
    ResourceManager rm = new PipelineConfigurator().getConfig(new Stanford331Configurator().getConfig(nonDefaultRm));
    String timePerSentence = rm.getString(Stanford331Configurator.STFRD_TIME_PER_SENTENCE);
    String maxParseSentenceLength = rm.getString(Stanford331Configurator.STFRD_MAX_SENTENCE_LENGTH);
    boolean useLazyInitialization = rm.getBoolean(PipelineConfigurator.USE_LAZY_INITIALIZATION.key, PipelineConfigurator.TRUE);
    Map<String, Annotator> viewGenerators = new HashMap<>();
    if (rm.getBoolean(PipelineConfigurator.USE_POS)) {
        POSAnnotator pos = new POSAnnotator();
        viewGenerators.put(pos.getViewName(), pos);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_LEMMA)) {
        IllinoisLemmatizer lem = new IllinoisLemmatizer(rm);
        viewGenerators.put(lem.getViewName(), lem);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_SHALLOW_PARSE)) {
        viewGenerators.put(ViewNames.SHALLOW_PARSE, new ChunkerAnnotator());
    }
    if (rm.getBoolean(PipelineConfigurator.USE_NER_CONLL)) {
        NERAnnotator nerConll = NerAnnotatorManager.buildNerAnnotator(rm, ViewNames.NER_CONLL);
        viewGenerators.put(nerConll.getViewName(), nerConll);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_NER_ONTONOTES)) {
        NERAnnotator nerOntonotes = NerAnnotatorManager.buildNerAnnotator(rm, ViewNames.NER_ONTONOTES);
        viewGenerators.put(nerOntonotes.getViewName(), nerOntonotes);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_DEP)) {
        DepAnnotator dep = new DepAnnotator();
        viewGenerators.put(dep.getViewName(), dep);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_STANFORD_DEP) || rm.getBoolean(PipelineConfigurator.USE_STANFORD_PARSE)) {
        Properties stanfordProps = new Properties();
        stanfordProps.put("annotators", "pos, parse");
        stanfordProps.put("parse.originalDependencies", true);
        stanfordProps.put("parse.maxlen", maxParseSentenceLength);
        // per sentence? could be per
        stanfordProps.put("parse.maxtime", timePerSentence);
        // document but no idea from
        // stanford javadoc
        POSTaggerAnnotator posAnnotator = new POSTaggerAnnotator("pos", stanfordProps);
        ParserAnnotator parseAnnotator = new ParserAnnotator("parse", stanfordProps);
        int maxLength = Integer.parseInt(maxParseSentenceLength);
        boolean throwExceptionOnSentenceLengthCheck = rm.getBoolean(Stanford331Configurator.THROW_EXCEPTION_ON_FAILED_LENGTH_CHECK.key);
        if (rm.getBoolean(PipelineConfigurator.USE_STANFORD_DEP)) {
            StanfordDepHandler depParser = new StanfordDepHandler(posAnnotator, parseAnnotator, maxLength, throwExceptionOnSentenceLengthCheck);
            viewGenerators.put(depParser.getViewName(), depParser);
        }
        if (rm.getBoolean(PipelineConfigurator.USE_STANFORD_PARSE)) {
            StanfordParseHandler parser = new StanfordParseHandler(posAnnotator, parseAnnotator, maxLength, throwExceptionOnSentenceLengthCheck);
            viewGenerators.put(parser.getViewName(), parser);
        }
    }
    if (rm.getBoolean(PipelineConfigurator.USE_SRL_VERB)) {
        Properties verbProps = new Properties();
        String verbType = SRLType.Verb.name();
        verbProps.setProperty(SrlConfigurator.SRL_TYPE.key, verbType);
        ResourceManager verbRm = new ResourceManager(verbProps);
        rm = Configurator.mergeProperties(rm, verbRm);
        try {
            SemanticRoleLabeler verbSrl = new SemanticRoleLabeler(rm, useLazyInitialization);
            viewGenerators.put(ViewNames.SRL_VERB, verbSrl);
        } catch (Exception e) {
            throw new IOException("SRL verb cannot init: " + e.getMessage());
        }
    }
    if (rm.getBoolean(PipelineConfigurator.USE_SRL_NOM)) {
        Properties nomProps = new Properties();
        String nomType = SRLType.Nom.name();
        nomProps.setProperty(SrlConfigurator.SRL_TYPE.key, nomType);
        ResourceManager nomRm = new ResourceManager(nomProps);
        rm = Configurator.mergeProperties(rm, nomRm);
        try {
            SemanticRoleLabeler nomSrl = new SemanticRoleLabeler(rm, useLazyInitialization);
            // note that you can't call nomSrl (or verbSrl).getViewName() as it may not be
            // initialized yet
            viewGenerators.put(ViewNames.SRL_NOM, nomSrl);
        // viewGenerators.put(ViewNames.SRL_NOM,new SrlHandler("NomSRL", "5.1.9", nomType,
        // ViewNames.SRL_NOM,
        // useLazyInitialization, rm));
        } catch (Exception e) {
            throw new IOException("SRL nom cannot init .." + e.getMessage());
        }
    }
    if (rm.getBoolean(PipelineConfigurator.USE_QUANTIFIER)) {
        Quantifier quantifierAnnotator = new Quantifier();
        viewGenerators.put(ViewNames.QUANTITIES, quantifierAnnotator);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_TRANSLITERATION)) {
        for (Language lang : TransliterationAnnotator.supportedLanguages) {
            TransliterationAnnotator transliterationAnnotator = new TransliterationAnnotator(true, lang);
            viewGenerators.put(ViewNames.TRANSLITERATION + "_" + lang.getCode(), transliterationAnnotator);
        }
    }
    if (rm.getBoolean(PipelineConfigurator.USE_SRL_PREP)) {
        PrepSRLAnnotator prepSRLAnnotator = new PrepSRLAnnotator();
        viewGenerators.put(ViewNames.SRL_PREP, prepSRLAnnotator);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_SRL_COMMA)) {
        CommaLabeler commaLabeler = new CommaLabeler();
        viewGenerators.put(ViewNames.SRL_COMMA, commaLabeler);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_VERB_SENSE)) {
        VerbSenseAnnotator verbSense = new VerbSenseAnnotator();
        viewGenerators.put(ViewNames.VERB_SENSE, verbSense);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_MENTION)) {
        MentionAnnotator mentionAnnotator = new MentionAnnotator("ACE_TYPE");
        viewGenerators.put(ViewNames.MENTION, mentionAnnotator);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_RELATION)) {
        viewGenerators.put(ViewNames.RELATION, new RelationAnnotator(true));
    }
    if (rm.getBoolean(PipelineConfigurator.USE_TIMEX3)) {
        Properties rmProps = new TemporalChunkerConfigurator().getDefaultConfig().getProperties();
        TemporalChunkerAnnotator tca = new TemporalChunkerAnnotator(new ResourceManager(rmProps));
        viewGenerators.put(ViewNames.TIMEX3, tca);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_DATALESS_ESA)) {
        rm = new ESADatalessConfigurator().getConfig(nonDefaultRm);
        ESADatalessAnnotator esaDataless = new ESADatalessAnnotator(rm);
        viewGenerators.put(ViewNames.DATALESS_ESA, esaDataless);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_DATALESS_W2V)) {
        rm = new W2VDatalessConfigurator().getConfig(nonDefaultRm);
        W2VDatalessAnnotator w2vDataless = new W2VDatalessAnnotator(rm);
        viewGenerators.put(ViewNames.DATALESS_W2V, w2vDataless);
    }
    if (rm.getBoolean(PipelineConfigurator.USE_QUESTION_TYPER)) {
        QuestionTypeAnnotator questionTyper = new QuestionTypeAnnotator();
        viewGenerators.put(ViewNames.QUESTION_TYPE, questionTyper);
    }
    return viewGenerators;
}
Also used : HashMap(java.util.HashMap) VerbSenseAnnotator(edu.illinois.cs.cogcomp.verbsense.VerbSenseAnnotator) MentionAnnotator(org.cogcomp.md.MentionAnnotator) Properties(java.util.Properties) POSTaggerAnnotator(edu.stanford.nlp.pipeline.POSTaggerAnnotator) PrepSRLAnnotator(edu.illinois.cs.cogcomp.prepsrl.PrepSRLAnnotator) Language(edu.illinois.cs.cogcomp.core.constants.Language) SemanticRoleLabeler(edu.illinois.cs.cogcomp.srl.SemanticRoleLabeler) TemporalChunkerAnnotator(edu.illinois.cs.cogcomp.temporal.normalizer.main.TemporalChunkerAnnotator) W2VDatalessAnnotator(edu.illinois.cs.cogcomp.datalessclassification.ta.W2VDatalessAnnotator) TemporalChunkerConfigurator(edu.illinois.cs.cogcomp.temporal.normalizer.main.TemporalChunkerConfigurator) Stanford331Configurator(edu.illinois.cs.cogcomp.pipeline.common.Stanford331Configurator) ParserAnnotator(edu.stanford.nlp.pipeline.ParserAnnotator) ESADatalessAnnotator(edu.illinois.cs.cogcomp.datalessclassification.ta.ESADatalessAnnotator) POSAnnotator(edu.illinois.cs.cogcomp.pos.POSAnnotator) ResourceManager(edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager) IOException(java.io.IOException) RelationAnnotator(org.cogcomp.re.RelationAnnotator) IOException(java.io.IOException) TemporalChunkerAnnotator(edu.illinois.cs.cogcomp.temporal.normalizer.main.TemporalChunkerAnnotator) ChunkerAnnotator(edu.illinois.cs.cogcomp.chunker.main.ChunkerAnnotator) PipelineConfigurator(edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator) TemporalChunkerAnnotator(edu.illinois.cs.cogcomp.temporal.normalizer.main.TemporalChunkerAnnotator) W2VDatalessAnnotator(edu.illinois.cs.cogcomp.datalessclassification.ta.W2VDatalessAnnotator) RelationAnnotator(org.cogcomp.re.RelationAnnotator) ESADatalessAnnotator(edu.illinois.cs.cogcomp.datalessclassification.ta.ESADatalessAnnotator) POSTaggerAnnotator(edu.stanford.nlp.pipeline.POSTaggerAnnotator) PrepSRLAnnotator(edu.illinois.cs.cogcomp.prepsrl.PrepSRLAnnotator) ParserAnnotator(edu.stanford.nlp.pipeline.ParserAnnotator) MentionAnnotator(org.cogcomp.md.MentionAnnotator) QuestionTypeAnnotator(edu.illinois.cs.cogcomp.question_typer.QuestionTypeAnnotator) ChunkerAnnotator(edu.illinois.cs.cogcomp.chunker.main.ChunkerAnnotator) DepAnnotator(edu.illinois.cs.cogcomp.depparse.DepAnnotator) POSAnnotator(edu.illinois.cs.cogcomp.pos.POSAnnotator) VerbSenseAnnotator(edu.illinois.cs.cogcomp.verbsense.VerbSenseAnnotator) NERAnnotator(edu.illinois.cs.cogcomp.ner.NERAnnotator) NERAnnotator(edu.illinois.cs.cogcomp.ner.NERAnnotator) ESADatalessConfigurator(edu.illinois.cs.cogcomp.datalessclassification.config.ESADatalessConfigurator) DepAnnotator(edu.illinois.cs.cogcomp.depparse.DepAnnotator) W2VDatalessConfigurator(edu.illinois.cs.cogcomp.datalessclassification.config.W2VDatalessConfigurator) IllinoisLemmatizer(edu.illinois.cs.cogcomp.nlp.lemmatizer.IllinoisLemmatizer) Quantifier(edu.illinois.cs.cogcomp.quant.driver.Quantifier) CommaLabeler(edu.illinois.cs.cogcomp.comma.CommaLabeler) StanfordParseHandler(edu.illinois.cs.cogcomp.pipeline.handlers.StanfordParseHandler) QuestionTypeAnnotator(edu.illinois.cs.cogcomp.question_typer.QuestionTypeAnnotator) StanfordDepHandler(edu.illinois.cs.cogcomp.pipeline.handlers.StanfordDepHandler)

Aggregations

Quantifier (edu.illinois.cs.cogcomp.quant.driver.Quantifier)2 ChunkerAnnotator (edu.illinois.cs.cogcomp.chunker.main.ChunkerAnnotator)1 CommaLabeler (edu.illinois.cs.cogcomp.comma.CommaLabeler)1 Language (edu.illinois.cs.cogcomp.core.constants.Language)1 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)1 ResourceManager (edu.illinois.cs.cogcomp.core.utilities.configuration.ResourceManager)1 ESADatalessConfigurator (edu.illinois.cs.cogcomp.datalessclassification.config.ESADatalessConfigurator)1 W2VDatalessConfigurator (edu.illinois.cs.cogcomp.datalessclassification.config.W2VDatalessConfigurator)1 ESADatalessAnnotator (edu.illinois.cs.cogcomp.datalessclassification.ta.ESADatalessAnnotator)1 W2VDatalessAnnotator (edu.illinois.cs.cogcomp.datalessclassification.ta.W2VDatalessAnnotator)1 DepAnnotator (edu.illinois.cs.cogcomp.depparse.DepAnnotator)1 NERAnnotator (edu.illinois.cs.cogcomp.ner.NERAnnotator)1 IllinoisLemmatizer (edu.illinois.cs.cogcomp.nlp.lemmatizer.IllinoisLemmatizer)1 PipelineConfigurator (edu.illinois.cs.cogcomp.pipeline.common.PipelineConfigurator)1 Stanford331Configurator (edu.illinois.cs.cogcomp.pipeline.common.Stanford331Configurator)1 StanfordDepHandler (edu.illinois.cs.cogcomp.pipeline.handlers.StanfordDepHandler)1 StanfordParseHandler (edu.illinois.cs.cogcomp.pipeline.handlers.StanfordParseHandler)1 POSAnnotator (edu.illinois.cs.cogcomp.pos.POSAnnotator)1 PrepSRLAnnotator (edu.illinois.cs.cogcomp.prepsrl.PrepSRLAnnotator)1 QuestionTypeAnnotator (edu.illinois.cs.cogcomp.question_typer.QuestionTypeAnnotator)1