diff --git a/lib/pocketsphinx/api/pocketsphinx.rb b/lib/pocketsphinx/api/pocketsphinx.rb index 1a476ed..f474da9 100644 --- a/lib/pocketsphinx/api/pocketsphinx.rb +++ b/lib/pocketsphinx/api/pocketsphinx.rb @@ -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) @@ -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 diff --git a/lib/pocketsphinx/decoder.rb b/lib/pocketsphinx/decoder.rb index aa7aa69..a3adb60 100644 --- a/lib/pocketsphinx/decoder.rb +++ b/lib/pocketsphinx/decoder.rb @@ -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, diff --git a/spec/decoder_spec.rb b/spec/decoder_spec.rb index b9eaafe..21c203d 100644 --- a/spec/decoder_spec.rb +++ b/spec/decoder_spec.rb @@ -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) @@ -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 diff --git a/spec/integration/decoder_spec.rb b/spec/integration/decoder_spec.rb index d307f41..ac0c054 100644 --- a/spec/integration/decoder_spec.rb +++ b/spec/integration/decoder_spec.rb @@ -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: