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

Monday, May 6, 2013

Convert large Images into thumbnails and maintain aspect ratio.

Description : 

Image scalar libraries are use to perform the action with images like to create images thumbnails and many more. there are different vendors provide different type of libraries to perform the action one is Google java API here  and other is scalar API here  for image scaling libraries and in this post define these libraries are easy to use and there is no need to implement large set of coding or mathematical computations . but if you want to customize something else or perform some action that are not present in this libraries so implement our own libraries . following describe "how to use and create image into thumbnails with image scalar libraries" .. 

Requirements:

  • Download and install JDK
  • Download the imgscalr-lib-4.2.jar or inject dependency
<dependencies>
 ...
 <dependency>
  <groupId>org.imgscalr</groupId>
  <artifactId>imgscalr-lib</artifactId>
  <version>4.2</version>
  <type>jar</type
  <scope>compile</scope>
 </dependency>
 ...
<dependencies>

Following is the example how to use : 

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;

class ImageThumbnail{
     private void createThumbnail() throws FileNotFoundException,IOException{
          File file = new File("1.jpg");
          BufferedImage img = ImageIO.read(file);
          BufferedImage thumb = Scalr.resize(img, Scalr.Method.SPEED,102, 102, Scalr.OP_ANTIALIAS);
          ImageIO.write(thumb, "png", new File("thumb.png"));
     }

     public static void main(String[] args) throws FileNotFoundException,IOException{
          new ImageThumbnail().createThumbnail();
     }
}

Download the Source Code From this link