title: How to extract nouns using OpenNLP in Scala?
body: <p>I want to extract noun phrases using openNLP. I have the following Scala code for it.</p>
  
  <pre><code>object ParserTest1 extends Serializable {
  
    def apply(line: String): (String) = {
      val is = new FileInputStream("/home/akshat/en-parser-chunking.bin")
      val model = new ParserModel(is)
      val parser = ParserFactory.create(model)
      var nounPhrases = ""
      val topParses = ParserTool.parseLine(line, parser, 1)
      for (p &lt;- topParses) {
      getNounPhrases(p)
      }
    def getNounPhrases(p: Parse) {
      if (p.getType == "NN" || p.getType == "NNS" || p.getType == "NNP" || 
        p.getType == "NNPS") {
        nounPhrases += p.getCoveredText + " "
      }    
  
      for (child &lt;- p.getChildren) {
        getNounPhrases(child)
      }
  }
  (nounPhrases)
  }
  }
  </code></pre>
  
  <p>My code is working fine but I have to extract only the first two nouns from the line and stop looking for other nouns after that, basically stop the loop after finding the first two nouns.
  What changes should be made in the code for this ?</p>