1
0
forked from GitHub/gf-core

now the database with glosses is compiled on the PC by running glosses.hs. On the phone we just extract the database

This commit is contained in:
krasimir
2017-04-19 13:00:46 +00:00
parent 5c93f2fba5
commit 25e14b253d
5 changed files with 60 additions and 33 deletions

View File

@@ -1,10 +1,21 @@
import SG import SG
import PGF2
import Data.Char import Data.Char
import Data.List import Data.List
main = do main = do
db <- openSG "assets/semantics.db"
inTransaction db $ do
ls <- fmap lines $ readFile "../../../lib/src/translator/Dictionary.gf" ls <- fmap lines $ readFile "../../../lib/src/translator/Dictionary.gf"
writeFile "assets/glosses.txt" (unlines [x | Just (fn,gloss) <- map gloss ls, x <- glossTriples fn gloss]) let glosses = [x | Just (fn,gloss) <- map gloss ls, x <- glossTriples fn gloss]
topics <- fmap (map toTriple . lines) $ readFile "topics.txt"
sequence_ [insertTriple db s p o | (s,p,o) <- glosses++topics]
closeSG db
toTriple l =
case readTriple l of
Just t -> t
Nothing -> error ("topics.txt: "++l)
gloss l = gloss l =
case words l of case words l of
@@ -14,9 +25,12 @@ gloss l =
_ -> Nothing _ -> Nothing
glossTriples fn s = glossTriples fn s =
(if null gs then [] else ["<"++fn++",gloss,"++show (merge gs)++">"])++ (if null gs then [] else [(fn_e,gloss,mkStr (merge gs))])++
(if null es then [] else ["<"++fn++",example,"++show (merge (map (init . tail) es))++">"]) (if null es then [] else [(fn_e,example,mkStr (merge (map (init . tail) es)))])
where where
fn_e = mkApp fn []
gloss = mkApp "gloss" []
example = mkApp "example" []
(es,gs) = partition isExample (splitGloss s) (es,gs) = partition isExample (splitGloss s)
splitGloss s = splitGloss s =

View File

@@ -2,6 +2,7 @@ package org.grammaticalframework.ui.android;
import java.io.*; import java.io.*;
import android.content.Context; import android.content.Context;
import android.util.Log;
import org.grammaticalframework.sg.*; import org.grammaticalframework.sg.*;
import org.grammaticalframework.pgf.*; import org.grammaticalframework.pgf.*;
@@ -10,8 +11,6 @@ public class SemanticGraphManager implements Closeable {
private final Context mContext; private final Context mContext;
private SG mDB; private SG mDB;
public static final String GLOSSES_FILE_NAME = "glosses.txt";
public static final String TOPICS_FILE_NAME = "topics.txt";
public static final String DATABASE_FILE_NAME = "semantics.db"; public static final String DATABASE_FILE_NAME = "semantics.db";
public SemanticGraphManager(Context context) { public SemanticGraphManager(Context context) {
@@ -41,37 +40,50 @@ public class SemanticGraphManager implements Closeable {
dir.mkdir(); dir.mkdir();
} }
if (!extractDatabase(file))
return;
mDB = SG.openSG(path); mDB = SG.openSG(path);
if (exists) if (exists)
return; return;
loadFile(GLOSSES_FILE_NAME);
loadFile(TOPICS_FILE_NAME);
} }
private void loadFile(String assetName) throws IOException { private boolean extractDatabase(File outFile) {
BufferedReader br = new BufferedReader( InputStream in = null;
new InputStreamReader( OutputStream out = null;
mContext.getAssets().open(assetName)));
try { try {
mDB.beginTrans(); in = mContext.getAssets().open(DATABASE_FILE_NAME);
out = new FileOutputStream(outFile);
String line; copyFile(in, out);
while ((line = br.readLine()) != null) { return true;
Expr[] triple = SG.readTriple(line); } catch(IOException e) {
mDB.insertTriple(triple[0],triple[1],triple[2]); Log.e("tag", "Failed to copy asset file: " + DATABASE_FILE_NAME, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
} }
mDB.commit(); return false;
} catch (IOException e) { }
mDB.rollback();
throw e; private void copyFile(InputStream in, OutputStream out) throws IOException {
} catch (SGError e) { byte[] buffer = new byte[1024];
mDB.rollback(); int read;
throw e; while((read = in.read(buffer)) != -1){
} finally { out.write(buffer, 0, read);
br.close();
} }
} }

View File

@@ -1,6 +1,7 @@
package se.chalmers.phrasebook.backend.syntax; package se.chalmers.phrasebook.backend.syntax;
import java.io.Serializable; import java.io.Serializable;
import org.grammaticalframework.pgf.Expr;
public class SyntaxNode implements Serializable { public class SyntaxNode implements Serializable {
private String desc; private String desc;