Created
September 24, 2014 14:45
-
-
Save 5t111111/16ed913af0446d4359f9 to your computer and use it in GitHub Desktop.
an example of using julius in ruby code
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
require 'socket' | |
require 'oga' | |
require 'stringio' | |
class Julius | |
def initialize(host = 'localhost', port = 10500) | |
@host = host | |
@port = port | |
end | |
def each_words | |
return false unless block_given? | |
socket = TCPSocket.new(@host, @port) | |
loop do | |
line = socket.readline(".\n") | |
io = StringIO.new(line.force_encoding('UTF-8').sub(/\.\n$/, '')) | |
parser = Oga::XML::PullParser.new(io) | |
words = '' | |
parser.parse do |node| | |
parser.on(:element) do | |
words += node.attribute('WORD').value if node.name == 'WHYPO' | |
end | |
end | |
yield words unless words.empty? | |
end | |
end | |
end | |
julius = Julius.new | |
julius.each_words do |words| | |
puts words | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment