Home | History | Annotate | Download | only in voicedialer
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.voicedialer;
     17 
     18 import android.util.Log;
     19 import android.content.Intent;
     20 import android.speech.srec.Recognizer;
     21 
     22 import java.io.IOException;
     23 import java.util.ArrayList;
     24 
     25 public class PhoneTypeChoiceRecognizerEngine extends RecognizerEngine {
     26     /**
     27      * Constructor.
     28      */
     29     public PhoneTypeChoiceRecognizerEngine() {
     30 
     31     }
     32 
     33     protected void setupGrammar() throws IOException, InterruptedException {
     34         if (mSrecGrammar == null) {
     35             if (false) Log.d(TAG, "start new Grammar");
     36             mSrecGrammar = mSrec.new Grammar(SREC_DIR + "/grammars/phone_type_choice.g2g");
     37             mSrecGrammar.setupRecognizer();
     38         }
     39     }
     40 
     41     /**
     42      * Called when recognition succeeds.  It receives a list
     43      * of results, builds a corresponding list of Intents, and
     44      * passes them to the {@link RecognizerClient}, which selects and
     45      * performs a corresponding action.
     46      * @param recognizerClient the client that will be sent the results
     47      */
     48     protected void onRecognitionSuccess(RecognizerClient recognizerClient) throws InterruptedException {
     49         if (false) Log.d(TAG, "onRecognitionSuccess " + mSrec.getResultCount());
     50 
     51         if (mLogger != null) mLogger.logNbestHeader();
     52 
     53         ArrayList<Intent> intents = new ArrayList<Intent>();
     54 
     55         for (int result = 0; result < mSrec.getResultCount() &&
     56                 intents.size() < RESULT_LIMIT; result++) {
     57 
     58             // parse the semanticMeaning string and build an Intent
     59             String conf = mSrec.getResult(result, Recognizer.KEY_CONFIDENCE);
     60             String literal = mSrec.getResult(result, Recognizer.KEY_LITERAL);
     61             String semantic = mSrec.getResult(result, Recognizer.KEY_MEANING);
     62             String msg = "conf=" + conf + " lit=" + literal + " sem=" + semantic;
     63             if (false) Log.d(TAG, msg);
     64         }
     65 
     66         // we only pay attention to the first result.
     67         if (mSrec.getResultCount() > 0) {
     68             // parse the semanticMeaning string and build an Intent
     69             String conf = mSrec.getResult(0, Recognizer.KEY_CONFIDENCE);
     70             String literal = mSrec.getResult(0, Recognizer.KEY_LITERAL);
     71             String semantic = mSrec.getResult(0, Recognizer.KEY_MEANING);
     72             String msg = "conf=" + conf + " lit=" + literal + " sem=" + semantic;
     73             if (false) Log.d(TAG, msg);
     74             if (mLogger != null) mLogger.logLine(msg);
     75 
     76             if (("H".equalsIgnoreCase(semantic)) ||
     77                 ("M".equalsIgnoreCase(semantic)) ||
     78                 ("W".equalsIgnoreCase(semantic)) ||
     79                 ("O".equalsIgnoreCase(semantic)) ||
     80                 ("R".equalsIgnoreCase(semantic)) ||
     81                 ("X".equalsIgnoreCase(semantic))) {
     82                 if (false) Log.d(TAG, " got valid response");
     83                 Intent intent = new Intent(RecognizerEngine.ACTION_RECOGNIZER_RESULT, null);
     84                 intent.putExtra(RecognizerEngine.SENTENCE_EXTRA, literal);
     85                 intent.putExtra(RecognizerEngine.SEMANTIC_EXTRA, semantic);
     86                 addIntent(intents, intent);
     87             } else {
     88                 // Anything besides yes or no is a failure.
     89             }
     90         }
     91 
     92         // log if requested
     93         if (mLogger != null) mLogger.logIntents(intents);
     94 
     95         // bail out if cancelled
     96         if (Thread.interrupted()) throw new InterruptedException();
     97 
     98         if (intents.size() == 0) {
     99             if (false) Log.d(TAG, " no intents");
    100             recognizerClient.onRecognitionFailure("No Intents generated");
    101         }
    102         else {
    103             if (false) Log.d(TAG, " success");
    104             recognizerClient.onRecognitionSuccess(
    105                     intents.toArray(new Intent[intents.size()]));
    106         }
    107     }
    108 }
    109