forked from GitHub/gf-core
added Java API to the simple word completion
This commit is contained in:
@@ -566,6 +566,84 @@ Java_org_grammaticalframework_pgf_Concr_lookupMorpho(JNIEnv* env, jobject self,
|
||||
return analyses;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_Lexicon_lookupWordPrefix
|
||||
(JNIEnv* env, jclass clazz, jobject jconcr, jstring prefix)
|
||||
{
|
||||
GuPool* pool = gu_new_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
|
||||
GuEnum* en = pgf_lookup_word_prefix(get_ref(env, jconcr), j2gu_string(env, prefix, pool),
|
||||
pool, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The lookup failed");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jclass iter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/FullFormIterator");
|
||||
jmethodID iter_constrId = (*env)->GetMethodID(env, iter_class, "<init>", "(Lorg/grammaticalframework/pgf/Concr;JJ)V");
|
||||
jobject iter = (*env)->NewObject(env, iter_class, iter_constrId, jconcr, p2l(pool), p2l(en));
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_FullFormIterator_fetchFullFormEntry
|
||||
(JNIEnv* env, jobject clazz, jlong enumRef, jobject jpool, jobject jconcr)
|
||||
{
|
||||
GuEnum* res = (GuEnum*) l2p(enumRef);
|
||||
|
||||
PgfFullFormEntry* entry = gu_next(res, PgfFullFormEntry*, get_ref(env, jpool));
|
||||
if (entry == NULL)
|
||||
return NULL;
|
||||
|
||||
GuString form = pgf_fullform_get_string(entry);
|
||||
|
||||
jclass entry_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/FullFormEntry");
|
||||
jmethodID entry_constrId = (*env)->GetMethodID(env, entry_class, "<init>", "(Ljava/lang/String;JLorg/grammaticalframework/pgf/Concr;)V");
|
||||
jobject jentry = (*env)->NewObject(env, entry_class, entry_constrId, gu2j_string(env,form), p2l(entry), jconcr);
|
||||
|
||||
return jentry;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_FullFormEntry_getAnalyses
|
||||
(JNIEnv* env, jobject self)
|
||||
{
|
||||
jclass list_class = (*env)->FindClass(env, "java/util/ArrayList");
|
||||
jmethodID list_constrId = (*env)->GetMethodID(env, list_class, "<init>", "()V");
|
||||
jobject analyses = (*env)->NewObject(env, list_class, list_constrId);
|
||||
|
||||
jmethodID addId = (*env)->GetMethodID(env, list_class, "add", "(Ljava/lang/Object;)Z");
|
||||
|
||||
jclass an_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/MorphoAnalysis");
|
||||
jmethodID an_constrId = (*env)->GetMethodID(env, an_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;D)V");
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
|
||||
JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, env, addId, an_class, an_constrId };
|
||||
pgf_fullform_get_analyses(get_ref(env, self), &callback.fn, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The lookup failed");
|
||||
}
|
||||
analyses = NULL;
|
||||
}
|
||||
|
||||
gu_pool_free(tmp_pool);
|
||||
|
||||
return analyses;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_org_grammaticalframework_pgf_Concr_hasLinearization(JNIEnv* env, jobject self, jstring jid)
|
||||
{
|
||||
|
||||
@@ -26,6 +26,10 @@ public class Concr {
|
||||
|
||||
public native List<MorphoAnalysis> lookupMorpho(String sentence);
|
||||
|
||||
public Iterable<FullFormEntry> lookupWordPrefix(String prefix) {
|
||||
return new Lexicon(this, prefix);
|
||||
}
|
||||
|
||||
public native boolean hasLinearization(String id);
|
||||
|
||||
public native void load(String path) throws FileNotFoundException;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FullFormEntry {
|
||||
private String form;
|
||||
private long ref;
|
||||
private Concr concr;
|
||||
|
||||
public FullFormEntry(String form, long ref, Concr concr) {
|
||||
this.form = form;
|
||||
this.ref = ref;
|
||||
this.concr = concr;
|
||||
}
|
||||
|
||||
public String getForm() {
|
||||
return form;
|
||||
}
|
||||
|
||||
public native List<MorphoAnalysis> getAnalyses();
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class FullFormIterator implements Iterator<FullFormEntry> {
|
||||
private Concr concr;
|
||||
private Pool pool;
|
||||
private long ref;
|
||||
private FullFormEntry entry;
|
||||
private boolean fetched;
|
||||
|
||||
public FullFormIterator(Concr concr, long pool, long ref) {
|
||||
this.concr = concr;
|
||||
this.pool = new Pool(pool);
|
||||
this.ref = ref;
|
||||
this.entry = null;
|
||||
this.fetched = false;
|
||||
}
|
||||
|
||||
private native FullFormEntry fetchFullFormEntry(long ref, Pool pool, Concr concr);
|
||||
|
||||
private void fetch() {
|
||||
if (!fetched) {
|
||||
entry = fetchFullFormEntry(ref, pool, concr);
|
||||
fetched = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
fetch();
|
||||
return (entry != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FullFormEntry next() {
|
||||
fetch();
|
||||
fetched = false;
|
||||
|
||||
if (entry == null)
|
||||
throw new NoSuchElementException();
|
||||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
29
src/runtime/java/org/grammaticalframework/pgf/Lexicon.java
Normal file
29
src/runtime/java/org/grammaticalframework/pgf/Lexicon.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
class Lexicon implements Iterable<FullFormEntry> {
|
||||
private Concr concr;
|
||||
private String prefix;
|
||||
private FullFormIterator iter;
|
||||
|
||||
public Lexicon(Concr concr, String prefix) {
|
||||
this.concr = concr;
|
||||
this.prefix = prefix;
|
||||
this.iter = lookupWordPrefix(concr, prefix);
|
||||
}
|
||||
|
||||
public Iterator<FullFormEntry> iterator() {
|
||||
if (iter == null) {
|
||||
// If someone has asked for a second iterator over
|
||||
// the same parse results then we have to parse again.
|
||||
return lookupWordPrefix(concr, prefix);
|
||||
} else {
|
||||
FullFormIterator tmp_iter = iter;
|
||||
iter = null;
|
||||
return tmp_iter;
|
||||
}
|
||||
}
|
||||
|
||||
static private native FullFormIterator lookupWordPrefix(Concr concr, String prefix);
|
||||
}
|
||||
Reference in New Issue
Block a user