Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Code Block
Wiki Markup
{{

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;

public class HelloWorld {
   private POSModel model;

   public HelloWorld( InputStream data ) throws IOException {
       setModel( new POSModel( data ) );
   }

   public String[]void run( String sentence ) {
       POSTaggerME tagger = new POSTaggerME( getModel() );
       String[] words = sentence.split( "\\s+" );
    String[] tags = tagger.tag( words );
    double[] probs  return tagger.tag( words= tagger.probs();

    for( int i = 0; i < tags.length; i++ ) {
      System.out.println( words[i] + " => " + tags[i] + " @ " + probs[i] );
    }
  }

   private void setModel( POSModel model ) {
       this.model = model;
   }

   private POSModel getModel() {
       return this.model;
   }

   public static void main( String args[] ) throws IOException {
       if( args.length < 2 ) {
           System.out.println( "TaggerTestHelloWord <file> \"sentence to tag\"" );
           return;
       }

       InputStream is = new FileInputStream( args[0] );
       HelloWorld hw = new HelloWorld( is );
       is.close();

    String tags[] =     hw.run( args[1] );

    for( int i = 0; i < tags.length; i++ ) {
      System.out.println( tags[i] );
    }
  }
}

}  }
}