Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert posterior probability using logmath_exp. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/pocketsphinx/api/pocketsphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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)
Expand All @@ -23,6 +24,9 @@ def self.ps_init(*args)
attach_function :ps_get_in_speech, [:decoder], :uint8
attach_function :ps_get_hyp, [:decoder, :pointer], :string
attach_function :ps_get_prob, [:decoder], :int32
attach_function :ps_get_logmath, [:decoder], :logmath
attach_function :logmath_get_base, [:logmath], FFI::NativeType::FLOAT64
attach_function :logmath_exp, [:logmath, :int], FFI::NativeType::FLOAT64
attach_function :ps_set_jsgf_string, [:decoder, :string, :string], :int
attach_function :ps_unset_search, [:decoder, :string], :int
attach_function :ps_get_search, [:decoder], :string
Expand Down
3 changes: 2 additions & 1 deletion lib/pocketsphinx/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ def in_speech?
# @return [Hypothesis] Hypothesis (behaves like a string)
def hypothesis
mp_path_score = FFI::MemoryPointer.new(:int32, 1)
logmath = ps_api.ps_get_logmath(ps_decoder)

hypothesis = ps_api.ps_get_hyp(ps_decoder, mp_path_score)
posterior_prob = ps_api.ps_get_prob(ps_decoder)
posterior_prob = ps_api.logmath_exp(logmath, mp_path_score.get_int32(0))

hypothesis.nil? ? nil : Hypothesis.new(
hypothesis,
Expand Down
11 changes: 7 additions & 4 deletions spec/decoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@

describe '#hypothesis' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_get_logmath)

expect(ps_api)
.to receive(:ps_get_hyp) do |ps_decoder, mp_path_score|
expect(ps_decoder).to eq(subject.ps_decoder)
Expand All @@ -153,10 +156,10 @@
end

expect(ps_api)
.to receive(:ps_get_prob) do |ps_decoder|
expect(ps_decoder).to eq(subject.ps_decoder)
1
end
.to receive(:logmath_exp) do |logmath, mp_path_score_int|
expect(mp_path_score_int).to be_a(Integer)
1
end

hypothesis = subject.hypothesis

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/decoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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.posterior_prob).to eq(0.4659446069409511)
end

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