0

I am struggling with a problem for a while. Suppose you have a collection of typed dependencies (from edu.stanford.nlp.trees.TypedDependency) and you want to convert them to a tree (from edu.stanford.nlp.trees.Tree).

For instance:

// Suppose you get the typed dependencies from a grammatical structure object
Collection<TypedDependency> atd = gs.allTypedDependencies(); 
Tree t = null;

Now, how could I transform the object atd into a tree (which could be stored in t)?

Belphegor
  • 4,456
  • 11
  • 34
  • 59

1 Answers1

1

These are not equivalent types. Typed Dependencies define a dependency tree (SemanticGraph), whereas the Tree class represents a constituency tree. While there is actually code to produce a dependency tree from a constituency tree, there is nothing in CoreNLP that would do the reverse. As far as I know, this is actually a nontrivial task.

My recommendation is to run the parse annotator rather than the depparse annotator, and get a Tree that way (see TreeCoreAnnotations.TreeAnnotation).

Gabor Angeli
  • 5,729
  • 1
  • 18
  • 29
  • Actually I am trying to convert GrammaticalStructure to a Tree (stackoverflow.com/questions/32565312/…). So, I thought that exploiting the Typed Dependencies was a good start. However, I found out that it is possible to construct the tree from a string like this: `(ROOT (S (NP (NNP Arnold) (NNP Schwarzenegger)) (VP (VBZ is) (NP (DT a) (JJ famous) (NN actor)))))` for the sentence "Arnold Schwarzenegger is a famous actor". This is done with the static method `Tree.valueOf()`. However, I don't know how to get to that string? Any suggestions? – Belphegor Sep 17 '15 at 14:36
  • BTW, thanks a lot for your answer, you definitely helped me a little with this answer, +1! – Belphegor Sep 17 '15 at 14:36
  • 1
    To get a constituency tree (like `(ROOT (S (NP (NNP Arnold) (NNP Schwarzenegger)) (VP (VBZ is) (NP (DT a) (JJ famous) (NN actor)))))`) you need to run the constituency parser, rather than the dependency parser. `LexicalizedParser.parse()` is the concrete method that does this; but, I recommend going through the CoreNLP interface and calling the `parse` annotator. – Gabor Angeli Sep 17 '15 at 18:38
  • Actually, I already have the `LexicalizedParser` but I desperately need the NN parser for my research (needs to translate the `GrammaticalStructure` into `Tree` so I could extend an already existing code), so I will have to find a way to do this. I looked at the `parse` annotator, but I cannot see anything helpful for this there. Tnx again for your constructive comments, and of course, in case you have some other suggestions - please let me know! Cheers! – Belphegor Sep 18 '15 at 10:13
  • 1
    If you have to convert from dependency trees, you might be interested in recent research on the topic (NAACL 2015): http://www.cs.cmu.edu/~nasmith/papers/kong+rush+smith.naacl15.pdf – Gabor Angeli Sep 18 '15 at 18:52