'Android Virtual Assistant not showing answer as required
package proyectos.carosdrean.xyz.asistente;
import android.content.Intent;
import android.os.Build;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.Normalizer;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final int RECONOCEDOR_VOZ = 7;
private TextView escuchando;
private TextView respuesta;
private ArrayList<Respuestas> respuest;
private TextToSpeech leer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inicializar();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == RECONOCEDOR_VOZ){
ArrayList<String> reconocido = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String escuchado = reconocido.get(0);
escuchando.setText(escuchado);
prepararRespuesta(escuchado);
}
}
private void prepararRespuesta(String escuchado) {
String normalizar = Normalizer.normalize(escuchado, Normalizer.Form.NFD);
String sintilde = normalizar.replaceAll("[^\\p{ASCII}]", "");
int resultado;
String respuesta = respuest.get(0).getRespuestas();
for (int i = 0; i < respuest.size(); i++) {
resultado = sintilde.toLowerCase().indexOf(respuest.get(i).getCuestion());
if(resultado != -1){
respuesta = respuest.get(i).getRespuestas();
if(operacion(respuest.get(i).getCuestion(), sintilde) != ""){
respuesta = respuesta + operacion(respuest.get(i).getCuestion(), sintilde);
}
}
}
responder(respuesta);
}
private String operacion(String cuestion, String escuchado){
String rpta = "";
if(cuestion.equals("plus") || cuestion.equals("menos") || cuestion.equals("por") || cuestion.equals("entre")){
rpta = operaciones(cuestion,escuchado);
}
return rpta;
}
private String operaciones(String operador, String numeros){
String rpta = "";
double respuesta = 0;
String errorDivision = "";
String[] numero = numeros.split(operador);
double num1 = obtenerNumero(numero[0]);
double num2 = obtenerNumero(numero[1]);
switch(operador){
case "+":
respuesta = num1 + num2;
break;
case "menos":
respuesta = num1 - num2;
break;
case "por":
respuesta = num1 * num2;
break;
case "entre":
if(num1 > 0)
{
respuesta = num1 / num2;
}
else
{
errorDivision = "ERROR: El primer numero no puede ser menos a 0";
}
break;
}
rpta = String.valueOf(respuesta);
return rpta;
}
private double obtenerNumero(String cadena)
{
double num;
String n = "";
char[] numero = cadena.toCharArray();
for (int i = 0; i < numero.length; i++){
if (Character.isDigit(numero[i])){
n = n + String.valueOf(numero[i]);
}
}
num = Double.parseDouble(n);
return num;
}
private void responder(String respuestita) {
respuesta.setText(respuestita);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
leer.speak(respuestita, TextToSpeech.QUEUE_FLUSH, null, null);
}else {
leer.speak(respuestita, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void inicializar(){
escuchando = (TextView)findViewById(R.id.tvEscuchado);
respuesta = (TextView)findViewById(R.id.tvRespuesta);
respuest = proveerDatos();
leer = new TextToSpeech(this, this);
}
public void hablar(View v){
Intent hablar = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
hablar.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-MX");
startActivityForResult(hablar, RECONOCEDOR_VOZ);
}
public ArrayList<Respuestas> proveerDatos(){
ArrayList<Respuestas> respuestas = new ArrayList<>();
respuestas.add(new Respuestas("defecto", "¡Aun no estoy programada para responder eso, lo siento!"));
respuestas.add(new Respuestas("hola", "hola que tal"));
respuestas.add(new Respuestas("chiste", "¿Sabes que mi hermano anda en bicicleta desde los 4 años? Mmm, ya debe estar lejos"));
respuestas.add(new Respuestas("adios", "que descanses"));
respuestas.add(new Respuestas("como estas", "esperando serte de ayuda"));
respuestas.add(new Respuestas("nombre", "mis amigos me llaman Mina"));
respuestas.add(new Respuestas("plus ", "la respuesta de la suma es " ));
respuestas.add(new Respuestas("+", "la respuesta de la suma es"));
respuestas.add(new Respuestas("menos ", "la respuesta de la resta es "));
respuestas.add(new Respuestas("por ", "la respuesta de la multiplicacion es "));
respuestas.add(new Respuestas("entre ", "la respuesta de la division es "));
return respuestas;
}
@Override
public void onInit(int status) {
}
}
I am trying to make this work. The assistant works I have implemented a calculations function where I can make basic sums divisions multiplications but it does not show the answer even though in the method preprarRespuesta I have included
- operacion. What that method and last if statement does is put together the answer from the array list at the bottom of the class with a string number that is generated using the other methods. But even after that is not displaying the number and I don´t get why. I have tried everything. Help appreciated Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
