Created
July 10, 2012 08:34
-
-
Save otobrglez/3082051 to your computer and use it in GitHub Desktop.
Sentence detection with OpenNLP and jRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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