Android speech recognizer vs OK GOOGLE - android

In my application I'm using SpeechRecognizer, in order to detect what the user said.
My device's language is set to English and it works perfect when I say something in English, but when I say something in other languages, for example - Hebrew, it doesn't work all the time as it works for English, until I set the device's language to Hebrew and then it works OK.
I'm trying to avoid from setting the device's language and want it to automatically detect the user language.
I've noticed that "OK Google" works and detect the correct words in Hebrew even when the device's language is set to English.
For the meantime, what I tried to do is when the user enter for the first time
to my application, I'm asking him to enter his country.
Then when I have his country -> I'm getting the country code and then I create a Locale using the country code.
This locale is then sent as the language to the speech recognizer.
But it didn't help..
// example of how to get the locale using the country code
Locale myLocale = null;
String toSearch = "IL";
toSearch = toSearch.trim().toLowerCase();
for (Locale locale : Locale.getAvailableLocales())
{
if(locale.getCountry().trim().toLowerCase().contains(toSearch))
{
myLocale = locale;
break;
}
}
// example of how I'm sending the locale
Intent recIntent= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recIntent.putExtra(RecognizerIntent.ExtraLanguage,myLocale);

Related

How to get system's Locale (not application's) in android

I am writing an app that has an option menu, with three options for language : automatic, english , greek.
When the user changes the setting, I save it in shared preferences, and call my starting activity with the new task flag
when that activity starts (before even setContentView) I call the following method:
string language = MainApplication.Language; //this is from shared preferences
if (language == null || language.Equals("auto")) {
language = Java.Util.Locale.Default.Language; //if language is not set, or the setting is auto then load the default language
MainApplication.Language = "auto"; //save into the shared preferences that the language was set to auto
}
App.instance.set_language(language); //update the data layer's language
Java.Util.Locale locale = new Java.Util.Locale(language);
Java.Util.Locale.Default = locale; //change the default locale
Configuration config = new Configuration();//create a new configuration and set its locale
config.Locale = locale;
Application.Context.Resources.UpdateConfiguration(config, null);//update the system locale
The above is written in Xamarin, so it's C# but it's the same code for java
My problem is that if I change the language to greek for example, when the code above runs it sets the default locale to "el" , but if the user selects the automatic option after that , because the default locale for the application is "el" it preserves that setting, even though my system language is english.
when I restart the application it works correctly because the application's default locale is that of the system
so my question is, is there a way to get the system's locale and not the applications?
p.s. I know that I can always store the system's locale in Application and change it in OnConfigurationChanged ,I was just wandering if there is another way
You can get it by adb shell getprop | grep locale
It will print similar output like:
[persist.sys.locale]: [tr-TR]
[ro.product.locale]: [tr-TR]
So you can run a process as:
Process process = Runtime.getRunTime().exec("getprop");
and check the output string with a BufferedReader.
or basically check this answer.

Text to speech returns a different non-existant Locale after setting an existing one

