Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get constituents from a tree annotation? #97

Closed
Eric-Ryan opened this issue Jul 8, 2019 · 10 comments
Closed

How do I get constituents from a tree annotation? #97

Eric-Ryan opened this issue Jul 8, 2019 · 10 comments

Comments

@Eric-Ryan
Copy link

Eric-Ryan commented Jul 8, 2019

In this example it shows the ability to extract constituents from a tree annotation:

    // get tree
    Tree tree =
        annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(TreeCoreAnnotations.TreeAnnotation.class);
    System.out.println(tree);
    Set<Constituent> treeConstituents = tree.constituents(new LabeledScoredConstituentFactory());
    for (Constituent constituent : treeConstituents) {
      if (constituent.label() != null &&
          (constituent.label().toString().equals("VP") || constituent.label().toString().equals("NP"))) {
        System.err.println("found constituent: "+constituent.toString());
        System.err.println(tree.getLeaves().subList(constituent.start(), constituent.end()+1));
      }
    }

I took the above code from the
Stanford CoreNLP Website
(https://stanfordnlp.github.io/CoreNLP/parse.html#examples).
I attempted to do this but
the constituents() method does not exist:

var sentences = annotation.get(typeof(CoreAnnotations.SentencesAnnotation));

foreach(CoreMap sentence in sentences as ArrayList){

    var tree = sentence.get(typeof(TreeCoreAnnotations.TreeAnnotation));

    tree.constituents(); // Here the constituents() method does not exist
}
@sergey-tihon
Copy link
Owner

sergey-tihon commented Jul 8, 2019

most probably you cannot use typeof you need java.lang.Class

public static java.lang.Class GetAnnotationClass<T>()
            => ikvm.@internal.ClassLiteral<T>.Value; // or = new T().getClass()

var sentences = annotation.get(GetAnnotationClass<CoreAnnotations.SentencesAnnotation>());
foreach(CoreMap sentence in sentences as ArrayList){
    var tree = sentence.get(GetAnnotationClass<TreeCoreAnnotations.TreeAnnotation>()) as Tree;
    tree.constituents(); // most likely Tree is edu.stanford.nlp.trees.Tree
}

@Eric-Ryan
Copy link
Author

Thank you for the quick response!

@Eric-Ryan
Copy link
Author

I get the error " cannot convert from 'method group' to 'Class' " on these two lines:

GetAnnotationClass<CoreAnnotations.SentencesAnnotation>
GetAnnotationClass<TreeCoreAnnotations.TreeAnnotation>

@sergey-tihon
Copy link
Owner

Sorry, I forgot brackets, GetAnnotationClass is a method that that we should call, so GetAnnotationClass<T>()

var sentences = annotation.get(GetAnnotationClass<CoreAnnotations.SentencesAnnotation>());

@Eric-Ryan
Copy link
Author

Eric-Ryan commented Jul 8, 2019

Thank you so much it worked! I am just still new with C# so I don't understand why I'm getting "Object reference not set to an instance of an object." when using "java.util.ArrayList":

            var sentences = annotation.get(GetAnnotationClass<CoreAnnotations.SentencesAnnotation>());
            foreach (CoreMap sentence in sentences as java.util.ArrayList)
            {
                Tree tree = sentence.get(GetAnnotationClass<TreeCoreAnnotations.TreeAnnotation>()) as Tree;
                var treeConstituents = tree.constituents(new LabeledScoredConstituentFactory());
                foreach (Constituent constituent in treeConstituents as java.util.ArrayList) // I get the error here
                {
                    if (constituent.label().toString().Equals("NP"))
                    {
                        Console.WriteLine(constituent.toString());
                        Console.WriteLine(tree.getLeaves().subList(constituent.start(), constituent.end() + 1));
                    }
                }

            }

As compared with the example I showed previously by the CoreNLP website, I cannot use "Set<Constituent>" so I used "var" instead but still get the error.

@Eric-Ryan
Copy link
Author

Eric-Ryan commented Jul 8, 2019

Nevermind! I think I might have solved it by using "java.util.AbstractSet" instead of "java.util.ArrayList". Thanks again!

@sergey-tihon
Copy link
Owner

interior with Java/IKVM types is not very intuitive.
If you not sure what exact type is returned from annotation, you can take a look in debugger it Console.WriteLine(obj.GetType())

@Eric-Ryan
Copy link
Author

Eric-Ryan commented Jul 14, 2019

Oh I see now, thank you. Excuse me for bothering, and perhaps this is out of your area of expertise but, I have been using CoreNLP within Unity and since adding the lines:

    public static java.lang.Class GetAnnotationClass<T>()
        	=> ikvm.@internal.ClassLiteral<T>.Value;

I have been getting an error about not being able to load the file "IKVM.OpenJDK.Core, Version 8.1". I was wondering if you could think of any DLL that I could be missing which could add the "GetAnnotationClass()" functionality within Unity? The full error within the console is this:

Error: Could not load signature of NLP:GetAnnotationClass due to: Could not load file or assembly 'IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one of its dependencies. assembly:IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58 type: member:(null) signature:

I greatly appreciate any feedback.

@sergey-tihon
Copy link
Owner

  1. I would check that IKVM.OpenJDK.Core is actually present near you exe or
  2. If you are on Windows you use Fuslogvw.exe to log all assembly resolution requests and result to find out what file was no resolved.
  3. you can try to replace GetAnnotationClass<T>() call by new T().getClass() and compare results

@Eric-Ryan
Copy link
Author

new T().getClass() worked perfectly. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants