title: Iterate over Stanford NLP tree properly
body: <p>My objective is to figure out whether the given word is <strong>preposition</strong> or <strong>subordinating conjunction</strong>. The main problem with stanford parser is that it has a single <strong>IN</strong> tag for both parts of speech mentioned above.
  So, to uniquely identify them I had implemented following procedure:</p>
  
  <p>I am trying to iterate over a nlp tree generated from Stanford parser.</p>
  
  <p>Image first:</p>
  
  <p><img src="https://i.stack.imgur.com/zgpRi.png" alt="parse tree of the sentence"></p>
  
  <p>Here I am trying to do in this way...</p>
  
  <pre><code>if IN is found
  {
      parentValue = parent of IN
  
      if parentValue is SBAR
      {        
        get leaf or child of IN ... (ie word itself)
        mark it as subordinating conjunction
      }
  
  
      if parentValue is PP
      {        
        get leaf or child of IN ... (ie word itself)
        mark it as preposition
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Why am I checking <strong>IN</strong> first?</p>
  </blockquote>
  
  <p>Basically, according to my understanding, if a sentence has preposition or subordinating conjunction, it either comes under <strong>PP or SBAR</strong> respectievely.But the catch is it can be possible there might be no <strong>IN</strong> as child , it can be another sentence, NP or anything. So, I zeroed to finding <strong>IN</strong> first.
  (Suggestions and corrections welcomed.)</p>
  
  <p><img src="https://i.stack.imgur.com/4yYXr.png" alt="see the case here"></p>
  
  <p>Also, I assume here, there will be no surprises <strong>below IN</strong> in any of the sentences I encounter in future. If I am wrong please correct me.</p>
  
  <p>I have written following code</p>
  
  <pre><code>package com.test.olabs.main;
  
  import java.util.List;
  
  import com.olabs.nlp.OlabsTokenizer;
  
  import edu.stanford.nlp.ling.CoreLabel;
  import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
  import edu.stanford.nlp.tagger.maxent.MaxentTagger;
  import edu.stanford.nlp.trees.Tree;
  
  public class MyTester {
  
      public static void main(String[] args) {
          MyTester t = new MyTester();
          t.test();
  
      }
  
      String sentence = "It seemed as if whole town was mourning his death.";
  
      private static final String ENG_BI_MODEL = "edu/stanford/nlp/models/pos-tagger/english-bidirectional/english-bidirectional-distsim.tagger";
      private static final String PCG_MODEL = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
      private static final MaxentTagger mxt = new MaxentTagger(ENG_BI_MODEL);
  
      private static final LexicalizedParser parser = LexicalizedParser
              .loadModel(PCG_MODEL);
      Tree parentNode = null;
      private void findPro(Tree t) {
          System.out.println("findpro tree value " + t.label().value());
          if (t.label().value().equals("IN")) {
              System.out.println("-----------in IN");
              if (parentNode.value().equals("PP"))
              {
                  System.out.println("found prep " +t.label().value());
              }
              if (parentNode.value().equals("SBAR"))
              {
                  System.out.println("----------in sbar "+t.label().value());
              }
          } else {
              for (Tree child : t.children()) {
                  parentNode = t; // parent is t and childVar is child , we need
                                  // to store parent ... so we stored it
                  findPro(child);
              }
          }
      }
  
      public Tree parse(String s) {
          List&lt;CoreLabel&gt; tokens = OlabsTokenizer.tokenizeString(s);
          mxt.tagCoreLabels(tokens);
          Tree tree = parser.apply(tokens);
          return tree;
      }
  
      void test() {
          MyTester test = new MyTester();
          Tree t = test.parse(sentence);
          findPro(t);
  
      }
  
  }
  </code></pre>
  
  <p>What I am able to do sucessfully with this code?
  1. I can get <strong>IN</strong> from tree.
  2. I can get <strong>parent of IN</strong> ie <strong>SBAR or PP</strong>  (Due to hackey code, as calling .parent() on tree gives you null)</p>
  
  <p>Problem is , now I am not able to get child of IN, I get two values <strong>as and if</strong> . You can check parse output for this in first image above.
  Answer should be only <strong>if</strong>.</p>
  
  <p>Here , the output is as follows :</p>
  
  <pre><code>findpro tree value ROOT
  findpro tree value S
  findpro tree value NP
  findpro tree value PRP
  findpro tree value It
  findpro tree value VP
  findpro tree value VBD
  findpro tree value seemed
  findpro tree value SBAR
  findpro tree value IN
  -----------in IN
  ----------in sbar IN
  findpro tree value IN
  -----------in IN
  ----------in sbar IN
  findpro tree value S
  findpro tree value NP
  findpro tree value JJ
  findpro tree value whole
  findpro tree value NN
  findpro tree value town
  findpro tree value VP
  findpro tree value VBD
  findpro tree value was
  findpro tree value VP
  findpro tree value VBG
  findpro tree value mourning
  findpro tree value NP
  findpro tree value PRP$
  findpro tree value his
  findpro tree value NN
  findpro tree value death
  findpro tree value .
  findpro tree value .
  </code></pre>
  
  <p>Basically , loop goes in <strong>IN twice</strong> , PP <strong>is not printed at all</strong>. I think it should go in IN only once and give output as <strong>if</strong>.Is this bug in stanford parser or my code?</p>
  
  <p>How can I get this all done correctly? Help needed.</p>
  
  <p>FYI,
  I have also tried first part of it in 
  <a href="https://stackoverflow.com/questions/30450705/identify-prepositons-and-individual-pos">Identify prepositons and individual POS</a>
  but without much help.</p>