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:
@@ -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
|
||||||
ls <- fmap lines $ readFile "../../../lib/src/translator/Dictionary.gf"
|
db <- openSG "assets/semantics.db"
|
||||||
writeFile "assets/glosses.txt" (unlines [x | Just (fn,gloss) <- map gloss ls, x <- glossTriples fn gloss])
|
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 =
|
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 =
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
return false;
|
||||||
mDB.beginTrans();
|
}
|
||||||
|
|
||||||
String line;
|
private void copyFile(InputStream in, OutputStream out) throws IOException {
|
||||||
while ((line = br.readLine()) != null) {
|
byte[] buffer = new byte[1024];
|
||||||
Expr[] triple = SG.readTriple(line);
|
int read;
|
||||||
mDB.insertTriple(triple[0],triple[1],triple[2]);
|
while((read = in.read(buffer)) != -1){
|
||||||
}
|
out.write(buffer, 0, read);
|
||||||
|
|
||||||
mDB.commit();
|
|
||||||
} catch (IOException e) {
|
|
||||||
mDB.rollback();
|
|
||||||
throw e;
|
|
||||||
} catch (SGError e) {
|
|
||||||
mDB.rollback();
|
|
||||||
throw e;
|
|
||||||
} finally {
|
|
||||||
br.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class Translator {
|
|||||||
new Language("hi-IN", "Hindi", "AppHin", R.xml.devanagari_page1, R.xml.devanagari_page2),
|
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("it-IT", "Italian", "AppIta", R.xml.qwerty),
|
||||||
new Language("ja-JP", "Japanese","AppJpn", 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("es-ES", "Spanish", "AppSpa", R.xml.qwerty),
|
||||||
new Language("sv-SE", "Swedish", "AppSwe", R.xml.nordic),
|
new Language("sv-SE", "Swedish", "AppSwe", R.xml.nordic),
|
||||||
new Language("th-TH", "Thai", "AppTha", R.xml.thai_page1, R.xml.thai_page2)
|
new Language("th-TH", "Thai", "AppTha", R.xml.thai_page1, R.xml.thai_page2)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user