added exhaustive generation in the Java binding

This commit is contained in:
kr.angelov
2013-09-06 07:21:38 +00:00
parent 14f8b96b56
commit 5a4454aa8f
4 changed files with 62 additions and 1 deletions

View File

@@ -19,6 +19,14 @@ public class Test {
for (Map.Entry<String,Concr> entry : gr.getLanguages().entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue()+" "+entry.getValue().getName());
}
int count = 10;
for (ExprProb ep : gr.generateAll("Phrase")) {
System.out.println(ep.getExpr());
if (count-- <= 0)
break;
}
Concr eng = gr.getLanguages().get("PhrasebookEng");
Concr ger = gr.getLanguages().get("PhrasebookGer");

View File

@@ -395,3 +395,25 @@ Java_org_grammaticalframework_pgf_Expr_showExpr(JNIEnv* env, jclass clazz, jlong
gu_pool_free(tmp_pool);
return jstr;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
{
GuPool* pool = gu_new_pool();
GuString startCat = j2gu_string(env, jstartCat, pool);
GuEnum* res =
pgf_generate_all(get_ref(env, jpgf), startCat, pool);
if (res == NULL) {
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed");
gu_pool_free(pool);
return NULL;
}
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V");
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(pool), p2l(res));
return jexpiter;
}

View File

@@ -0,0 +1,29 @@
package org.grammaticalframework.pgf;
import java.util.Iterator;
class Generator implements Iterable<ExprProb> {
private PGF gr;
private String startCat;
private ExprIterator iter;
public Generator(PGF gr, String startCat) {
this.gr = gr;
this.startCat = startCat;
this.iter = generateAll(gr, startCat);
}
public Iterator<ExprProb> iterator() {
if (iter == null) {
// If someone has asked for a second iterator over
// the same parse results then we have to parse again.
return generateAll(gr, startCat);
} else {
ExprIterator tmp_iter = iter;
iter = null;
return tmp_iter;
}
}
static native ExprIterator generateAll(PGF gr, String startCat);
}

View File

@@ -22,7 +22,9 @@ public class PGF {
public native Type getFunctionType(String fun);
public native Iterator<ExprProb> generate(Type type);
public Iterable<ExprProb> generateAll(String startCat) {
return new Generator(this, startCat);
}
public native Expr compute(Expr expr);