mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
added exhaustive generation in the Java binding
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
29
src/runtime/java/org/grammaticalframework/pgf/Generator.java
Normal file
29
src/runtime/java/org/grammaticalframework/pgf/Generator.java
Normal 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);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user