Search in sources :

Example 1 with DependencyParser

use of edu.stanford.nlp.parser.nndep.DependencyParser in project CoreNLP by stanfordnlp.

the class SceneGraphImageDependencyParser method main.

public static void main(String[] args) throws IOException {
    DependencyParser parser = DependencyParser.loadFromModelFile(DependencyParser.DEFAULT_MODEL);
    String filename = args[0];
    BufferedReader reader = IOUtils.readerFromString(filename);
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        SceneGraphImage img = SceneGraphImage.readFromJSON(line);
        if (img == null) {
            continue;
        }
        for (SceneGraphImageRegion region : img.regions) {
            if (region.tokens != null) {
                region.gs = parser.predict(region.tokens);
            }
        }
        System.out.println(img.toJSON());
    }
}
Also used : SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) DependencyParser(edu.stanford.nlp.parser.nndep.DependencyParser) BufferedReader(java.io.BufferedReader) SceneGraphImageRegion(edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion)

Example 2 with DependencyParser

use of edu.stanford.nlp.parser.nndep.DependencyParser in project CoreNLP by stanfordnlp.

the class TaggerParserPosTagCompatibilityITest method testTagSet4.

private static void testTagSet4(String[] lexParsers, String[] maxentTaggers, String[] srParsers, String[] nnDepParsers) {
    // Choose a reference point to work from. We choose the first maxent tagger, since there must be one of those.
    String refTaggerName = maxentTaggers[0];
    MaxentTagger refTagger = new MaxentTagger(refTaggerName);
    Set<String> tagSet = refTagger.tagSet();
    tagSet.removeAll(tagsToIgnore);
    for (String name : maxentTaggers) {
        MaxentTagger tagger = new MaxentTagger(name);
        Set<String> maxentTagSet = new HashSet<>(tagger.tagSet());
        maxentTagSet.removeAll(tagsToIgnore);
        assertEquals(refTaggerName + " vs. " + name + " tag set mismatch:\n" + "left - right: " + Sets.diff(tagSet, maxentTagSet) + "; right - left: " + Sets.diff(maxentTagSet, tagSet) + "\n", tagSet, maxentTagSet);
    }
    for (String name : lexParsers) {
        LexicalizedParser lp = LexicalizedParser.loadModel(name);
        Set<String> lexParserTagSet = new HashSet<>(lp.getLexicon().tagSet(lp.treebankLanguagePack().getBasicCategoryFunction()));
        lexParserTagSet.removeAll(tagsToIgnore);
        assertEquals(refTaggerName + " vs. " + name + " tag set mismatch:\n" + "left - right: " + Sets.diff(tagSet, lexParserTagSet) + "; right - left: " + Sets.diff(lexParserTagSet, tagSet) + "\n", tagSet, lexParserTagSet);
    }
    for (String name : srParsers) {
        ShiftReduceParser srp = ShiftReduceParser.loadModel(name);
        Set<String> srParserTagSet = new HashSet<>(srp.tagSet());
        srParserTagSet.removeAll(tagsToIgnore);
        assertEquals(refTaggerName + " vs. " + name + " tag set mismatch:\n" + "left - right: " + Sets.diff(tagSet, srParserTagSet) + "; right - left: " + Sets.diff(srParserTagSet, tagSet) + "\n", tagSet, srParserTagSet);
    }
    for (String name : nnDepParsers) {
        DependencyParser dp = DependencyParser.loadFromModelFile(name);
        Set<String> nnDepParserTagSet = new HashSet<String>(dp.getPosSet());
        nnDepParserTagSet.removeAll(tagsToIgnore);
        assertEquals(refTaggerName + " vs. " + name + " tag set mismatch:\n" + "left - right: " + Sets.diff(tagSet, nnDepParserTagSet) + "; right - left: " + Sets.diff(nnDepParserTagSet, tagSet) + "\n", tagSet, nnDepParserTagSet);
    }
}
Also used : MaxentTagger(edu.stanford.nlp.tagger.maxent.MaxentTagger) ShiftReduceParser(edu.stanford.nlp.parser.shiftreduce.ShiftReduceParser) DependencyParser(edu.stanford.nlp.parser.nndep.DependencyParser) LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser)

Example 3 with DependencyParser

use of edu.stanford.nlp.parser.nndep.DependencyParser in project CoreNLP by stanfordnlp.

the class DependencyParserDemo method main.

public static void main(String[] args) {
    String modelPath = DependencyParser.DEFAULT_MODEL;
    String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words-distsim.tagger";
    for (int argIndex = 0; argIndex < args.length; ) {
        switch(args[argIndex]) {
            case "-tagger":
                taggerPath = args[argIndex + 1];
                argIndex += 2;
                break;
            case "-model":
                modelPath = args[argIndex + 1];
                argIndex += 2;
                break;
            default:
                throw new RuntimeException("Unknown argument " + args[argIndex]);
        }
    }
    String text = "I can almost always tell when movies use fake dinosaurs.";
    MaxentTagger tagger = new MaxentTagger(taggerPath);
    DependencyParser parser = DependencyParser.loadFromModelFile(modelPath);
    DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));
    for (List<HasWord> sentence : tokenizer) {
        List<TaggedWord> tagged = tagger.tagSentence(sentence);
        GrammaticalStructure gs = parser.predict(tagged);
        // Print typed dependencies
        log.info(gs);
    }
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) TaggedWord(edu.stanford.nlp.ling.TaggedWord) MaxentTagger(edu.stanford.nlp.tagger.maxent.MaxentTagger) DependencyParser(edu.stanford.nlp.parser.nndep.DependencyParser) StringReader(java.io.StringReader) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) DocumentPreprocessor(edu.stanford.nlp.process.DocumentPreprocessor)

Aggregations

DependencyParser (edu.stanford.nlp.parser.nndep.DependencyParser)3 MaxentTagger (edu.stanford.nlp.tagger.maxent.MaxentTagger)2 HasWord (edu.stanford.nlp.ling.HasWord)1 TaggedWord (edu.stanford.nlp.ling.TaggedWord)1 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)1 ShiftReduceParser (edu.stanford.nlp.parser.shiftreduce.ShiftReduceParser)1 DocumentPreprocessor (edu.stanford.nlp.process.DocumentPreprocessor)1 SceneGraphImage (edu.stanford.nlp.scenegraph.image.SceneGraphImage)1 SceneGraphImageRegion (edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion)1 GrammaticalStructure (edu.stanford.nlp.trees.GrammaticalStructure)1 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1