Skip to content

Commit

Permalink
Convert logarithmic values when calculating a hypothesis [#18]
Browse files Browse the repository at this point in the history
  • Loading branch information
watsonbox committed Aug 10, 2015
1 parent 5eb320b commit 9958a43
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/pocketsphinx/api/pocketsphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ module Pocketsphinx

typedef :pointer, :decoder
typedef :pointer, :configuration
typedef :pointer, :logmath

# Allows expect(API::Pocketsphinx).to receive(:ps_init) in JRuby specs
def self.ps_init(*args)
ps_init_private(*args)
end

attach_function :ps_get_logmath, [:decoder], :logmath
attach_function :logmath_exp, [:logmath, :int], :double

attach_function :ps_init_private, :ps_init, [:configuration], :decoder
attach_function :ps_reinit, [:decoder, :configuration], :int
attach_function :ps_default_search_args, [:pointer], :void
Expand Down
10 changes: 8 additions & 2 deletions lib/pocketsphinx/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def hypothesis

hypothesis.nil? ? nil : Hypothesis.new(
hypothesis,
mp_path_score.get_int32(0),
posterior_prob
log_prob_to_linear(mp_path_score.get_int32(0)),
log_prob_to_linear(posterior_prob)
)
end

Expand Down Expand Up @@ -211,5 +211,11 @@ def post_init_decoder
configuration.post_init_decoder(self)
end
end

# Convert logarithmic probability to linear floating point
def log_prob_to_linear(log_prob)
logmath = ps_api.ps_get_logmath(ps_decoder)
ps_api.logmath_exp(logmath, log_prob)
end
end
end
16 changes: 8 additions & 8 deletions spec/decoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
describe '#hypothesis' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_get_hyp) do |ps_decoder, mp_path_score|
.to receive(:ps_get_hyp).ordered do |ps_decoder, mp_path_score|
expect(ps_decoder).to eq(subject.ps_decoder)
expect(mp_path_score).to be_a(FFI::MemoryPointer)

Expand All @@ -152,17 +152,17 @@
"Hypothesis"
end

expect(ps_api)
.to receive(:ps_get_prob) do |ps_decoder|
expect(ps_decoder).to eq(subject.ps_decoder)
1
end
expect(ps_api).to receive(:ps_get_prob).with(subject.ps_decoder).ordered.and_return(1)
expect(ps_api).to receive(:ps_get_logmath).with(subject.ps_decoder).ordered.and_return(:logmath)
expect(ps_api).to receive(:logmath_exp).with(:logmath, 20).ordered.and_return(0.5)
expect(ps_api).to receive(:ps_get_logmath).with(subject.ps_decoder).ordered.and_return(:logmath)
expect(ps_api).to receive(:logmath_exp).with(:logmath, 1).ordered.and_return(0.4)

hypothesis = subject.hypothesis

expect(hypothesis).to eq("Hypothesis")
expect(hypothesis.path_score).to eq(20)
expect(hypothesis.posterior_prob).to eq(1)
expect(hypothesis.path_score).to eq(0.5)
expect(hypothesis.posterior_prob).to eq(0.4)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/integration/decoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
subject.decode File.open('spec/assets/audio/goforward.raw', 'rb')
expect(subject.hypothesis).to eq("go forward ten meters")

expect(subject.hypothesis.path_score).to eq(-7636)
expect(subject.hypothesis.posterior_prob).to eq(-54501)
expect(subject.hypothesis.path_score).to eq(0.4659446069409511)
expect(subject.hypothesis.posterior_prob).to eq(0.004293161767851012)
end

# FIXME: This test illustrates a current issue discussed in:
Expand Down

0 comments on commit 9958a43

Please sign in to comment.