1
0
forked from GitHub/gf-core

now by default the app shows glosses for the words for which we have something. this requires one more step in the App compilation which is described in the README

This commit is contained in:
krasimir
2015-11-13 17:28:11 +00:00
parent 25906435f0
commit e7afafda3a
5 changed files with 148 additions and 104 deletions

View File

@@ -30,6 +30,9 @@ Build JNI code:
$ cd jni
$ $ANDROID_NDK_LOCATION/ndk-build
Build the semantic database code:
$ runghc glosses.hs
Build APK:

19
src/ui/android/glosses.hs Normal file
View File

@@ -0,0 +1,19 @@
import SG
main = do
ls <- fmap lines $ readFile "../../../lib/src/translator/Dictionary.gf"
writeFile "assets/glosses.txt" (unlines ["<"++fn++",gloss,"++show gloss++">" | Just (fn,gloss) <- map gloss ls])
gloss l =
case words l of
("fun":fn:_) -> case dropWhile (/='\t') l of
'\t':l -> Just (fn,l)
_ -> Nothing
_ -> Nothing
test = do
db <- openSG "semantics.db"
ls <- fmap lines $ readFile "assets/glosses.txt"
inTransaction db $
sequence_ [insertTriple db s p o | Just (s,p,o) <- map readTriple ls]
closeSG db

View File

@@ -1,92 +0,0 @@
package org.grammaticalframework.ui.android;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DBManager extends SQLiteOpenHelper {
private final Context mContext;
public DBManager(Context context) {
super(context, "defs.db", null, 1);
mContext = context;
}
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
copyDatabaseFromAssets();
return super.getReadableDatabase();
}
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
copyDatabaseFromAssets();
return super.getWritableDatabase();
}
private void copyDatabaseFromAssets() throws SQLiteException {
String path = mContext.getDatabasePath(getDatabaseName()).getPath();
File file = new File(path);
if (file.exists()) {
return;
}
InputStream ins = null;
OutputStream outs = null;
try {
File dir = new File(mContext.getApplicationInfo().dataDir + "/databases");
if (!dir.exists()) {
dir.mkdir();
}
ins = mContext.getAssets().open(getDatabaseName());
outs = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length;
while ((length = ins.read(buffer)) > 0) {
outs.write(buffer, 0, length);
}
outs.flush();
} catch (IOException e) {
SQLiteException se = new SQLiteException("Unable to write " + path + " to data directory");
se.setStackTrace(e.getStackTrace());
if (outs != null) {
try {
outs.close();
} catch (IOException e2) {
}
outs = null;
}
file.delete();
throw se;
} finally {
try {
if (outs != null)
outs.close();
if (ins != null)
ins.close();
} catch (IOException e) {
}
}
}
@Override
public final void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

View File

@@ -0,0 +1,97 @@
package org.grammaticalframework.ui.android;
import java.io.*;
import android.content.Context;
import org.grammaticalframework.sg.*;
import org.grammaticalframework.pgf.*;
public class SemanticGraphManager implements Closeable {
private final Context mContext;
private SG mDB;
public static final String GLOSSES_FILE_NAME = "glosses.txt";
public static final String DATABASE_FILE_NAME = "semantics.db";
public SemanticGraphManager(Context context) {
mContext = context;
mDB = null;
}
private void createDatabaseFromAssets() throws SGError, IOException {
if (mDB != null)
return;
String path = mContext.getDatabasePath(DATABASE_FILE_NAME).getPath();
boolean exists = false;
File file = new File(path);
if (((GFTranslator) mContext.getApplicationContext()).getTranslator().isUpgraded("db_version")) {
file.delete();
} else {
if (file.exists()) {
exists = true;
}
}
File dir = new File(mContext.getApplicationInfo().dataDir + "/databases");
if (!dir.exists()) {
dir.mkdir();
}
mDB = SG.openSG(path);
if (exists)
return;
BufferedReader br = new BufferedReader(
new InputStreamReader(
mContext.getAssets().open(GLOSSES_FILE_NAME)));
try {
mDB.beginTrans();
String line;
while ((line = br.readLine()) != null) {
Expr[] triple = SG.readTriple(line);
mDB.insertTriple(triple[0],triple[1],triple[2]);
}
mDB.commit();
} catch (IOException e) {
mDB.rollback();
throw e;
} catch (SGError e) {
mDB.rollback();
throw e;
} finally {
br.close();
}
}
public void close() {
if (mDB != null) {
mDB.close();
mDB = null;
}
}
public Expr getGloss(Expr lemma) {
Expr obj = null;
try {
createDatabaseFromAssets();
TripleResult res = mDB.queryTriple(lemma, Expr.readExpr("gloss"), null);
if (res.hasNext()) {
obj = res.getObject();
}
res.close();
} catch (IOException e) {
// nothing
} catch (SGError e) {
// nothing
}
return obj;
}
}

View File

@@ -2,6 +2,7 @@ package org.grammaticalframework.ui.android;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.util.Pair;
import android.view.inputmethod.CompletionInfo;
@@ -48,7 +49,7 @@ public class Translator {
private ConcrLoader mSourceLoader;
private ConcrLoader mTargetLoader;
private ConcrLoader mOtherLoader;
private DBManager mDBManager;
private SemanticGraphManager mSGManager;
private static final String SOURCE_LANG_KEY = "source_lang";
private static final String TARGET_LANG_KEY = "target_lang";
@@ -99,7 +100,7 @@ public class Translator {
mOtherLoader = null;
mDBManager = new DBManager(context);
mSGManager = new SemanticGraphManager(context);
}
public List<Language> getAvailableLanguages() {
@@ -366,27 +367,26 @@ public class Translator {
}
public String getInflectionTable(String lemma) {
String def = "";
/* SQLiteDatabase db = mDBManager.getReadableDatabase();
Cursor crs = db.rawQuery("select def from defs where fun=?1", new String[] { lemma });
if (crs.moveToNext()) {
def = escapeHtml(crs.getString(0));
}
crs.close();*/
Expr lemmaExpr = Expr.readExpr(lemma);
Expr gloss = mSGManager.getGloss(lemmaExpr);
Expr empty = Expr.readExpr("\"\"");
Concr targetLang = getTargetConcr();
String cat = getGrammar().getFunctionType(lemma).getCategory();
if (targetLang.hasLinearization(lemma) &&
targetLang.hasLinearization("Inflection"+cat)) {
Expr e = Expr.readExpr("MkDocument \""+def+"\" (Inflection"+cat+" "+lemma+") \"\"");
if (gloss == null)
gloss = empty;
Expr e = new Expr("MkDocument", gloss, Expr.readExpr("Inflection"+cat+" "+lemma), empty);
String html =
"<html><head><meta charset=\"UTF-8\"/></head><body>" +
targetLang.linearize(e) +
"</body>";
return html;
} else if (def != "") {
return "<p style=\"font-size:20px\">"+def+"</p>";
} else if (gloss != null) {
return "<p style=\"font-size:20px\">"+targetLang.linearize(gloss)+"</p>";
} else {
return null;
}
@@ -555,4 +555,21 @@ public class Translator {
}
}
}
public boolean isUpgraded(String key) {
int old_code = mSharedPref.getInt(key, 0);
int new_code = 0;
try {
new_code = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
// Huh? Really?
}
SharedPreferences.Editor editor = mSharedPref.edit();
editor.putInt(key, new_code);
editor.commit();
return (old_code != new_code);
}
}