expose Java API for word completion

This commit is contained in:
kr.angelov
2014-04-11 07:50:22 +00:00
parent 65b4ba2459
commit b82f9a5035
7 changed files with 121 additions and 5 deletions

View File

@@ -388,9 +388,58 @@ Java_org_grammaticalframework_pgf_Parser_parse
return jexpiter;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Completer_complete(JNIEnv* env, jclass clazz, jobject jconcr, jstring jstartCat, jstring js, jstring jprefix)
{
GuPool* pool = gu_new_pool();
GuString startCat = j2gu_string(env, jstartCat, pool);
GuString s = j2gu_string(env, js, pool);
GuString prefix = j2gu_string(env, jprefix, pool);
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), pool);
GuEnum* res =
pgf_complete(get_ref(env, jconcr), startCat, s, prefix, parse_err, pool);
if (!gu_ok(parse_err)) {
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
GuString msg = (GuString) gu_exn_caught_data(parse_err);
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
GuString tok = (GuString) gu_exn_caught_data(parse_err);
throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok);
}
gu_pool_free(pool);
return NULL;
}
jclass tokiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/TokenIterator");
jmethodID constrId = (*env)->GetMethodID(env, tokiter_class, "<init>", "(JJ)V");
jobject jtokiter = (*env)->NewObject(env, tokiter_class, constrId, p2l(pool), p2l(res));
return jtokiter;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_TokenIterator_fetchTokenProb(JNIEnv* env, jclass clazz, jlong enumRef, jobject jpool)
{
GuEnum* res = (GuEnum*) l2p(enumRef);
PgfTokenProb* tp = gu_next(res, PgfTokenProb*, get_ref(env, jpool));
if (tp == NULL)
return NULL;
jclass tp_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/TokenProb");
jmethodID tp_constrId = (*env)->GetMethodID(env, tp_class, "<init>", "(Ljava/lang/String;D)V");
jobject jtp = (*env)->NewObject(env, tp_class, tp_constrId, gu2j_string(env,tp->tok), tp->prob);
return jtp;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_ExprIterator_fetchExprProb
(JNIEnv* env, jobject self, jlong enumRef, jobject pool, jobject gr)
(JNIEnv* env, jclass clazz, jlong enumRef, jobject pool, jobject gr)
{
GuEnum* res = (GuEnum*) l2p(enumRef);

View File

@@ -20,6 +20,10 @@ public class Concr {
}
}
public Iterable<TokenProb> complete(String startCat, String s, String prefix) throws ParseError {
return new Completer(this, startCat, s, prefix);
}
public native String linearize(Expr expr);
public native Map<String, String> tabularLinearize(Expr expr);

View File

@@ -18,7 +18,7 @@ class ExprIterator implements Iterator<ExprProb> {
this.fetched = false;
}
private native ExprProb fetchExprProb(long ref, Pool pool, PGF gr);
private native static ExprProb fetchExprProb(long ref, Pool pool, PGF gr);
private void fetch() {
if (!fetched) {

View File

@@ -18,7 +18,7 @@ class FullFormIterator implements Iterator<FullFormEntry> {
this.fetched = false;
}
private native FullFormEntry fetchFullFormEntry(long ref, Pool pool, Concr concr);
private native static FullFormEntry fetchFullFormEntry(long ref, Pool pool, Concr concr);
private void fetch() {
if (!fetched) {

View File

@@ -16,7 +16,7 @@ class Generator implements Iterable<ExprProb> {
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.
// the same results then we have to generate again.
return generateAll(gr, startCat);
} else {
ExprIterator tmp_iter = iter;
@@ -25,5 +25,5 @@ class Generator implements Iterable<ExprProb> {
}
}
static native ExprIterator generateAll(PGF gr, String startCat);
private native static ExprIterator generateAll(PGF gr, String startCat);
}

View File

@@ -0,0 +1,44 @@
package org.grammaticalframework.pgf;
import java.util.*;
class TokenIterator implements Iterator<TokenProb> {
private Pool pool;
private long ref;
private TokenProb tp;
private boolean fetched;
public TokenIterator(long pool, long ref) {
this.pool = new Pool(pool);
this.ref = ref;
this.tp = null;
this.fetched = false;
}
private native static TokenProb fetchTokenProb(long ref, Pool pool);
private void fetch() {
if (!fetched) {
tp = fetchTokenProb(ref, pool);
fetched = true;
}
}
public boolean hasNext() {
fetch();
return (tp != null);
}
public TokenProb next() {
fetch();
fetched = false;
if (tp == null)
throw new NoSuchElementException();
return tp;
}
public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,19 @@
package org.grammaticalframework.pgf;
public class TokenProb {
private String tok;
private double prob;
public TokenProb(String tok, double prob) {
this.tok = tok;
this.prob = prob;
}
public String getToken() {
return tok;
}
public double getProb() {
return prob;
}
}