Pregunta

Brief description of Application thus far:

The user sees a TextView that has a word. The user will click on an ImageButton that will trigger the Speech To Text API and say the word given on the TextView. If the text from the Speech To Text and text in TextView match, then the TextView will change text to, "correct!" etc.

The issue is comparing the string text values. The TextView has "hello." The SpeechToText api returns "Hello" when I say "Hello," but the text values are considered different.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main"
    android:orientation="vertical" >
    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:src="@android:drawable/ic_btn_speak_now" />
    <TextView
        android:id="@+id/inputText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/givenText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:layout_gravity="center" />
</LinearLayout>

Main.Java

package com.example.speechtotext;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    protected static final int RESULT_SPEECH = 1;
    private ImageButton btnSpeak;
    private TextView inputText;
    private TextView givenText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputText = (TextView)findViewById(R.id.inputText);
        givenText = (TextView)findViewById(R.id.givenText);
        btnSpeak = (ImageButton)findViewById(R.id.btnSpeak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                try{
                    startActivityForResult(intent, RESULT_SPEECH);
                    inputText.setText("");
                }catch(ActivityNotFoundException a) { 
                    Toast t = Toast.makeText(getApplicationContext(),
                            "your device does not support Speech to Text!",Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                inputText.setText(text.get(0));

                if(givenText.getText().toString().equals(inputText.getText().toString())){
                    givenText.setText("Correct!");
                }
                else{
                    givenText.setText("Incorrect!");
                }
            }
            break;
        }
        }
    }
}

Any Ideas?

¿Fue útil?

Solución

The problem you are facing can be solved easily by changing one line in your code, i.e:

if(givenText.getText().toString().equals(inputText.getText().toString())){

Change that to:

if(givenText.getText().toString().equalsIgnoreCase(inputText.getText().toString())){

Also, a general observation that I made when I was working on a similar app, I noticed that the Speech Recognition API isn't perfect and will not work well unless you/your user has an accent that matches the one that the API was trained on. So, in order to fix this you should consider going through the ArrayList of strings returned and checking ALL of them to see if any one of them matches the text you showed the user.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top