Thursday, May 16, 2013

Search Noun , Adjectives & Verbs from Text Using JAVA with Apache OpenNLP Parse

Description : 

Some time we need to parse the text from text paragraph or we need to search some nouns , verbs etc from the paragraph. To sort the nouns and verbs or etc from paragraph , we need to implement Natural Language Processing , which is he part of Artificial Intelligence. But there are number of tools are provide to perform the task , and these tools are also used with programming languages. There are several tools are provide by the OpenNLP to process the paragraph , according to requirements , here is the documentation , that define how to use OpenNLP in different conditions or requirements. Today's we show how to use Apache OpenNLP to process the paragraph with the help of Java Using Parse Technique .

Requirements :

  • Download the Apache OpenNLP jars from OpenNLP website. 
  • Download JDK 7 (From my side , this is tested in JDK 7)
  • Download parser-chunking tool from here (there are so many tools are here)

Following is the code of Program : 

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;

public class ParserTest {

 static Set<String> nounPhrases = new HashSet<>();
 static Set<String> adjectivePhrases = new HashSet<>();
 static Set<String> verbPhrases = new HashSet<>();
 private static String line = "The Moon is a barren, rocky world     without air and water. It has dark lava plain on its surface. " +
"The Moon is filled wit craters. It has no light of its own. It gets its light from the Sun. The Moo keeps changing its " +
"shape as it moves round the Earth. It spins on its axis in 27.3 days stars were named after the Edwin Aldrin were the " +
first ones to set their foot on the Moon on 21 July 1969 They reached the Moon in their space craft named Apollo II";
 public void getNounPhrases(Parse p) {
  if (p.getType().equals("NN") || p.getType().equals("NNS") ||  p.getType().equals("NNP") || p.getType().equals("NNPS")) {
        nounPhrases.add(p.getCoveredText());
  }

  if (p.getType().equals("JJ") || p.getType().equals("JJR") || p.getType().equals("JJS")) {
    adjectivePhrases.add(p.getCoveredText());
  }
   
  if (p.getType().equals("VB") || p.getType().equals("VBP") || p.getType().equals("VBG")|| p.getType().equals("VBD") || p.getType().equals("VBN")) {
    verbPhrases.add(p.getCoveredText());
   }
   
  for (Parse child : p.getChildren()) {
        getNounPhrases(child);
  }
}
public void parserAction() throws Exception {
 InputStream is = new FileInputStream("en-parser-chunking.bin");
 ParserModel model = new ParserModel(is);
 Parser parser = ParserFactory.create(model);
 Parse topParses[] = ParserTool.parseLine(line, parser, 1);
 for (Parse p : topParses){
  //p.show();
  getNounPhrases(p);
 }
}

 public static void main(String[] args) throws Exception {
  new ParserTest().parserAction();
  System.out.println("List of Noun Parse : "+nounPhrases);
  System.out.println("List of Adjective Parse : "+adjectivePhrases);
  System.out.println("List of Verb Parse : "+verbPhrases);
 }
}

in this program the "NN","NNP" etc are the code for finding Nouns , Adjective , Verbs etc . Here are the list of all codes.
Download The Example Code From here