mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
fix the error handling in the Java binding
This commit is contained in:
@@ -6,7 +6,7 @@ public class Test {
|
||||
public static void main(String[] args) {
|
||||
PGF gr = null;
|
||||
try {
|
||||
gr = PGF.readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf");
|
||||
gr = PGF.readPGF("Phrasebook.pgf");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
@@ -14,16 +14,22 @@ public class Test {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
System.out.println(gr.getAbstractName());
|
||||
for (Map.Entry<String,Concr> entry : gr.getLanguages().entrySet()) {
|
||||
System.out.println(entry.getKey()+" "+entry.getValue()+" "+entry.getValue().getName());
|
||||
}
|
||||
|
||||
Concr eng = gr.getLanguages().get("ParseEng");
|
||||
for (ExprProb ep : eng.parse("Phr", "where are you")) {
|
||||
System.out.println("["+ep.getProb()+"] "+ep.getExpr());
|
||||
System.out.println(eng.linearize(ep.getExpr()));
|
||||
|
||||
Concr eng = gr.getLanguages().get("PhrasebookEng");
|
||||
Concr ger = gr.getLanguages().get("PhrasebookGer");
|
||||
|
||||
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()+"\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,6 +226,7 @@ Java_org_grammaticalframework_pgf_Parser_parse
|
||||
|
||||
gu_pool_free(pool);
|
||||
gu_pool_free(out_pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
|
||||
|
||||
@@ -6,7 +6,7 @@ public class Concr {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Concr {
|
||||
// private stuff
|
||||
|
||||
private PGF gr;
|
||||
public long ref;
|
||||
private long ref;
|
||||
|
||||
private Concr(PGF gr, long ref) {
|
||||
this.gr = gr;
|
||||
|
||||
@@ -4,7 +4,7 @@ public class Expr {
|
||||
private Pool pool;
|
||||
private long ref;
|
||||
|
||||
private Expr(Pool pool, long ref) {
|
||||
Expr(Pool pool, long ref) {
|
||||
this.pool = pool;
|
||||
this.ref = ref;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
public class ParseError extends Exception {
|
||||
public ParseError(String token) {
|
||||
super(token);
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return getMessage();
|
||||
}
|
||||
}
|
||||
@@ -6,20 +6,30 @@ class Parser implements Iterable<ExprProb> {
|
||||
private Concr concr;
|
||||
private String s;
|
||||
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.startCat = startCat;
|
||||
this.s = s;
|
||||
this.iter = parse(concr, startCat, s);
|
||||
}
|
||||
|
||||
public Iterator<ExprProb> iterator() {
|
||||
ExprIterator iter = parse(concr, startCat, s);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user