original question
I have a standard texttospeech, android.speech.tts.TextToSpeech
I initialize it and set a language by using tts.setLanguage(Locale.getDefault())
That default Locale is de_DE (for germany, correct).
Right after setting it, i ask the tts to give me its language tts.getLanguage()
now it tells me that its set to "deu_DEU"
There is no Locale with that setting. So i cant even check if its set to the right language because i cant find the Locale object that has the matching values.
Issue might be related to Android 4.3, but i didnt find any info.
Background is, that i need to show values with the same decimal symbol, but tts needs the correct symbol or it says "dot" in german which makes NO sense at all.
Conclusion:
A Locale is a container that contains a string that is composed of a language, a country and an optional string. Every text-to-speech engine can return a custom Locale like "eng_USA_texas".
Furthermore the Locale that is returned by the tts engine can only be a "close match" to the wanted Locale. So "en_US" instead of "en_UK".
However, Locale has a method called getLanguage() and it returns the first part of above mentioned string. "en" or "eng". Those Language codes are regulated by ISO and one can hope that everyone sticks to it. (see link in the accepted answer)
So checking for tts.getLanguage().getLanguage().startsWith("en") should always be true if its some form of english language setting and the ISO standards are fulfilled.
It is important to mention that Locales should not be compared by locale_a == locale_b as both can be different objects yet have the same content, they are containers of sort.
Always compare with locale_a.equals(locale_b)
I hope this helps people sort out some problems with tts and language
You're right, it's frustrating how the locale codes the TTS object uses are different to those of the device locale. I don't understand why this decision was made.
To add further complication, the TTS Engine can supply all kinds of different locales, such as eng_US_sarah or en-US-female etc. It's down to the TTS Engine how these are stored and displayed.
I've had to write additional code to iterate through the returned locales and attempt to match them to the locale the system can use, or vica-versa.
To start with, take a look at how the engines you have installed are returning their locale information. You can then start to collate in your code a list to associate 'deu_DEU' to 'de_De'.
This is often simplistic by using split("_") & startsWith(String), but unfortunately not for all locales.
Here's some base code I've used to analyse the installed TTS Engines' locale structure.
private void getEngines() {
final Intent ttsIntent = new Intent();
ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
final PackageManager pm = getActivity().getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(ttsIntent, PackageManager.GET_META_DATA);
final ArrayList<Intent> intentArray = new ArrayList<Intent>(list.size());
for (int i = 0; i < list.size(); i++) {
final Intent getIntent = new Intent();
getIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
getIntent.setPackage(list.get(i).activityInfo.applicationInfo.packageName);
getIntent.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
intentArray.add(getIntent);
}
for (int i = 0; i < intentArray.size(); i++) {
startActivityForResult(intentArray.get(i), i);
}
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
try {
if (data != null) {
System.out.print(data.getStringArrayListExtra("availableVoices").toString());
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
From the above ISO-3 codes and the device locale format, you should be able to come up with something for the locales you are concerned with.
I've been intending to submit an enhancement request to AOSP for a while, as all TTS Engines need to use constant values and extras such as gender etc need to be added to use the TTS Engines to their full capabilities.
EDIT: Further to your edit, note the wording regarding setLanguage(). The individual TTS Engine will try and match as close as possible to the requested locale, but that applied locale may be completely wrong, depending on how lenient the Engine provider is in their code and their response.
After creating an object of TextToSpeech class, you should configure it (or check it's available state/values) into TextToSpeech.OnInitListener's onInit() callback. You will get reliable information there about your TextToSpeech object.
Check my answer here:
https://stackoverflow.com/a/65620221/7835969

Android - Hebrew locale doesn't work in Samsung S1

I'm need to show date in dd/MM/yyyy format (full month name) in Hebrew.
In Samsung s1 - it shows me English locale and in Motorola Atrix 4G it works fine
(shows the month name in Hebrew).
I'm using:
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, getLocale());
// I TRIED ALL THESE 3 LOCALES
private Locale getLocale() {
return new Locale("he");
//return new Locale("iw_IL");
//return new Locale("iw");
}
Note: I need to show Hebrew even if the phone defalt locale is different.
How can I implement for all devices (that has hebrew)?
Per Google, en_US ... is the only locale Java guarantees is always available.
If you know you're going to need Hebrew month names regardless of what's provided by the underlying platform, you should include that data in your app.

How to use more then one language in android speech recognition at the same time

All we know that many languages includes several words from other languages. While using android speech recognition (Androdi4.1) to develop an android application, we need that app process the speech input with two languages (EN and TR) at the same time (or asynch) and bring us both of the results.
English word "Burger" in Turkish pronunciation is "Börgır" .
If we use android speech recognition in english, application understands Burger as Burger
(on text) .
If we use android speech recognition in turkish, application understands Burger as Börgır(on text)
This is because Turkish language is written as spoken.
For example:
speech recognition result with Turkish option: Börgır yemek istiyorum
speech recognition result with English option: Burger xxxxx yyyyyyyyy
Expected result is(with combined result) : Burger yemek istiyorum
So do you know how to process two languages at the same time , with android speech recognition to collect both results, after that we will combine them in order to get best results.
Below code is used for implementing android speech recognition within application:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "tr"); --- or eng-us
This can't be done using the current Android APIs.
You could try to run two instances of SpeechRecognizer at the same time (one with a Turkish EXTRA_LANGUAGE, the other with English). This will probably not work because different services cannot use the microphone at the same time.
You could try to implement the recording part yourself and then push the resulting byte array to two different recognizers. But there is no API for that.
I solved it like that:
language = "tr-TR";
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, language);
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, language);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, language);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS, language);
startActivityForResult(intent, REQUEST_CODE);
I suggest that you pick one language for the speech recognizer like english. Then when you speak your other language, see what the speech recognizer comes up with. You may be able to interpret the english recognition of that.
For example, if you want to speak spanish to the english recognizer and the user says "si" in spanish, you might notice the recognizer recognizes that as "see"

Load all languages in Android

I have several language files in Android application: value/strings.xml, value-en/strings.xml, ...etc
It is possible to load the content of this files in some arrays or something. For example, I would like to load default text strings and english strings in 2 different arrays at run time.
Thanks
Alin
Create a method like this:
Resources getResourcesByLocale( Context context, String localeName ) {
Resource res = context.getResources();
Configuration conf = new Configuration(res.getConfiguration());
conf.locale = new Locale(localeName);
return new Resources(res.getAssets(), res.getDisplayMetrics(), conf);
}
Than you can get resources for any locale you've defined, for example:
Resources res_en = getResourcesByLocale(context, "en");
Resources res_de = getResourcesByLocale(context, "de");
Resources res_fr = getResourcesByLocale(context, "fr");
String some_name_en = res_en.getString(R.string.some_name);
String some_name_fr = res_fr.getString(R.string.some_name);
// etc...
Moreover, you do not need to take care about exceptions if you did not define string for some locale, because anyway default (from res/values/*) will be loaded instead.
Actually the situation is like that. Imagine I have this scenario: some chinese open the application. He has the mobile phone set with ch locale. The application default is xx as language, meaning I have 2 language files values/strings.xml (spanish for eg as default) and another language values-en/strings.xml for english. The default will make no sense for him, so english will be the most appropiate for his understanding, even if he does not understand it very good. So at the app start I open language settings (android language settings), where any selection will set the app in spanish unless he select english. I am forcing him to change the phone locale in english basically, just to use my app. Overall the concept of android is wrong, because i need to be able to see an application in any language I want without changing device language.
What I have done: - I created in values folder one more string_xx.xml file. Now, for a translation string name = "txtTranslation" I have in string_xx file "en_txtTranslation" key. R.java loads them all and in my app, based on a global var selectedLanguage = xx, I attach the write string using this code:
public String translate(String text)
{
String appLanguage = UtilsCentral.getUserLanguage(getApplicationContext());
if (appLanguage != "")
{
return getString(getResources().getIdentifier(appLanguage + "_" + text, "string", this.getPackageName()));
}
else
{
return getString(getResources().getIdentifier(text, "string", this.getPackageName()));
}
}
Indeed, at activity on create i need to set all views with .text = tarnslate("txtTranslation")
Note: UtilsCentral.getUserLanguage(getApplicationContext()) returns app language (user selection)
Conclusion, there is more unuseful work, but lets me do what i need, and what i believe is normal.

Categories

Resources