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 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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -65,14 +67,9 @@ public class MainActivity extends Activity {
|
|||||||
|
|
||||||
mTts = new TTS(this);
|
mTts = new TTS(this);
|
||||||
|
|
||||||
mTranslator = new Translator(this);
|
if (mTranslator == null) {
|
||||||
new AsyncTask<Void,Void,Void>() {
|
mTranslator = new Translator(this);
|
||||||
@Override
|
}
|
||||||
protected Void doInBackground(Void... params) {
|
|
||||||
mTranslator.init();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}.execute();
|
|
||||||
|
|
||||||
mSourceLanguageView.setLanguages(mTranslator.getAvailableSourceLanguages());
|
mSourceLanguageView.setLanguages(mTranslator.getAvailableSourceLanguages());
|
||||||
mSourceLanguageView.setSelectedLanguage(mTranslator.getSourceLanguage());
|
mSourceLanguageView.setSelectedLanguage(mTranslator.getSourceLanguage());
|
||||||
|
|||||||
@@ -71,10 +71,10 @@ public class Translator {
|
|||||||
*/
|
*/
|
||||||
public String translate(String input) {
|
public String translate(String input) {
|
||||||
try {
|
try {
|
||||||
Concr sourceGrammar = getConcr(getSourceLanguage().getConcrete());
|
Concr sourceLang = getConcr(getSourceLanguage().getConcrete());
|
||||||
Expr expr = sourceGrammar.parseBest("S", input);
|
Expr expr = sourceLang.parseBest(getGrammar().getStartCat(), input);
|
||||||
Concr targetGrammar = getConcr(getTargetLanguage().getConcrete());
|
Concr targetLang = getConcr(getTargetLanguage().getConcrete());
|
||||||
String output = targetGrammar.linearize(expr);
|
String output = targetLang.linearize(expr);
|
||||||
return output;
|
return output;
|
||||||
} catch (ParseError e) {
|
} catch (ParseError e) {
|
||||||
Log.e(TAG, "Parse error: " + e);
|
Log.e(TAG, "Parse error: " + e);
|
||||||
|
|||||||
Reference in New Issue
Block a user