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 PGF2
import Data.Char
import Data.List
main = do
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])
db <- openSG "assets/semantics.db"
inTransaction db $ do
ls <- fmap lines $ readFile "../../../lib/src/translator/Dictionary.gf"
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 =
case words l of
@@ -14,9 +25,12 @@ gloss l =
_ -> Nothing
glossTriples fn s =
(if null gs then [] else ["<"++fn++",gloss,"++show (merge gs)++">"])++
(if null es then [] else ["<"++fn++",example,"++show (merge (map (init . tail) es))++">"])
(if null gs then [] else [(fn_e,gloss,mkStr (merge gs))])++
(if null es then [] else [(fn_e,example,mkStr (merge (map (init . tail) es)))])
where
fn_e = mkApp fn []
gloss = mkApp "gloss" []
example = mkApp "example" []
(es,gs) = partition isExample (splitGloss s)
splitGloss s =

View File

@@ -2,6 +2,7 @@ package org.grammaticalframework.ui.android;
import java.io.*;
import android.content.Context;
import android.util.Log;
import org.grammaticalframework.sg.*;
import org.grammaticalframework.pgf.*;
@@ -10,8 +11,6 @@ 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 TOPICS_FILE_NAME = "topics.txt";
public static final String DATABASE_FILE_NAME = "semantics.db";
public SemanticGraphManager(Context context) {
@@ -40,38 +39,51 @@ public class SemanticGraphManager implements Closeable {
if (!dir.exists()) {
dir.mkdir();
}
if (!extractDatabase(file))
return;
mDB = SG.openSG(path);
if (exists)
return;
loadFile(GLOSSES_FILE_NAME);
loadFile(TOPICS_FILE_NAME);
}
private void loadFile(String assetName) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(
mContext.getAssets().open(assetName)));
private boolean extractDatabase(File outFile) {
InputStream in = null;
OutputStream out = null;
try {
in = mContext.getAssets().open(DATABASE_FILE_NAME);
out = new FileOutputStream(outFile);
copyFile(in, out);
return true;
} catch(IOException e) {
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
}
}
}
return false;
}
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();
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}

View File

@@ -41,7 +41,7 @@ public class Translator {
new Language("hi-IN", "Hindi", "AppHin", R.xml.devanagari_page1, R.xml.devanagari_page2),
new Language("it-IT", "Italian", "AppIta", R.xml.qwerty),
new Language("ja-JP", "Japanese","AppJpn", R.xml.qwerty),
new Language("ru-RU", "Russian", "AppRus", R.xml.cyrillic),
new Language("ru-RU", "Russian", "AppRus", R.xml.cyrillic),
new Language("es-ES", "Spanish", "AppSpa", R.xml.qwerty),
new Language("sv-SE", "Swedish", "AppSwe", R.xml.nordic),
new Language("th-TH", "Thai", "AppTha", R.xml.thai_page1, R.xml.thai_page2)

View File

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