Why do I get UnsupportedOperationException with Stanford CoreNLP

81 views Asked by At

I would like to find the head word of each phrase (constituent) from a Tree in Stanford CoreNLP, but when I try tree.Parent() for any of the constituents, I get UnsupportedOperationException. What am I doing wrong?

Here is my code:

List<Tree> allConstituents = new ArrayList<>();
    private Tree parseTree;

  List<CoreMap> sentences = LoadAndParse(language, filetype, modelPath, text);

            for (CoreMap sentence : sentences) {
                Tree parse = sentence.get(TreeAnnotation.class);
                allConstituents = parseTree.subTreeList();

            for (int i = 0; i < allConstituents.size(); i++) {
                    Tree constituentTree = allConstituents.get(i);
                    HeadFinder headFinder = new SemanticHeadFinder();
                    String head = constituentTree.headTerminal(headFinder, constituentTree.parent());

                }
              }

Here is an example that I have:

Your tasks are challenging:

I get 13 as the size of the parseTree.subTreeList(), but for all of them, I get the UnsupportedOperationException on the constituentTree.parent() method. Can anyone help me what is the correct way to get the semantic head of "all" constituents in the tree?

1

There are 1 answers

0
user1419243 On BEST ANSWER

I'm not sure if it is really an answer which works for all, but in my case it was helpful:

Use the main Tree which includes the whole sentence as the second input for all constituents; that is:

                String head = constituentTree.headTerminal(headFinder, parseTree);