Skip to content

Commit

Permalink
Aldonis ekranon kie oni povas elprovi la parolrekonon.
Browse files Browse the repository at this point in the history
Forĵetis la ekranon kiu devis funcii kiel 'klavaro'/eniga metodo - ĝi estis tute fuŝa kaj ne funkciis
  • Loading branch information
nordfalk committed Jul 8, 2022
1 parent 2762a6c commit 56963fa
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 336 deletions.
67 changes: 4 additions & 63 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,15 @@
android:launchMode="standard"
android:theme="@style/AppTheme">

<!-- todo: make work - see K6nele
<activity
android:name="dk.nordfalk.esperanto.parolrekono.SpeechActivity"
android:theme="@style/Theme.LocalSTT.Translucent">
<intent-filter>
<action android:name="android.speech.action.RECOGNIZE_SPEECH" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.speech.action.WEB_SEARCH" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<!-- input/output: Nothing -->
<!-- Samsung Galaxy SII launches VOICE_COMMAND when HOME is double-clicked -->
<intent-filter>
<action android:name="android.intent.action.VOICE_COMMAND" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--
Action ASSIST (API 16)
Input: EXTRA_ASSIST_PACKAGE, EXTRA_ASSIST_CONTEXT. Output: nothing.
* "Search assistant" on CM10.2, which can be mapped to various buttons.
* Long press on the HOME button on Nexus 5X.
* Upper physical button on Huawei Watch 2.
The default ASSIST app is user-configurable on the phone but not on Wear,
i.e. on the phone the user can choose which app is started, e.g. when long-pressing
on the HOME button, and the filter priority plays no role. On Wear the choice
is based only on the priority.
We set it to lower than default to let the other guy win. This is probably
specific to Huawei Watch 2 with its weird setup,
where the upper button launches ASSIST (and this cannot be
changed) and the lower button can open any app (other than Google Assist).
-->
<intent-filter android:priority="-10">
<action android:name="android.intent.action.ASSIST" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- input/output: Nothing -->
<!-- API 3 -->
<!-- "Voice search" on CM10.2, which can be mapped to various buttons -->
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<!-- input/output: Nothing -->
<intent-filter>
<action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- TODO: future work
<intent-filter>
<action android:name="android.provider.MediaStore.RECORD_SOUND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
-->
</activity>
-->
<activity
android:name="dk.nordfalk.esperanto.parolrekono.MainActivity">
android:name="dk.nordfalk.esperanto.parolrekono.MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/cat/oreilly/localstt/VoskRecognitionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private void setupRecognizer() {
Log.i(TAG, "Creating recognizer");

recognizer = new Recognizer(model, 16000.0f);
//recognizer.setMaxAlternatives(3);
}

if (speechService == null) {
Expand Down Expand Up @@ -184,7 +185,26 @@ private void error(int errorCode) {
@Override
public void onResult(String hypothesis) {
if (hypothesis != null) {
Log.i(TAG, hypothesis);
Log.i(TAG, "onResult: " + hypothesis);
/*
onResult: {
"text" : "oni diru ĉu la rezulto estos pli bone aŭ ne"
}
//recognizer.setMaxAlternatives(3);
onResult: {
"alternatives" : [{
"confidence" : 329.542145,
"text" : " nun ni vidu ĉu venas pli ol unu se la rezulto aŭ ne"
}, {
"confidence" : 329.418335,
"text" : " nu ni vidu ĉu venas pli ol unu se la rezulto aŭ ne"
}, {
"confidence" : 328.344757,
"text" : " nun ni diru ĉu venas pli ol unu se la rezulto aŭ ne"
}]
}
*/
Gson gson = new Gson();
Map<String, String> map = gson.fromJson(hypothesis, Map.class);
String text = map.get("text");
Expand All @@ -195,7 +215,7 @@ public void onResult(String hypothesis) {
@Override
public void onFinalResult(String hypothesis) {
if (hypothesis != null) {
Log.i(TAG, hypothesis);
Log.i(TAG, "onFinalResult: " + hypothesis);
Gson gson = new Gson();
Map<String, String> map = gson.fromJson(hypothesis, Map.class);
String text = map.get("text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dk.nordfalk.esperanto.parolrekono;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
Expand All @@ -24,7 +25,9 @@
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -36,13 +39,16 @@
import java.util.Locale;

import cat.oreilly.localstt.R;
import cat.oreilly.localstt.VoskRecognitionService;

public class MainActivity extends AppCompatActivity {
protected static final String TAG = MainActivity.class.getSimpleName();

public static final Integer RecordAudioRequestCode = 1;
private SpeechRecognizer speechRecognizer;
private EditText editText;
private Button button;
private TextView resultsTextView;

protected void toast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
Expand All @@ -51,15 +57,32 @@ protected void toast(String message) {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speech_activity);
setContentView(R.layout.main_activity);

editText = findViewById(R.id.editText);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
button = findViewById(R.id.button);
resultsTextView = findViewById(R.id.resultsTextView);

// new ComponentName("dk.nordfalk.esperanto.parolrekono", "cat.oreilly.localstt.VoskRecognitionService"));
ComponentName serviceComponent = new ComponentName(getPackageName(), VoskRecognitionService.class.getName());
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, serviceComponent);

button.setOnClickListener(v -> {
//final Locale esperanto = Locale.forLanguageTag("eo");
//speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, esperanto);
//speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Parolu nun");
// speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
speechRecognizer.startListening(speechRecognizerIntent);
});


speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
Log.i(TAG, "onReadyForSpeech "+bundle);
editText.setHint("onReadyForSpeech");
}

@Override
Expand Down Expand Up @@ -95,6 +118,7 @@ public void onResults(Bundle bundle) {
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.i(TAG, results.toString());
editText.setText(results.get(0));
resultsTextView.setText(String.join("\n", results));
toast(String.format(getString(R.string.recognized), results.get(0)));
}

Expand All @@ -104,6 +128,7 @@ public void onPartialResults(Bundle bundle) {
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.i(TAG, results.toString());
editText.setText(results.get(0));
resultsTextView.setText(String.join("\n", results));
}

@Override
Expand All @@ -121,11 +146,6 @@ public void onStart() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
checkPermission();
}
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Parolu nun");
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.startListening(speechRecognizerIntent);
}

@Override
Expand Down
Loading

0 comments on commit 56963fa

Please sign in to comment.