示例#1
0
文件: jpgf.c 项目: Deseaus/GF
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(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, 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, 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;
}
示例#2
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jstring JNICALL
Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, jobject jexpr)
{
	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);
	GuStringBuf* sbuf = gu_string_buf(tmp_pool);
	GuOut* out = gu_string_buf_out(sbuf);
	
	pgf_linearize(get_ref(env, self), gu_variant_from_ptr((void*) get_ref(env, jexpr)), out, err);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, PgfLinNonExist)) {
			gu_pool_free(tmp_pool);
			return NULL;
		} else if (gu_exn_caught(err, 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 expression cannot be linearized");
		}
		gu_pool_free(tmp_pool);
		return NULL;
	}

	GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
	jstring jstr = gu2j_string(env, str);

	gu_pool_free(tmp_pool);
	
	return jstr;
}
示例#3
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jobject JNICALL 
Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring s)
{ 
	GuPool* pool = gu_new_pool();
	GuPool* tmp_pool = gu_local_pool();

	// Create an exception frame that catches all errors.
	GuExn* err = gu_exn(tmp_pool);

	const char *fpath = (*env)->GetStringUTFChars(env, s, 0); 

	// Read the PGF grammar.
	PgfPGF* pgf = pgf_read(fpath, pool, err);

	(*env)->ReleaseStringUTFChars(env, s, fpath);

	if (!gu_ok(err)) {
		if (gu_exn_caught(err, GuErrno)) {
			throw_jstring_exception(env, "java/io/FileNotFoundException", s);
		} else {
			throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The grammar cannot be loaded");
		}
		gu_pool_free(pool);
		gu_pool_free(tmp_pool);
		return NULL;
	}

	gu_pool_free(tmp_pool);

	jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(JJ)V");
	return (*env)->NewObject(env, cls, constrId, p2l(pool), p2l(pgf));
}
示例#4
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_sg_SG_openSG(JNIEnv *env, jclass cls, jstring path)
{
	GuPool* tmp_pool = gu_local_pool();

	// Create an exception frame that catches all errors.
	GuExn* err = gu_exn(tmp_pool);

	const char *fpath = (*env)->GetStringUTFChars(env, path, 0); 

	// Read the PGF grammar.
	SgSG* sg = sg_open(fpath, err);

	(*env)->ReleaseStringUTFChars(env, path, fpath);

	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "The database cannot be opened";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
		gu_pool_free(tmp_pool);
		return NULL;
	}

	gu_pool_free(tmp_pool);

	jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(J)V");
	return (*env)->NewObject(env, cls, constrId, p2l(sg));
}
示例#5
0
文件: pgf-translate.c 项目: k0001/GF
static void
print_result(PgfExprProb* ep, PgfConcr* to_concr,
             GuOut* out, GuExn* err, GuPool* ppool)
{
    // Write out the abstract syntax tree
    gu_printf(out, err, " [%f] ", ep->prob);
    pgf_print_expr(ep->expr, NULL, 0, out, err);
    gu_putc('\n', out, err);

    // Enumerate the concrete syntax trees corresponding
    // to the abstract tree.
    GuEnum* cts = pgf_lzr_concretize(to_concr, ep->expr, err, ppool);
    while (true) {
        PgfCncTree ctree =
            gu_next(cts, PgfCncTree, ppool);
        if (gu_variant_is_null(ctree)) {
            break;
        }
        gu_putc(' ', out, err);
        // Linearize the concrete tree as a simple
        // sequence of strings.
        pgf_lzr_linearize_simple(to_concr, ctree, 0, out, err, ppool);

        if (gu_exn_caught(err, PgfLinNonExist)) {
            // encountered nonExist. Unfortunately there
            // might be some output printed already. The
            // right solution should be to use GuStringBuf.
            gu_exn_clear(err);
        }
        gu_putc('\n', out, err);
        gu_out_flush(out, err);
    }
}
示例#6
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Concr_lookupMorpho(JNIEnv* env, jobject self, jstring sentence)
{
	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_new_pool();
	
	GuExn* err = gu_exn(tmp_pool);

	JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId };
	pgf_lookup_morpho(get_ref(env, self), j2gu_string(env, sentence, tmp_pool),
	                  &callback.fn, err);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, 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;
}
示例#7
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT jlong JNICALL
Java_org_grammaticalframework_sg_SG_insertTriple(JNIEnv *env, jobject self,
                                                jobject jsubj,
                                                jobject jpred,
                                                jobject jobj)
{
	SgSG *sg = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);

	SgTriple triple;
	triple[0] = gu_variant_from_ptr((void*) get_ref(env, jsubj));
	triple[1] = gu_variant_from_ptr((void*) get_ref(env, jpred));
	triple[2] = gu_variant_from_ptr((void*) get_ref(env, jobj));

	SgId id = sg_insert_triple(sg, triple, err);
	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "The insertion failed";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
		gu_pool_free(tmp_pool);
		return 0;
	}

	gu_pool_free(tmp_pool);

	return id;
}
示例#8
0
文件: jpgf.c 项目: Deseaus/GF
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(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, 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;
}
示例#9
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT void JNICALL
Java_org_grammaticalframework_pgf_Concr_load__Ljava_io_InputStream_2(JNIEnv* env, jobject self, jobject java_stream)
{
	GuPool* tmp_pool = gu_local_pool();

	GuInStream* jstream =
		jpgf_new_java_stream(env, java_stream, tmp_pool);
	if (!jstream) {
		gu_pool_free(tmp_pool);
		return;
	}

	GuIn* in = gu_new_in(jstream, tmp_pool);

	// Create an exception frame that catches all errors.
	GuExn* err = gu_exn(tmp_pool);

	pgf_concrete_load(get_ref(env, self), in, err);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, 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 language cannot be loaded");
		}
	}

	gu_pool_free(tmp_pool);
}
示例#10
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jobject JNICALL 
Java_org_grammaticalframework_pgf_Parser_parseWithHeuristics
  (JNIEnv* env, jclass clazz, jobject jconcr, jstring jstartCat, jstring js, jdouble heuristics, jlong callbacksRef, 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);
    GuExn* parse_err = gu_new_exn(pool);

	GuEnum* res =
		pgf_parse_with_heuristics(get_ref(env, jconcr), startCat, s, heuristics, l2p(callbacksRef), parse_err, pool, out_pool);

	if (!gu_ok(parse_err)) {
		if (gu_exn_caught(parse_err, 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, PgfParseError)) {
			GuString tok = (GuString) gu_exn_caught_data(parse_err);
			throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok);
		}

		gu_pool_free(out_pool);
		return NULL;
	}

	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;
}
示例#11
0
文件: jpgf.c 项目: Deseaus/GF
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 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_exn(tmp_pool);

	JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId };
	pgf_fullform_get_analyses(entry, &callback.fn, err);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, 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);

	jclass entry_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/FullFormEntry");
	jmethodID entry_constrId = (*env)->GetMethodID(env, entry_class, "<init>", "(Ljava/lang/String;DLjava/util/List;)V");
	jobject jentry = (*env)->NewObject(env, entry_class, entry_constrId, gu2j_string(env,form), - log(callback.prob), analyses);

	return jentry;
}
示例#12
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT void JNICALL
Java_org_grammaticalframework_sg_TripleResult_close(JNIEnv *env, jobject self)
{
	SgTripleResult *res = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);
	
	sg_triple_result_close(res, err);
	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "Closing the result failed";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
	}

	gu_pool_free(tmp_pool);
}
示例#13
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_sg_SG_queryTriple(JNIEnv *env, jobject self,
                                                jobject jsubj,
                                                jobject jpred,
                                                jobject jobj)
{
	SgSG *sg = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);

	SgTriple triple;
	triple[0] = (jsubj == NULL) ? gu_null_variant
	                            : gu_variant_from_ptr((void*) get_ref(env, jsubj));
	triple[1] = (jpred == NULL) ? gu_null_variant
	                            : gu_variant_from_ptr((void*) get_ref(env, jpred));
	triple[2] = (jobj == NULL)  ? gu_null_variant
	                            : gu_variant_from_ptr((void*) get_ref(env, jobj));
	
	SgTripleResult* res = sg_query_triple(sg, triple, err);
	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "The query failed";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
		gu_pool_free(tmp_pool);
		return NULL;
	}

	gu_pool_free(tmp_pool);

	jclass res_class = (*env)->FindClass(env, "org/grammaticalframework/sg/TripleResult");
	jmethodID constrId = (*env)->GetMethodID(env, res_class, "<init>", "(JLorg/grammaticalframework/pgf/Expr;Lorg/grammaticalframework/pgf/Expr;Lorg/grammaticalframework/pgf/Expr;)V");
	jobject jres = (*env)->NewObject(env, res_class, constrId, p2l(res), jsubj, jpred, jobj);

	return jres;
}
示例#14
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT void JNICALL
Java_org_grammaticalframework_sg_SG_close(JNIEnv *env, jobject self)
{
	GuPool* tmp_pool = gu_local_pool();

	// Create an exception frame that catches all errors.
	GuExn* err = gu_exn(tmp_pool);

	sg_close(get_ref(env, self), err);
	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "The database cannot be closed";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
		gu_pool_free(tmp_pool);
		return;
	}

	gu_pool_free(tmp_pool);
}
示例#15
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jobjectArray JNICALL 
Java_org_grammaticalframework_pgf_Concr_bracketedLinearize(JNIEnv* env, jobject self, jobject jexpr)
{
	jclass object_class = (*env)->FindClass(env, "java/lang/Object");
	if (!object_class)
		return NULL;

	jclass bracket_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Bracket");
	if (!bracket_class)
		return NULL;
	jmethodID bracket_constrId = (*env)->GetMethodID(env, bracket_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;II[Ljava/lang/Object;)V");
	if (!bracket_constrId)
		return NULL;

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);

	PgfConcr* concr = get_ref(env, self);

	GuEnum* cts = 
		pgf_lzr_concretize(concr, gu_variant_from_ptr((void*) get_ref(env, jexpr)), err, tmp_pool);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, 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 expression cannot be concretized");
		}
		gu_pool_free(tmp_pool);
		return NULL;
	}

	PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool);
	if (gu_variant_is_null(ctree)) {
		throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be concretized");
		gu_pool_free(tmp_pool);
		return NULL;
	}

	ctree = pgf_lzr_wrap_linref(ctree, tmp_pool);

	PgfBracketLznState state;
	state.funcs = &pgf_bracket_lin_funcs;
	state.env   = env;
	state.tmp_pool = tmp_pool;
	state.stack = gu_new_buf(GuBuf*, tmp_pool);
	state.list  = gu_new_buf(jobject, tmp_pool);
	state.object_class = object_class;
	state.bracket_class = bracket_class;
	state.bracket_constrId = bracket_constrId;
	pgf_lzr_linearize(concr, ctree, 0, &state.funcs, tmp_pool);

	size_t len = gu_buf_length(state.list);
	jobjectArray array = (*env)->NewObjectArray(env, len, object_class, NULL);
	for (int i = 0; i < len; i++) {
		jobject obj = gu_buf_get(state.list, jobject, i);
		(*env)->SetObjectArrayElement(env, array, i, obj);
		(*env)->DeleteLocalRef(env, obj);
	}

	gu_pool_free(tmp_pool);

	return array;
}
示例#16
0
文件: jpgf.c 项目: Deseaus/GF
JNIEXPORT jobject JNICALL 
Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject self, jobject jexpr)
{
	jclass map_class = (*env)->FindClass(env, "java/util/HashMap");
	if (!map_class)
		return NULL;
	jmethodID constrId = (*env)->GetMethodID(env, map_class, "<init>", "()V");
	if (!constrId)
		return NULL;
	jobject table = (*env)->NewObject(env, map_class, constrId);
	if (!table)
		return NULL;

	jmethodID put_method = (*env)->GetMethodID(env, map_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
	if (!put_method)
		return NULL;
		
	PgfConcr* concr = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuExn* err = gu_exn(tmp_pool);

	GuEnum* cts = 
		pgf_lzr_concretize(concr,
		                   gu_variant_from_ptr((void*) get_ref(env, jexpr)),
		                   err,
		                   tmp_pool);
	if (!gu_ok(err)) {
		if (gu_exn_caught(err, 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 expression cannot be concretized");
		}
		gu_pool_free(tmp_pool);
		return NULL;
	}

	PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool);
	if (gu_variant_is_null(ctree)) {
		gu_pool_free(tmp_pool);
		return NULL;
	}

	size_t n_lins;
	GuString* labels;
	pgf_lzr_get_table(concr, ctree, &n_lins, &labels);

	for (size_t lin_idx = 0; lin_idx < n_lins; lin_idx++) {
		GuStringBuf* sbuf = gu_string_buf(tmp_pool);
		GuOut* out = gu_string_buf_out(sbuf);

		pgf_lzr_linearize_simple(concr, ctree, lin_idx, out, err, tmp_pool);

		jstring jstr = NULL;
		if (gu_ok(err)) {
			GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
			jstr = gu2j_string(env, str);
		} else {
			gu_exn_clear(err);
		}

		jstring jname = gu2j_string(env, labels[lin_idx]);

		(*env)->CallObjectMethod(env, table, put_method, jname, jstr);

		(*env)->DeleteLocalRef(env, jname);
		
		if (jstr != NULL)
			(*env)->DeleteLocalRef(env, jstr);
	}
	
	gu_pool_free(tmp_pool);

	return table;
}
示例#17
0
文件: pgf-translate.c 项目: k0001/GF
int main(int argc, char* argv[]) {
    // Set the character locale, so we can produce proper output.
    setlocale(LC_CTYPE, "");

    // Create the pool that is used to allocate everything
    GuPool* pool = gu_new_pool();
    int status = EXIT_SUCCESS;
    if (argc < 5) {
        fprintf(stderr, "usage: %s pgf cat from-lang to-lang\n", argv[0]);
        status = EXIT_FAILURE;
        goto fail;
    }

    GuString filename = argv[1];
    GuString cat = argv[2];
    GuString from_lang = argv[3];
    GuString to_lang = argv[4];

    // Create an exception frame that catches all errors.
    GuExn* err = gu_new_exn(pool);

    // Read the PGF grammar.
    PgfPGF* pgf = pgf_read(filename, pool, err);

    // If an error occured, it shows in the exception frame
    if (!gu_ok(err)) {
        fprintf(stderr, "Reading PGF failed\n");
        status = EXIT_FAILURE;
        goto fail;
    }

    // Look up the source and destination concrete categories
    PgfConcr* from_concr = pgf_get_language(pgf, from_lang);
    PgfConcr* to_concr = pgf_get_language(pgf, to_lang);
    if (!from_concr || !to_concr) {
        fprintf(stderr, "Unknown language\n");
        status = EXIT_FAILURE;
        goto fail_concr;
    }

    // Register a callback for the literal category Symbol
    PgfCallbacksMap* callbacks =
        pgf_new_callbacks_map(from_concr, pool);
    pgf_callbacks_map_add_literal(from_concr, callbacks,
                                  "PN", &pgf_nerc_literal_callback);
    pgf_callbacks_map_add_literal(from_concr, callbacks,
                                  "Symb", &pgf_unknown_literal_callback);

    // Create an output stream for stdout
    GuOut* out = gu_file_out(stdout, pool);

    // We will keep the latest results in the 'ppool' and
    // we will iterate over them by using 'result'.
    GuPool* ppool = NULL;
    GuEnum* result = NULL;

    // The interactive translation loop.
    // XXX: This currently reads stdin directly, so it doesn't support
    // encodings properly. TODO: use a locale reader for input
    while (true) {
        fprintf(stdout, "> ");
        fflush(stdout);
        char buf[4096];
        char* line = fgets(buf, sizeof(buf), stdin);
        if (line == NULL) {
            if (ferror(stdin)) {
                fprintf(stderr, "Input error\n");
                status = EXIT_FAILURE;
            }
            break;
        } else if (strcmp(line, "") == 0) {
            // End nicely on empty input
            break;
        } else if (strcmp(line, "\n") == 0) {
            // Empty line -> show the next tree for the last sentence

            if (result != NULL) {
                clock_t start = clock();

                PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

                clock_t end = clock();
                double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
                printf("%.2f sec\n", cpu_time_used);

                // The enumerator will return a null variant at the
                // end of the results.
                if (ep == NULL) {
                    goto fail_parse;
                }

                print_result(ep, to_concr, out, err, ppool);
            }
            continue;
        }

        // We release the last results
        if (ppool != NULL) {
            gu_pool_free(ppool);
            ppool  = NULL;
            result = NULL;
        }

        // We create a temporary pool for translating a single
        // sentence, so our memory usage doesn't increase over time.
        ppool = gu_new_pool();

        clock_t start = clock();

        GuExn* parse_err = gu_new_exn(ppool);
        result =
            pgf_parse_with_heuristics(from_concr, cat, line,
                                      -1, callbacks,
                                      parse_err, ppool, ppool);
        if (!gu_ok(parse_err)) {
            if (gu_exn_caught(parse_err, PgfExn)) {
                GuString msg = gu_exn_caught_data(parse_err);
                gu_string_write(msg, out, err);
                gu_putc('\n', out, err);
            } else if (gu_exn_caught(parse_err, PgfParseError)) {
                gu_puts("Unexpected token: \"", out, err);
                GuString tok = gu_exn_caught_data(parse_err);
                gu_string_write(tok, out, err);
                gu_puts("\"\n", out, err);
            }

            goto fail_parse;
        }

        PgfExprProb* ep = gu_next(result, PgfExprProb*, ppool);

        clock_t end = clock();
        double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
        printf("%.2f sec\n", cpu_time_used);

        // The enumerator will return null at the end of the results.
        if (ep == NULL) {
            goto fail_parse;
        }

        print_result(ep, to_concr, out, err, ppool);

        continue;
fail_parse:
        // Free all resources allocated during parsing and linearization
        gu_pool_free(ppool);
        ppool = NULL;
        result = NULL;
    }
示例#18
0
文件: jsg.c 项目: Ehrlemark/GF
JNIEXPORT jboolean JNICALL
Java_org_grammaticalframework_sg_TripleResult_hasNext(JNIEnv *env, jobject self)
{
	SgTripleResult *res = get_ref(env, self);

	GuPool* tmp_pool = gu_local_pool();
	GuPool* out_pool = gu_new_pool();
	GuExn* err = gu_exn(tmp_pool);

	SgId key;
	SgTriple triple;
	int r = sg_triple_result_fetch(res, &key, triple, out_pool, err);
	if (!gu_ok(err)) {
		GuString msg;
		if (gu_exn_caught(err, SgError)) {
			msg = (GuString) gu_exn_caught_data(err);
		} else {
			msg = "The fetch failed";
		}
		throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg);
		gu_pool_free(out_pool);
		gu_pool_free(tmp_pool);
		return JNI_FALSE;
	}

	gu_pool_free(tmp_pool);

	if (r) {
		SgTriple orig_triple;
		sg_triple_result_get_query(res, orig_triple);

		jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool");
		jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V");
		jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(out_pool));

		jclass expr_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Expr");
		jmethodID constrId = (*env)->GetMethodID(env, expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V");

		jclass result_class = (*env)->GetObjectClass(env, self);

		jfieldID keyId = (*env)->GetFieldID(env, result_class, "key", "J");
		(*env)->SetLongField(env, self, keyId, key);

		if (triple[0] != orig_triple[0]) {
			jfieldID subjId = (*env)->GetFieldID(env, result_class, "subj", "Lorg/grammaticalframework/pgf/Expr;");
			jobject jsubj = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[0])));
			(*env)->SetObjectField(env, self, subjId, jsubj);
		}

		if (triple[1] != orig_triple[1]) {
			jfieldID predId = (*env)->GetFieldID(env, result_class, "pred", "Lorg/grammaticalframework/pgf/Expr;");
			jobject jpred = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[1])));
			(*env)->SetObjectField(env, self, predId, jpred);
		}

		if (triple[2] != orig_triple[2]) {
			jfieldID objId = (*env)->GetFieldID(env, result_class, "obj", "Lorg/grammaticalframework/pgf/Expr;");
			jobject jobj  = (*env)->NewObject(env, expr_class, constrId, jpool, jpool, p2l(gu_variant_to_ptr(triple[2])));
			(*env)->SetObjectField(env, self, objId, jobj);
		}

		return JNI_TRUE;
	} else {
		gu_pool_free(out_pool);
		return JNI_FALSE;
	}
}