awaisathar/dependensee

Null pointer exception in dependensee

Closed this issue · 4 comments

Hi,

When I run the following code, there is a null pointer exception in com.chaoticity.dependensee.Graph.addEdge()
(See below the code). The list of typed dependencies is printed in the output.

The code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import com.chaoticity.dependensee.*; 

import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.process.TokenizerFactory;
import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.process.PTBTokenizer;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.parser.nndep.DependencyParser;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
import edu.stanford.nlp.ling.TaggedWord;

...

    String text = "I can almost always tell when movies use fake dinosaurs.";
    String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/" + 
                                    "english-left3words-distsim.tagger";
    String modelPath = DependencyParser.DEFAULT_MODEL; 
    String writeFilePath = ROOT_DIR + "sts2016" + sep + "test" + sep;  

    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
        System.out.println(gs.typedDependenciesCCprocessed());

        try { 
            Main.writeImage(gs.root(), gs.typedDependenciesCCprocessed(), 
                            writeFilePath + "littletest1.png");  
        } 
        catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 

Output from running the code:

Reading POS tagger model from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [1.3 sec].
Loading depparse model file: edu/stanford/nlp/models/parser/nndep/english_UD.gz ...
PreComputed 100000, Elapsed Time: 2.034 (s)
Initializing dependency parser done [5.0 sec].
[nsubj(tell-5, I-1), aux(tell-5, can-2), advmod(always-4, almost-3), advmod(tell-5, always-4), root(ROOT-0, tell-5), advmod(use-8, when-6), nsubj(use-8, movies-7), advcl(tell-5, use-8), amod(dinosaurs-10, fake-9), dobj(use-8, dinosaurs-10), punct(tell-5, .-11)]
java.lang.NullPointerException
at com.chaoticity.dependensee.Graph.addEdge(Graph.java:51)
at com.chaoticity.dependensee.Main.getGraph(Main.java:117)
at com.chaoticity.dependensee.Main.writeImage(Main.java:208)
at Test.Test27(Test.java:119)
at Test.main(Test.java:1045)

Thanks for reporting this. For some reason, the tree rooted at gs.root() does not yield a tagged tree. I've simplified things a bit and now you should be able to directly process the typed dependency list by using

Main.writeImage(s.typedDependenciesCCprocessed(), "image.png", 3);

Yes, here's the commit 55efd47