Monday, October 19, 2009

OpenNLP tutorial

I had been struggling to make OpenNLP run for quite a few days. Only tutorial I found on web was danielmclaren.net. With help of it and little of my java knowledge here is introductory full fledge code to get started with OpenNLP. You have to include .jar files of opennlp , maxent and gnu trove. maxent and trove .jar files will be available in lib folder inside main folder of opennlp. While opennlp .jar file will be available in output folder of main folder opennlp.





/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package helloworld;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.maxent.*;
import java.util.*;
import gnu.trove.*;
import opennlp.tools.lang.english.*;
import java.awt.*;
/**
*
* @author amrish
*/
public class Main {

public static void main(String[] args) {


String paragraph = "Sahajanand is God. Gunatit is Akshar.";
try{
SentenceDetectorME sdetector = new SentenceDetector("/home/amrish/NLP_Softwares/opennlp-tools-1.3.0/models/sentdetect/EnglishSD.bin.gz");
String[] sents = sdetector.sentDetect(paragraph.toString());
System.out.println(sents[0] + "\n" + sents[1]);
}
catch(Exception e)
{}

}

}