Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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