Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Created July 10, 2012 08:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otobrglez/3082051 to your computer and use it in GitHub Desktop.
Save otobrglez/3082051 to your computer and use it in GitHub Desktop.
Sentence detection with OpenNLP and jRuby
# By Oto Brglez - <oto.brglez@opalab.com>
# Read:
# http://opennlp.apache.org/documentation/1.5.2-incubating/apidocs/opennlp-tools/index.html
# http://opennlp.apache.org/documentation/1.5.2-incubating/manual/opennlp.html#tools.sentdetect.detection
require 'bundler/setup'
require 'ruby-debug'
require 'pp'
# Java & jRuby
require 'java'
Dir["apache-opennlp/lib/*"].each { |jar| require jar }
# OpenNLP
include_class "opennlp.tools.util.Span"
include_class "opennlp.tools.sentdetect.SentenceModel"
include_class "opennlp.tools.sentdetect.SentenceDetectorME"
begin
stream = java.io.FileInputStream.new("apache-opennlp/nlp-models/en-sent.bin")
model = SentenceModel.new(stream)
detector = SentenceDetectorME.new(model)
sentences = detector.sentDetect("Today is a nice day. I love the day. My name is Oto and I'm from Maribor.")
sentences.each do |s|
puts s
end
rescue Exception => e
puts e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment