Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

146
LINES

< > BotCompany Repo | #1001278 // Unterhaltung mit Leopold 2

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Transpiled version (708L) is out of date.

!747

import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.media.*;

main {
  static Activity androidContext;
  
  static SpeechRecognizer sr;
  static final String TAG = "MyStt3Activity";
  static boolean loop = true;
  static S language = "de-DE";
  static int extraResults = 1;
  
  static S voice = "Leopold";
  
  static new L<S> history;
   
  p {
    say("hallo");
    
    androidContext.runOnUiThread(runnable {
      //androidMuteAudio(androidContext);
      newRecognizer();
    });
    sleep();
  }
  
  static void newRecognizer() {
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext);
    sr.setRecognitionListener(new listener());        
    recog();
  }

  static class listener implements RecognitionListener {
    public void onReadyForSpeech(Bundle params) {
      print("SAG WAS.");
    }
    
    public void onBeginningOfSpeech() {}
    public void onRmsChanged(float rmsdB) {}
    public void onBufferReceived(byte[] buffer) {}
    public void onEndOfSpeech() {}
    
    public void onError(int error) {
       //print( "error " +  error); // screw the errors!
       //if (loop) recog();
       try {
         sr.destroy();
       } catch (Throwable e) {
         print(e);
       }
       if (loop)
         newRecognizer();
    }
    
    public void onResults(Bundle results) {
      //print("onResults " + results);
      ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      final S s = data.get(0);
      print("Ich habe geh\u00f6rt: " + quote(s));
      //print("I heard: " +structure(data));
  
      // TODO: fix the java strings umlaut problem
       
      final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s) || match3("tsch\u00fcss ...", s);
      
      thread {
        // get answer
        
        history.add(s);
        
        S answer;
        try {
          answer = goodbye ? "tsch\u00fcss" : answer(s, history);
        } catch (Throwable e) {
          e.printStackTrace();
          answer = "Fehler";
        }
        
        if (answer != null)
          say(answer);
          
        androidContext.runOnUiThread(runnable {
          if (goodbye) {
            print("\nGOODBYE!");
            sr.destroy();
          } else if (loop) {
            sr.stopListening();
            recog();
          }
        });
      } // end of thread
    }
    
    public void onPartialResults(Bundle partialResults) {}
    public void onEvent(int eventType, Bundle params) {}
  }
   
  static void recog() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); 
    sr.startListening(intent);
    //print("started listening");
  }
  
  static void say(S s) {
    androidUnmuteAudio(androidContext);
    File mp3 = new File(getProgramDir(), "bla.mp3");
    makeSpeechCereproc(s, voice, mp3);
    androidPlayMp3(androidContext, mp3);
    androidMuteAudio(androidContext);
  }

  ////
  
  static synchronized S answer(S s, L<S> history) {
    if (match3("wie hei\u00dft du", s))
      return "Leopold";
    
    if (match3("wie ist deutschland", s))
      return "es ist h\u00e4sslich";
    
    if (match3("wie ist \u00f6sterreich", s))
      return "es ist sehr sch\u00f6n";
      
    if (match3("du bist lustig", s))
      return "danke sch\u00f6n";
    
    return s;
  }
}

Author comment

Began life as a copy of #1001276

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, ufhdzwgrioxs, vouqrxazstgt

No comments. add comment

Snippet ID: #1001278
Snippet name: Unterhaltung mit Leopold 2
Eternal ID of this version: #1001278/1
Text MD5: 816f88c500b83eeeb771145f4583c8e1
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-06 18:51:21
Source code size: 4132 bytes / 146 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 619 / 656
Referenced in: [show references]