forked from GitHub/gf-core
ensure that the grammar is loaded only once even if the main activity is recreated
This commit is contained in:
@@ -34,7 +34,9 @@ public class MainActivity extends Activity {
|
||||
|
||||
private TTS mTts;
|
||||
|
||||
private Translator mTranslator;
|
||||
// mTranslator is static to ensure that the grammar
|
||||
// is loaded only once even if the activity has been recreated.
|
||||
private static Translator mTranslator;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -65,14 +67,9 @@ public class MainActivity extends Activity {
|
||||
|
||||
mTts = new TTS(this);
|
||||
|
||||
mTranslator = new Translator(this);
|
||||
new AsyncTask<Void,Void,Void>() {
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
mTranslator.init();
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
if (mTranslator == null) {
|
||||
mTranslator = new Translator(this);
|
||||
}
|
||||
|
||||
mSourceLanguageView.setLanguages(mTranslator.getAvailableSourceLanguages());
|
||||
mSourceLanguageView.setSelectedLanguage(mTranslator.getSourceLanguage());
|
||||
|
||||
@@ -71,10 +71,10 @@ public class Translator {
|
||||
*/
|
||||
public String translate(String input) {
|
||||
try {
|
||||
Concr sourceGrammar = getConcr(getSourceLanguage().getConcrete());
|
||||
Expr expr = sourceGrammar.parseBest("S", input);
|
||||
Concr targetGrammar = getConcr(getTargetLanguage().getConcrete());
|
||||
String output = targetGrammar.linearize(expr);
|
||||
Concr sourceLang = getConcr(getSourceLanguage().getConcrete());
|
||||
Expr expr = sourceLang.parseBest(getGrammar().getStartCat(), input);
|
||||
Concr targetLang = getConcr(getTargetLanguage().getConcrete());
|
||||
String output = targetLang.linearize(expr);
|
||||
return output;
|
||||
} catch (ParseError e) {
|
||||
Log.e(TAG, "Parse error: " + e);
|
||||
|
||||
Reference in New Issue
Block a user