2013-07-23 12 views
11

È possibile determinare tramite codice quali pacchetti lingua sono attualmente installati su un dispositivo? Abbiamo provato questo:Rileva lingue installate per il riconoscimento offline

Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); 
    sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null); 

public class LanguageDetailsChecker extends BroadcastReceiver { 

    private List<String> supportedLanguages; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Bundle results = getResultExtras(true); 
     if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) 
     { 
      supportedLanguages =results.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES); 
      Log.d("TAG","languages: " + supportedLanguages); 
     } 
    } 
} 

Tuttavia, l'uscita mi mostra un sacco di lingue, mentre io ho solo en_UK, it_IT es_ES e installati. Qualche idea?

+1

A quale servizio di riconoscimento vocale testate? – ozbek

+0

Sto usando Google – JesusS

+0

Il riconoscimento vocale di Google supporta dozzine di lingue. Che cosa intendi esattamente se hai installato solo inglese, Regno Unito, Stati Uniti e spagnolo? – ozbek

risposta

2

Se si dispone di root (sorry), si può fare in questo modo:

public static boolean isOfflineDictionaryPresent(String language) { 
    if (locale == null) locale = Locale.US; 
    String dir = "/system/usr/srec/config/" + 
      language.replace('_', '.').toLowerCase(); 
    if ((new File(dir)).isDirectory()) return true; 
    return false; 
} 

Questo è stato strappato dalla Android 4.2.2 Recognizer.java source e modificato:

  • restituisce un semplice booleano invece della directory dizionario
  • prende un ingresso stringa (es. "en_US") invece di un Locale

Vorrei avere la lista completa così come sei, e scorrere attraverso di loro per verificare quali sono disponibili offline. Ho controllato la cartella /system/usr/srec/config/ su due dispositivi, ed entrambi corrispondono ai dizionari che ho installato.

Ovviamente, il lato negativo è che funziona solo per root, quindi non sono sicuro di quanto utile sarà per voi alla fine. Non sono sicuro di cosa dire per non root, non riesco a trovare nulla.


Modifica: Per curiosità, però, che cosa fa EXTRA_SUPPORTED_LANGUAGES contengono se siete offline? Se ritorna correttamente, potresti semplicemente eliminare il gestore di rete.

+0

Contiene: lingue: [af-ZA, id-ID, ms-MY, ca-ES, cs-CZ, de-DE, en-AU, it -CA, en-001, en-IN, en-NZ, en-ZA, en-GB, en-US, es-AR, es-BO, es-CL, es-CO, es-CR, es-EC , es-US, es-SV, es-ES, es-GT, es-HN, ​​es-MX, es-NI, es-PA, es-PY, es-PE, es-PR, es-DO, es -UY, es-VE, eu-ES, fil-PH, fr-FR, gl-ES, hr-HR, zu-ZA, è-IS, it-IT, hu-HU, nl-NL, nb-NO , pl-PL, pt-BR, pt-PT, ro-RO, sk-SK, fi-FI, sv-SE, vi-VN, tr-TR, bg-BG, ru-RU, sr-RS, uk -UA, lui-IL, ar-IL, ar-JO, ar-AE, ar-BH, ar-SA, ar-KW, ar-OM, ar-PS, ar-QA, ar-LB, ar-EG , ko-KR, cmn-Hans-CN, cmn-Hans-HK, cmn-Hant-TW, yue-Hant-HK, ja-JP, la] – JesusS

+0

Quali sono tutte le lingue disponibili.Contiene es-ES, en-US e en-GB, che sono le lingue che ho attualmente installato, – JesusS

+0

, quindi la falsificazione di una situazione di "nessuna rete" non funzionerebbe. Non sei sicuro di cosa dirti oltre alla soluzione di root, quindi. Forse qualcun altro troverà qualcosa. – Geobits

0

controllo questo campione:

public class SpeakingAndroid extends Activity implements OnInitListener { 

     //TTS object 
    private TextToSpeech myTTS; 
     //status check code 
    private int TS_CHECK_CODE = 0; 

     //create the Activity 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

      //check for TTS data 
      Intent checkTTSIntent = new Intent(); 
      checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
      startActivityForResult(checkTTSIntent, TS_CHECK_CODE); 
    } 

     //act on result of TTS data check 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == MY_DATA_CHECK_CODE) { 
      if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
       //the user has the necessary data - create the TTS 
      myTTS = new TextToSpeech(this, this); 
      } 
      else { 
        //no data - install it now 
       Intent installTTSIntent = new Intent(); 
       installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(installTTSIntent); 
      } 
     } 
    } 

     //setup TTS 
    public void onInit(int initStatus) { 

      //check for successful instantiation 
     if (initStatus == TextToSpeech.SUCCESS) { 
      if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) 
       myTTS.setLanguage(Locale.US); 
     } 
     else if (initStatus == TextToSpeech.ERROR) { 
      Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

è possibile ottenere tutte le lingue e di controllare ogni Lang è supportato da TTS o no? myTTS.isLanguageAvailable(Locale.US)

+4

La tua risposta riguarda il text-to-speech, non (offline) speech-to-text. – Kaarel

Problemi correlati