title: How can I find grammatical relations of a noun phrase using Stanford Parser or Stanford CoreNLP
body: <p>I am using stanford CoreNLP to try to find grammatical relations of noun phrases.</p>
  
  <p>Here is an example: </p>
  
  <p>Given the sentence "The fitness room was dirty." </p>
  
  <p>I managed to identify "The fitness room" as my target noun phrase. I am now looking for a way to find that the "dirty" adjective has a relationship to "the fitness room" <strong>and not only to</strong> "room".</p>
  
  <p>example code:</p>
  
  <pre><code>private static void doSentenceTest(){
      Properties props = new Properties();
      props.put("annotators","tokenize, ssplit, pos, lemma, ner, parse, dcoref");
      StanfordCoreNLP stanford = new StanfordCoreNLP(props);
  
      TregexPattern npPattern = TregexPattern.compile("@NP");
  
      String text = "The fitness room was dirty.";
  
  
      // create an empty Annotation just with the given text
      Annotation document = new Annotation(text);
      // run all Annotators on this text
      stanford.annotate(document);
  
      List&lt;CoreMap&gt; sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
      for (CoreMap sentence : sentences) {
  
          Tree sentenceTree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
          TregexMatcher matcher = npPattern.matcher(sentenceTree);
  
          while (matcher.find()) {
              //this tree should contain "The fitness room" 
              Tree nounPhraseTree = matcher.getMatch();
              //Question : how do I find that "dirty" has a relationship to the nounPhraseTree
  
  
          }
  
          // Output dependency tree
          TreebankLanguagePack tlp = new PennTreebankLanguagePack();
          GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
          GrammaticalStructure gs = gsf.newGrammaticalStructure(sentenceTree);
          Collection&lt;TypedDependency&gt; tdl = gs.typedDependenciesCollapsed();
  
          System.out.println("typedDependencies: "+tdl); 
  
      }
  
  }
  </code></pre>
  
  <p>I used the Stanford CoreNLP on the sentence extracted the root Tree object of it. On this tree object I managed to extract Noun Phrases using a TregexPattern and a TregexMatcher. This gives me a child Tree that contains the actual noun phrase. What I would like to do know is find modifiers of the noun phrase in the original sentence. </p>
  
  <p>The typedDependecies ouptut gives me the following : </p>
  
  <pre><code>typedDependencies: [det(room-3, The-1), nn(room-3, fitness-2), nsubj(dirty-5, room-3), cop(dirty-5, was-4), root(ROOT-0, dirty-5)]
  </code></pre>
  
  <p>where I can see <strong>nsubj(dirty-5, room-3)</strong> but I dont have the full noun phrase as dominator.</p>
  
  <p>I hope I am clear enough.
  Any help appreciated.</p>