working on semantic grammar integration

parent 0273c0ab
......@@ -18,7 +18,7 @@ package com.yottacode.pictogrammar;
import java.util.LinkedList;
import java.util.Vector;
//import org.grammaticalframework.PGF;
import org.grammaticalframework.PGF;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -38,7 +38,7 @@ package com.yottacode.pictogrammar;
public class Translate extends Activity implements iVocabularyListener, iImgDownloaderListener
{
private ArrayAdapter mArrayAdapter;
//private PGF mPGF;
private PGF mPGF;
private TTSHelper tts;
LinkedList<Picto> pictos;
......@@ -215,7 +215,7 @@ public class Translate extends Activity implements iVocabularyListener, iImgDown
/**
* This class is used to load the PGF file asychronously.
* It display a blocking progress dialog while doing so.
*//*
*/
private class LoadPGFTask extends AsyncTask<Void, Void, PGF> {
private ProgressDialog progress;
......@@ -238,12 +238,12 @@ public class Translate extends Activity implements iVocabularyListener, iImgDown
}
protected void onPostExecute(PGF result) {
//mPGF = result;
mPGF = result;
if (this.progress != null)
this.progress.dismiss(); // Remove loading popup
}
}
*/
/**
* This class is used to parse a sentence asychronously.
* It display a blocking progress dialog while doing so.
......@@ -262,13 +262,13 @@ public class Translate extends Activity implements iVocabularyListener, iImgDown
try {
// Creating a Parser object for the FoodEng concrete grammar
tts.speakText(s[0]);
// Parser mParser = new Parser(mPGF, "SUpO_ES");
Parser mParser = new Parser(mPGF, "SUpO_ES");
// Spliting the input (basic tokenization)
// String[] tokens = s[0].split(" ");
// parsing the tokens
// ParseState mParseState = mParser.parse(tokens);
//Tree[] trees = (Tree[])mParseState.getTrees();
// tokens=mParseState.predict();
String[] tokens = s[0].split(" ");
//parsing the tokens
ParseState mParseState = mParser.parse(tokens);
Tree[] trees = (Tree[])mParseState.getTrees();
tokens=mParseState.predict();
/* String[] translations = new String[trees.length];
// Creating a Linearizer object for the FoodCat concrete grammar
Linearizer mLinearizer = new Linearizer(mPGF, "SUpO_EN");
......
package com.yottacode.pictogram.grammar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.yottacode.pictogram.R;
import org.grammaticalframework.PGF;
import org.grammaticalframework.Linearizer;
import org.grammaticalframework.PGFBuilder;
import org.grammaticalframework.Parser;
import org.grammaticalframework.UnknownLanguageException;
import org.grammaticalframework.parser.ParseState;
import org.grammaticalframework.Trees.Absyn.Tree;
import java.io.InputStream;
public class SemanticGrammar
{
protected Activity activity;
private PGF mPGF;
Parser mParser;
Linearizer mLinearizer;
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
public void SemanticGrammar(Activity activity) {
this.activity=activity;
new LoadPGFTask().execute();
}
/**
* This class is used to load the PGF file asychronously.
* It display a blocking progress dialog while doing so.
*/
private class LoadPGFTask extends AsyncTask<Void, Void, PGF> {
private ProgressDialog progress;
protected void onPreExecute() {
// Display loading popup
this.progress =
ProgressDialog.show(activity, activity.getResources().getString(R.string.app_name)
,activity.getResources().getString(R.string.loadingGrammar),true);
}
protected PGF doInBackground(Void... a) {
int pgf_res = R.raw.supo;
InputStream is = activity.getResources().openRawResource(pgf_res);
try {
PGF pgf = PGFBuilder.fromInputStream(is, new String[] {activity.getString(R.string.pictogrammar),activity.getString(R.string.naturalgrammar)});
return pgf;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void onPostExecute(PGF result) {
mPGF = result;
if (this.progress != null) {
this.progress.dismiss(); // Remove loading popup
try {
mParser=new Parser(mPGF, activity.getString(R.string.pictogrammar));
mLinearizer = new Linearizer(mPGF, activity.getString(R.string.naturalgrammar));
} catch (Exception e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
Toast.makeText(activity.getApplicationContext(), activity.getResources().getString(R.string.nogrammar), Toast.LENGTH_SHORT).show();
}
}
}
protected String[] translate(String[] tokens) {
try {
//parsing the tokens
ParseState mParseState = mParser.parse(tokens);
Tree[] trees = (Tree[])mParseState.getTrees();
String[] translations = new String[trees.length];
// Linearizing all the trees
for (int i = 0 ; i < trees.length ; i++) {
try {
String t = mLinearizer.linearizeString(trees[i]);
translations[i] = t;
} catch (java.lang.Exception e) {
translations[i] = "/!\\ Linearization error";
}
}
return translations;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected String[] predict(String[] tokens) {
try {
//parsing the tokens
ParseState mParseState = mParser.parse(tokens);
return mParseState.predict();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
\ No newline at end of file
......@@ -41,4 +41,9 @@
<string name="exitingApp">Exiting the app</string>
<string name="serverError">There is a server error. Try again later</string>
<!--Semantic grammar -->
<string name="loadingGrammar">Please wait, loading semmantic grammar</string>
<string name="naturalgrammar">SUpO_EN</string>
<string name="pictogrammar">SUpO_PICTOEN</string>
<string name="nogrammar">Warning: unknown language</string>
</resources>
......@@ -44,6 +44,13 @@
<string name="exitingApp">Saliendo de la aplicación</string>
<string name="serverError">Error en el servidor de datos. Inténtelo más tarde</string>
<!--Semantic grammar -->
<string name="loadingGrammar">Por favor espere, cargando gramática semántica</string>
<string name="naturalgrammar">SUpO_ES</string>
<string name="pictogrammar">SUpO_PICTOES</string>
<string name="nogrammar">Advertencia: Lenguaje no soportado</string>
<item type="integer" name="maxInTape">8</item>
</resources>
......@@ -44,6 +44,10 @@
<string name="exitingApp">Saliendo de la aplicación</string>
<string name="serverError">Error en el servidor de datos. Inténtelo más tarde</string>
<item type="integer" name="maxInTape">8</item>
<!--Semantic grammar -->
<string name="loadingGrammar">Please wait, loading semmantic grammar</string>
<string name="grammar">SUpO_EN</string>
<string name="nogrammar">Warning: unknown language</string>
<item type="integer" name="maxInTape">8</item>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment