Bug 1193183 - Correctly implement SpeechRecognitionAlternative::confidence using ps_get_prob(). r=anatal
authorKelly Davis <kdavis@mozilla.com>
Tue, 11 Aug 2015 07:03:00 -0400
changeset 258976 9734ce79206552529d8bad58e07d341315495a46
parent 258975 8a585b9dd1db7df42fe01374a7631dbd82b52122
child 258977 1cffe0166adb73f384a6883c46cbec60c64059b3
push id29268
push userryanvm@gmail.com
push dateTue, 25 Aug 2015 00:37:23 +0000
treeherdermozilla-central@08015770c9d6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersanatal
bugs1193183
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1193183 - Correctly implement SpeechRecognitionAlternative::confidence using ps_get_prob(). r=anatal
dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
--- a/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
+++ b/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp
@@ -16,30 +16,33 @@
 #include "nsIObserverService.h"
 #include "mozilla/Services.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsMemory.h"
 
 extern "C" {
 #include "pocketsphinx/pocketsphinx.h"
+#include "sphinxbase/logmath.h"
 #include "sphinxbase/sphinx_config.h"
 #include "sphinxbase/jsgf.h"
 }
 
 namespace mozilla {
 
 using namespace dom;
 
 class DecodeResultTask : public nsRunnable
 {
 public:
   DecodeResultTask(const nsString& hypstring,
+                   float64 confidence,
                    WeakPtr<dom::SpeechRecognition> recognition)
       : mResult(hypstring),
+        mConfidence(confidence),
         mRecognition(recognition),
         mWorkerThread(do_GetCurrentThread())
   {
     MOZ_ASSERT(
       !NS_IsMainThread()); // This should be running on the worker thread
   }
 
   NS_IMETHOD
@@ -54,17 +57,17 @@ public:
     SpeechRecognitionResultList* resultList =
       new SpeechRecognitionResultList(mRecognition);
     SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
     if (0 < mRecognition->MaxAlternatives()) {
       SpeechRecognitionAlternative* alternative =
         new SpeechRecognitionAlternative(mRecognition);
 
       alternative->mTranscript = mResult;
-      alternative->mConfidence = 100;
+      alternative->mConfidence = mConfidence;
 
       result->mItems.AppendElement(alternative);
     }
     resultList->mItems.AppendElement(result);
 
     event->mRecognitionResultList = resultList;
     NS_DispatchToMainThread(event);
 
@@ -72,16 +75,17 @@ public:
     // around forever... bad!
     // But thread->Shutdown must be called from the main thread, not from the
     // thread itself.
     return mWorkerThread->Shutdown();
   }
 
 private:
   nsString mResult;
+  float64 mConfidence;
   WeakPtr<dom::SpeechRecognition> mRecognition;
   nsCOMPtr<nsIThread> mWorkerThread;
 };
 
 class DecodeTask : public nsRunnable
 {
 public:
   DecodeTask(WeakPtr<dom::SpeechRecognition> recogntion,
@@ -90,33 +94,38 @@ public:
   {
   }
 
   NS_IMETHOD
   Run()
   {
     char const* hyp;
     int rv;
-    int32 score;
+    int32 final;
+    int32 logprob;
+    float64 confidence;
     nsAutoCString hypoValue;
 
     rv = ps_start_utt(mPs);
     rv = ps_process_raw(mPs, &mAudiovector[0], mAudiovector.Length(), FALSE,
                         FALSE);
 
     rv = ps_end_utt(mPs);
+    confidence = 0;
     if (rv >= 0) {
-      hyp = ps_get_hyp(mPs, &score);
-      if (hyp) {
+      hyp = ps_get_hyp_final(mPs, &final);
+      if (hyp && final) {
+        logprob = ps_get_prob(mPs);
+        confidence = logmath_exp(ps_get_logmath(mPs), logprob);
         hypoValue.Assign(hyp);
       }
     }
 
     nsCOMPtr<nsIRunnable> resultrunnable =
-      new DecodeResultTask(NS_ConvertUTF8toUTF16(hypoValue), mRecognition);
+      new DecodeResultTask(NS_ConvertUTF8toUTF16(hypoValue), confidence, mRecognition);
     return NS_DispatchToMainThread(resultrunnable);
   }
 
 private:
   WeakPtr<dom::SpeechRecognition> mRecognition;
   nsTArray<int16_t> mAudiovector;
   ps_decoder_t* mPs;
 };
@@ -149,17 +158,17 @@ PocketSphinxSpeechRecognitionService::Po
 #endif
   tmpFile->AppendRelativePath(NS_LITERAL_STRING("models"));     //
   tmpFile->AppendRelativePath(NS_LITERAL_STRING("dict"));       //
   tmpFile->AppendRelativePath(NS_LITERAL_STRING("en-US.dic")); //
   tmpFile->GetPath(aStringDictPath);
 
   // FOR B2G PATHS HARDCODED (APPEND /DATA ON THE BEGINING, FOR DESKTOP, ONLY
   // MODELS/ RELATIVE TO ROOT
-  mPSConfig = cmd_ln_init(nullptr, ps_args(), TRUE, "-hmm",
+  mPSConfig = cmd_ln_init(nullptr, ps_args(), TRUE, "-bestpath", "yes", "-hmm",
                           ToNewUTF8String(aStringAMPath), // acoustic model
                           "-dict", ToNewUTF8String(aStringDictPath), nullptr);
   if (mPSConfig == nullptr) {
     ISDecoderCreated = false;
   } else {
     mPSHandle = ps_init(mPSConfig);
     if (mPSHandle == nullptr) {
       ISDecoderCreated = false;