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.

1  
!747
2  
3  
import android.app.*;
4  
import android.content.*;
5  
import android.os.Bundle;
6  
import android.view.View;
7  
import android.view.View.OnClickListener;
8  
import android.speech.RecognitionListener;
9  
import android.speech.RecognizerIntent;
10  
import android.speech.SpeechRecognizer;
11  
import android.widget.Button;
12  
import android.widget.TextView;
13  
import android.util.Log;
14  
import android.media.*;
15  
16  
main {
17  
  static Activity androidContext;
18  
  
19  
  static SpeechRecognizer sr;
20  
  static final String TAG = "MyStt3Activity";
21  
  static boolean loop = true;
22  
  static S language = "de-DE";
23  
  static int extraResults = 1;
24  
  
25  
  static S voice = "Leopold";
26  
  
27  
  static new L<S> history;
28  
   
29  
  p {
30  
    say("hallo");
31  
    
32  
    androidContext.runOnUiThread(runnable {
33  
      //androidMuteAudio(androidContext);
34  
      newRecognizer();
35  
    });
36  
    sleep();
37  
  }
38  
  
39  
  static void newRecognizer() {
40  
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext);
41  
    sr.setRecognitionListener(new listener());        
42  
    recog();
43  
  }
44  
45  
  static class listener implements RecognitionListener {
46  
    public void onReadyForSpeech(Bundle params) {
47  
      print("SAG WAS.");
48  
    }
49  
    
50  
    public void onBeginningOfSpeech() {}
51  
    public void onRmsChanged(float rmsdB) {}
52  
    public void onBufferReceived(byte[] buffer) {}
53  
    public void onEndOfSpeech() {}
54  
    
55  
    public void onError(int error) {
56  
       //print( "error " +  error); // screw the errors!
57  
       //if (loop) recog();
58  
       try {
59  
         sr.destroy();
60  
       } catch (Throwable e) {
61  
         print(e);
62  
       }
63  
       if (loop)
64  
         newRecognizer();
65  
    }
66  
    
67  
    public void onResults(Bundle results) {
68  
      //print("onResults " + results);
69  
      ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
70  
      final S s = data.get(0);
71  
      print("Ich habe geh\u00f6rt: " + quote(s));
72  
      //print("I heard: " +structure(data));
73  
  
74  
      // TODO: fix the java strings umlaut problem
75  
       
76  
      final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s) || match3("tsch\u00fcss ...", s);
77  
      
78  
      thread {
79  
        // get answer
80  
        
81  
        history.add(s);
82  
        
83  
        S answer;
84  
        try {
85  
          answer = goodbye ? "tsch\u00fcss" : answer(s, history);
86  
        } catch (Throwable e) {
87  
          e.printStackTrace();
88  
          answer = "Fehler";
89  
        }
90  
        
91  
        if (answer != null)
92  
          say(answer);
93  
          
94  
        androidContext.runOnUiThread(runnable {
95  
          if (goodbye) {
96  
            print("\nGOODBYE!");
97  
            sr.destroy();
98  
          } else if (loop) {
99  
            sr.stopListening();
100  
            recog();
101  
          }
102  
        });
103  
      } // end of thread
104  
    }
105  
    
106  
    public void onPartialResults(Bundle partialResults) {}
107  
    public void onEvent(int eventType, Bundle params) {}
108  
  }
109  
   
110  
  static void recog() {
111  
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
112  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
113  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
114  
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
115  
116  
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); 
117  
    sr.startListening(intent);
118  
    //print("started listening");
119  
  }
120  
  
121  
  static void say(S s) {
122  
    androidUnmuteAudio(androidContext);
123  
    File mp3 = new File(getProgramDir(), "bla.mp3");
124  
    makeSpeechCereproc(s, voice, mp3);
125  
    androidPlayMp3(androidContext, mp3);
126  
    androidMuteAudio(androidContext);
127  
  }
128  
129  
  ////
130  
  
131  
  static synchronized S answer(S s, L<S> history) {
132  
    if (match3("wie hei\u00dft du", s))
133  
      return "Leopold";
134  
    
135  
    if (match3("wie ist deutschland", s))
136  
      return "es ist h\u00e4sslich";
137  
    
138  
    if (match3("wie ist \u00f6sterreich", s))
139  
      return "es ist sehr sch\u00f6n";
140  
      
141  
    if (match3("du bist lustig", s))
142  
      return "danke sch\u00f6n";
143  
    
144  
    return s;
145  
  }
146  
}

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: 621 / 660
Referenced in: [show references]