mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
an initial Java/C API for predicting literals. Still not utilized
This commit is contained in:
@@ -48,8 +48,25 @@ pgf_match_string_lit(PgfLiteralCallback* self,
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pgf_predict_empty_next(GuEnum* self, void* to, GuPool* pool)
|
||||
{
|
||||
*((PgfTokenProb**) to) = NULL;
|
||||
}
|
||||
|
||||
static GuEnum*
|
||||
pgf_predict_empty(PgfLiteralCallback* self,
|
||||
size_t lin_idx,
|
||||
GuString prefix,
|
||||
GuPool *out_pool)
|
||||
{
|
||||
GuEnum* en = gu_new(GuEnum, out_pool);
|
||||
en->next = pgf_predict_empty_next;
|
||||
return en;
|
||||
}
|
||||
|
||||
static PgfLiteralCallback pgf_string_literal_callback =
|
||||
{ pgf_match_string_lit } ;
|
||||
{ pgf_match_string_lit, pgf_predict_empty } ;
|
||||
|
||||
|
||||
|
||||
@@ -101,7 +118,7 @@ pgf_match_int_lit(PgfLiteralCallback* self,
|
||||
}
|
||||
|
||||
static PgfLiteralCallback pgf_int_literal_callback =
|
||||
{ pgf_match_int_lit } ;
|
||||
{ pgf_match_int_lit, pgf_predict_empty } ;
|
||||
|
||||
|
||||
|
||||
@@ -153,7 +170,7 @@ pgf_match_float_lit(PgfLiteralCallback* self,
|
||||
}
|
||||
|
||||
static PgfLiteralCallback pgf_float_literal_callback =
|
||||
{ pgf_match_float_lit } ;
|
||||
{ pgf_match_float_lit, pgf_predict_empty } ;
|
||||
|
||||
|
||||
|
||||
@@ -231,7 +248,7 @@ pgf_match_name_lit(PgfLiteralCallback* self,
|
||||
}
|
||||
|
||||
PgfLiteralCallback pgf_nerc_literal_callback =
|
||||
{ pgf_match_name_lit } ;
|
||||
{ pgf_match_name_lit, pgf_predict_empty } ;
|
||||
|
||||
|
||||
PgfCallbacksMap*
|
||||
|
||||
@@ -161,6 +161,10 @@ struct PgfLiteralCallback {
|
||||
size_t lin_idx,
|
||||
GuString sentence, size_t* poffset,
|
||||
GuPool *out_pool);
|
||||
GuEnum* (*predict)(PgfLiteralCallback* self,
|
||||
size_t lin_idx,
|
||||
GuString prefix,
|
||||
GuPool *out_pool);
|
||||
};
|
||||
|
||||
void
|
||||
|
||||
@@ -713,19 +713,26 @@ typedef struct {
|
||||
PgfLiteralCallback callback;
|
||||
jobject jcallback;
|
||||
jmethodID match_methodId;
|
||||
jmethodID predict_methodId;
|
||||
GuFinalizer fin;
|
||||
} JPgfLiteralCallback;
|
||||
|
||||
static PgfExprProb*
|
||||
jpgf_literal_callback_fn(PgfLiteralCallback* self,
|
||||
size_t lin_idx,
|
||||
GuString sentence, size_t* poffset,
|
||||
GuPool *out_pool)
|
||||
typedef struct {
|
||||
GuEnum en;
|
||||
jobject jiterator;
|
||||
GuFinalizer fin;
|
||||
} JPgfTokenProbEnum;
|
||||
|
||||
static PgfExprProb*
|
||||
jpgf_literal_callback_match(PgfLiteralCallback* self,
|
||||
size_t lin_idx,
|
||||
GuString sentence, size_t* poffset,
|
||||
GuPool *out_pool)
|
||||
{
|
||||
JPgfLiteralCallback* callback = gu_container(self, JPgfLiteralCallback, callback);
|
||||
|
||||
JNIEnv *env;
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, &env, NULL);
|
||||
|
||||
jstring jsentence = gu2j_string(env, sentence);
|
||||
jobject result = (*env)->CallObjectMethod(env, callback->jcallback, callback->match_methodId, lin_idx, jsentence, *poffset);
|
||||
@@ -782,13 +789,50 @@ jpgf_literal_callback_fn(PgfLiteralCallback* self,
|
||||
return ep;
|
||||
}
|
||||
|
||||
static void
|
||||
jpgf_token_prob_enum_fin(GuFinalizer* self)
|
||||
{
|
||||
JPgfTokenProbEnum* en = gu_container(self, JPgfTokenProbEnum, fin);
|
||||
|
||||
JNIEnv *env;
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, &env, NULL);
|
||||
|
||||
(*env)->DeleteGlobalRef(env, en->jiterator);
|
||||
}
|
||||
|
||||
static GuEnum*
|
||||
jpgf_literal_callback_predict(PgfLiteralCallback* self,
|
||||
size_t lin_idx,
|
||||
GuString prefix,
|
||||
GuPool *out_pool)
|
||||
{
|
||||
JPgfLiteralCallback* callback = gu_container(self, JPgfLiteralCallback, callback);
|
||||
|
||||
JNIEnv *env;
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, &env, NULL);
|
||||
|
||||
jstring jprefix = gu2j_string(env, prefix);
|
||||
jobject jiterator = (*env)->CallObjectMethod(env, callback->jcallback, callback->predict_methodId, lin_idx, jprefix);
|
||||
if (jiterator == NULL)
|
||||
return NULL;
|
||||
|
||||
JPgfTokenProbEnum* en = gu_new(JPgfTokenProbEnum, out_pool);
|
||||
en->en.next = NULL;
|
||||
en->jiterator = (*env)->NewGlobalRef(env, jiterator);
|
||||
en->fin.fn = jpgf_token_prob_enum_fin;
|
||||
|
||||
gu_pool_finally(out_pool, &en->fin);
|
||||
|
||||
return &en->en;
|
||||
}
|
||||
|
||||
static void
|
||||
jpgf_literal_callback_fin(GuFinalizer* self)
|
||||
{
|
||||
JPgfLiteralCallback* callback = gu_container(self, JPgfLiteralCallback, fin);
|
||||
|
||||
JNIEnv *env;
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
|
||||
(*cachedJVM)->AttachCurrentThread(cachedJVM, &env, NULL);
|
||||
|
||||
(*env)->DeleteGlobalRef(env, callback->jcallback);
|
||||
}
|
||||
@@ -800,12 +844,14 @@ Java_org_grammaticalframework_pgf_Concr_addLiteral(JNIEnv* env, jobject self, js
|
||||
GuPool* pool = pgf_concr_get_pool(concr);
|
||||
|
||||
JPgfLiteralCallback* callback = gu_new(JPgfLiteralCallback, pool);
|
||||
callback->callback.match = jpgf_literal_callback_fn;
|
||||
callback->callback.match = jpgf_literal_callback_match;
|
||||
callback->callback.predict = jpgf_literal_callback_predict;
|
||||
callback->jcallback = (*env)->NewGlobalRef(env, jcallback);
|
||||
callback->fin.fn = jpgf_literal_callback_fin;
|
||||
|
||||
jclass callback_class = (*env)->GetObjectClass(env, jcallback);
|
||||
callback->match_methodId = (*env)->GetMethodID(env, callback_class, "match", "(ILjava/lang/String;I)Lorg/grammaticalframework/pgf/LiteralCallback$CallbackResult;");
|
||||
callback->predict_methodId = (*env)->GetMethodID(env, callback_class, "predict", "(ILjava/lang/String;)Ljava/util/Iterator;");
|
||||
|
||||
gu_pool_finally(pool, &callback->fin);
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public interface LiteralCallback {
|
||||
public CallbackResult match(int lin_idx, String sentence, int start_offset);
|
||||
|
||||
|
||||
public Iterator<TokenProb> predict(int lin_idx, String prefix);
|
||||
|
||||
public static class CallbackResult {
|
||||
private ExprProb ep;
|
||||
private int offset;
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class NercLiteralCallback implements LiteralCallback {
|
||||
public CallbackResult match(int lin_idx, String sentence, int offset) {
|
||||
StringBuilder sbuilder = new StringBuilder();
|
||||
@@ -33,4 +36,8 @@ public class NercLiteralCallback implements LiteralCallback {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Iterator<TokenProb> predict(int lin_idx, String prefix) {
|
||||
return Collections.<TokenProb>emptyList().iterator();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user