Skip to content

Instantly share code, notes, and snippets.

@5t111111
Created September 24, 2014 14:45
Show Gist options
  • Save 5t111111/16ed913af0446d4359f9 to your computer and use it in GitHub Desktop.
Save 5t111111/16ed913af0446d4359f9 to your computer and use it in GitHub Desktop.
an example of using julius in ruby code
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