fix the error handling in the Java binding

This commit is contained in:
kr.angelov
2013-08-28 08:51:53 +00:00
parent 0efde1e2b0
commit de7fbd22e7
6 changed files with 43 additions and 15 deletions

View File

@@ -6,7 +6,7 @@ public class Test {
public static void main(String[] args) { public static void main(String[] args) {
PGF gr = null; PGF gr = null;
try { try {
gr = PGF.readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf"); gr = PGF.readPGF("Phrasebook.pgf");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
@@ -14,16 +14,22 @@ public class Test {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
System.out.println(gr.getAbstractName()); System.out.println(gr.getAbstractName());
for (Map.Entry<String,Concr> entry : gr.getLanguages().entrySet()) { for (Map.Entry<String,Concr> entry : gr.getLanguages().entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue()+" "+entry.getValue().getName()); System.out.println(entry.getKey()+" "+entry.getValue()+" "+entry.getValue().getName());
} }
Concr eng = gr.getLanguages().get("ParseEng"); Concr eng = gr.getLanguages().get("PhrasebookEng");
for (ExprProb ep : eng.parse("Phr", "where are you")) { Concr ger = gr.getLanguages().get("PhrasebookGer");
System.out.println("["+ep.getProb()+"] "+ep.getExpr());
System.out.println(eng.linearize(ep.getExpr())); try {
for (ExprProb ep : eng.parse(gr.getStartCat(), "where is the conference")) {
System.out.println("["+ep.getProb()+"] "+ep.getExpr());
System.out.println(ger.linearize(ep.getExpr()));
}
} catch (ParseError e) {
System.out.println("Parsing failed at token \""+e.getToken()+"\"");
} }
} }
} }

View File

@@ -226,6 +226,7 @@ Java_org_grammaticalframework_pgf_Parser_parse
gu_pool_free(pool); gu_pool_free(pool);
gu_pool_free(out_pool); gu_pool_free(out_pool);
return NULL;
} }
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator"); jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");

View File

@@ -6,7 +6,7 @@ public class Concr {
public native String getName(); public native String getName();
public Iterable<ExprProb> parse(String startCat, String s) { public Iterable<ExprProb> parse(String startCat, String s) throws ParseError {
return new Parser(this, startCat, s); return new Parser(this, startCat, s);
} }
@@ -25,7 +25,7 @@ public class Concr {
// private stuff // private stuff
private PGF gr; private PGF gr;
public long ref; private long ref;
private Concr(PGF gr, long ref) { private Concr(PGF gr, long ref) {
this.gr = gr; this.gr = gr;

View File

@@ -4,7 +4,7 @@ public class Expr {
private Pool pool; private Pool pool;
private long ref; private long ref;
private Expr(Pool pool, long ref) { Expr(Pool pool, long ref) {
this.pool = pool; this.pool = pool;
this.ref = ref; this.ref = ref;
} }

View File

@@ -0,0 +1,11 @@
package org.grammaticalframework.pgf;
public class ParseError extends Exception {
public ParseError(String token) {
super(token);
}
public String getToken() {
return getMessage();
}
}

View File

@@ -6,20 +6,30 @@ class Parser implements Iterable<ExprProb> {
private Concr concr; private Concr concr;
private String s; private String s;
private String startCat; private String startCat;
private ExprIterator iter;
public Parser(Concr concr, String startCat, String s) { public Parser(Concr concr, String startCat, String s) throws ParseError {
this.concr = concr; this.concr = concr;
this.startCat = startCat; this.startCat = startCat;
this.s = s; this.s = s;
this.iter = parse(concr, startCat, s);
} }
public Iterator<ExprProb> iterator() { public Iterator<ExprProb> iterator() {
ExprIterator iter = parse(concr, startCat, s);
if (iter == null) { if (iter == null) {
throw new PGFError("The sentence cannot be parsed"); // If someone has asked for a second iterator over
// the same parse results then we have to parse again.
try {
return parse(concr, startCat, s);
} catch (ParseError e) {
return null;
}
} else {
ExprIterator tmp_iter = iter;
iter = null;
return tmp_iter;
} }
return iter;
} }
static native ExprIterator parse(Concr concr, String startCat, String s); static native ExprIterator parse(Concr concr, String startCat, String s) throws ParseError;
} }