sentence lookup API from Java

This commit is contained in:
krasimir
2017-05-12 11:12:29 +00:00
parent 7db25c5a74
commit 2b911792ee
3 changed files with 74 additions and 2 deletions

View File

@@ -651,6 +651,34 @@ Java_org_grammaticalframework_pgf_Completer_complete(JNIEnv* env, jclass clazz,
return jtokiter;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_SentenceExtractor_lookupSentence
(JNIEnv* env, jclass clazz, jobject jconcr, jstring jstartCat, jstring js, jobject jpool)
{
GuPool* pool = get_ref(env, jpool);
GuPool* out_pool = gu_new_pool();
GuString startCat = j2gu_string(env, jstartCat, pool);
GuString s = j2gu_string(env, js, pool);
PgfType* type = gu_new_flex(pool, PgfType, exprs, 0);
type->hypos = gu_empty_seq();
type->cid = startCat;
type->n_exprs = 0;
GuEnum* res =
pgf_lookup_sentence(get_ref(env, jconcr), type, s, pool, out_pool);
jfieldID refId = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, jconcr), "gr", "Lorg/grammaticalframework/pgf/PGF;");
jobject jpgf = (*env)->GetObjectField(env, jconcr, refId);
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;Lorg/grammaticalframework/pgf/Pool;JJ)V");
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, jpool, p2l(out_pool), p2l(res));
return jexpiter;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_TokenIterator_fetchTokenProb(JNIEnv* env, jclass clazz, jlong enumRef, jobject jpool)
{
@@ -669,11 +697,12 @@ Java_org_grammaticalframework_pgf_TokenIterator_fetchTokenProb(JNIEnv* env, jcla
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_ExprIterator_fetchExprProb
(JNIEnv* env, jclass clazz, jlong enumRef, jobject pool, jobject gr)
(JNIEnv* env, jclass clazz, jlong enumRef, jobject jpool, jobject gr)
{
GuEnum* res = (GuEnum*) l2p(enumRef);
GuPool* pool = get_ref(env, jpool);
PgfExprProb* ep = gu_next(res, PgfExprProb*, NULL);
PgfExprProb* ep = gu_next(res, PgfExprProb*, pool);
if (ep == NULL)
return NULL;

View File

@@ -39,6 +39,10 @@ public class Concr {
return new Completer(this, startCat, s, prefix);
}
public Iterable<ExprProb> lookupSentence(String startCat, String s) {
return new SentenceExtractor(this, startCat, s);
}
/** Computes the linearization of the abstract expression. */
public native String linearize(Expr expr);

View File

@@ -0,0 +1,39 @@
package org.grammaticalframework.pgf;
import java.util.*;
class SentenceExtractor implements Iterable<ExprProb> {
private Concr concr;
private String s;
private String startCat;
private double heuristics;
private Map<String,LiteralCallback> callbacks;
private ExprIterator iter;
public SentenceExtractor(Concr concr, String startCat, String s) {
this.concr = concr;
this.startCat = startCat;
this.s = s;
this.iter = doExtract();
}
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 doExtract();
} else {
ExprIterator tmp_iter = iter;
iter = null;
return tmp_iter;
}
}
private ExprIterator doExtract()
{
Pool pool = new Pool();
return lookupSentence(concr, startCat, s, pool);
}
static native ExprIterator lookupSentence(Concr concr, String startCat, String s, Pool pool);
}