JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Expr_readExpr(JNIEnv* env, jclass clazz, jstring s) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuString buf = j2gu_string(env, s, tmp_pool); GuIn* in = gu_data_in((uint8_t*) buf, strlen(buf), tmp_pool); GuExn* err = gu_exn(tmp_pool); PgfExpr e = pgf_read_expr(in, pool, err); if (!gu_ok(err) || gu_variant_is_null(e)) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed"); gu_pool_free(tmp_pool); gu_pool_free(pool); return NULL; } gu_pool_free(tmp_pool); 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(pool)); jmethodID constrId = (*env)->GetMethodID(env, clazz, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V"); return (*env)->NewObject(env, clazz, constrId, jpool, NULL, p2l(gu_variant_to_ptr(e))); }
int main(int argc, char* argv[]) { // Set the character locale, so we can produce proper output. setlocale(LC_CTYPE, ""); if (argc != 2) { fprintf(stderr, "usage: %s pgf\n", argv[0]); return EXIT_FAILURE; } char* filename = argv[1]; GuPool* pool = gu_new_pool(); GuExn* err = gu_exn(NULL, type, pool); PgfPGF* pgf = pgf_read(filename, pool, err); int status = 0; if (!gu_ok(err)) { fprintf(stderr, "Reading PGF failed\n"); status = 1; goto fail_read; } GuOut* out = gu_file_out(stdout, pool); pgf_print(pgf, out, err); gu_out_flush(out, err); fail_read: gu_pool_free(pool); return status; }
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; }
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; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_io_InputStream_2(JNIEnv *env, jclass cls, jobject java_stream) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuInStream* jstream = jpgf_new_java_stream(env, java_stream, tmp_pool); if (!jstream) { gu_pool_free(pool); gu_pool_free(tmp_pool); return NULL; } GuIn* in = gu_new_in(jstream, tmp_pool); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); // Read the PGF grammar. PgfPGF* pgf = pgf_read_in(in, pool, tmp_pool, err); if (!gu_ok(err)) { 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)); }
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; }
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)); }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat) { GuPool* pool = gu_new_pool(); GuPool* out_pool = gu_new_pool(); GuString startCat = j2gu_string(env, jstartCat, pool); GuExn* err = gu_exn(pool); GuEnum* res = pgf_generate_all(get_ref(env, jpgf), startCat, err, pool, out_pool); if (res == NULL) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed"); gu_pool_free(pool); return NULL; } jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator"); jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V"); jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(out_pool), p2l(res)); return jexpiter; }
JNIEXPORT jobject JNICALL Java_org_grammaticalframework_pgf_PGF_getFunctionType(JNIEnv* env, jobject self, jstring jid) { PgfPGF* pgf = get_ref(env, self); GuPool* tmp_pool = gu_new_pool(); PgfCId id = j2gu_string(env, jid, tmp_pool); PgfType* tp = pgf_function_type(pgf, id); gu_pool_free(tmp_pool); if (tp == NULL) return NULL; jclass type_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Type"); jmethodID constrId = (*env)->GetMethodID(env, type_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;J)V"); jobject jtype = (*env)->NewObject(env, type_class, constrId, self, p2l(tp)); return jtype; }
JNIEXPORT jobjectArray JNICALL Java_org_grammaticalframework_sg_SG_readTriple(JNIEnv *env, jclass cls, jstring s) { GuPool* pool = gu_new_pool(); GuPool* tmp_pool = gu_local_pool(); GuString buf = j2gu_string(env, s, tmp_pool); GuIn* in = gu_data_in((uint8_t*) buf, strlen(buf), tmp_pool); GuExn* err = gu_exn(tmp_pool); const int len = 3; PgfExpr exprs[len]; int res = pgf_read_expr_tuple(in, 3, exprs, pool, err); if (!gu_ok(err) || res == 0) { throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed"); gu_pool_free(tmp_pool); gu_pool_free(pool); return NULL; } gu_pool_free(tmp_pool); 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(pool)); jclass expr_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Expr"); jmethodID expr_constrId = (*env)->GetMethodID(env, expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V"); jobjectArray array = (*env)->NewObjectArray(env, len, expr_class, NULL); for (int i = 0; i < len; i++) { jobject obj = (*env)->NewObject(env, expr_class, expr_constrId, jpool, NULL, p2l(gu_variant_to_ptr(exprs[i]))); (*env)->SetObjectArrayElement(env, array, i, obj); (*env)->DeleteLocalRef(env, obj); } return array; }
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; }
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 != 4) { fprintf(stderr, "usage: %s pgf cat from_lang\n", argv[0]); status = EXIT_FAILURE; goto fail; } char* filename = argv[1]; GuString cat = gu_str_string(argv[2], pool); GuString from_lang = gu_str_string(argv[3], pool); // Create an exception frame that catches all errors. GuExn* err = gu_new_exn(NULL, gu_kind(type), 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; } pgf_load_meta_child_probs(pgf, "../../../treebanks/PennTreebank/ParseEngAbs3.probs", pool, err); if (!gu_ok(err)) { fprintf(stderr, "Loading meta child probs failed\n"); status = EXIT_FAILURE; goto fail; } // Look up the source and destination concrete categories PgfConcr* from_concr = pgf_get_language(pgf, from_lang); if (!from_concr) { fprintf(stderr, "Unknown language\n"); status = EXIT_FAILURE; goto fail_concr; } // Register a callback for the literal category Symbol pgf_parser_add_literal(from_concr, gu_str_string("Symb", pool), &pgf_nerc_literal_callback); // We will keep the latest results in the 'ppool' and // we will iterate over them by using 'result'. GuPool* ppool = 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) { 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; } // We create a temporary pool for translating a single // sentence, so our memory usage doesn't increase over time. ppool = gu_new_pool(); GuReader *rdr = gu_string_reader(gu_str_string(line, ppool), ppool); PgfLexer *lexer = pgf_new_simple_lexer(rdr, ppool); pgf_print_chunks(from_concr, cat, lexer, ppool); // Free all resources allocated during parsing and linearization gu_pool_free(ppool); } fail_concr: fail: gu_pool_free(pool); return status; }
PgfToken pgf_lexer_read_token(PgfLexer *lexer, GuExn* err) { GuPool* tmp_pool = gu_new_pool(); GuStringBuf* buf = gu_string_buf(tmp_pool); GuWriter* wtr = gu_string_buf_writer(buf); while (iswspace(lexer->ucs)) { lexer->ucs = gu_read_ucs(lexer->rdr, err); if (gu_exn_is_raised(err)) goto stop; } if (iswalpha(lexer->ucs) || lexer->ucs == '\'' || lexer->ucs == '_') { int counter = 0; do { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; counter++; pgf_lexer_read_ucs(lexer, err); if (lexer->ucs == '.' && counter < 4) { // perhaps an abreviation gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; counter = 0; pgf_lexer_read_ucs(lexer, err); } } while (iswalnum(lexer->ucs) || lexer->ucs == '\'' || lexer->ucs == '_'); } else if (iswdigit(lexer->ucs) || lexer->ucs == '-') { if (lexer->ucs == '-') { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; pgf_lexer_read_ucs(lexer, err); if (!iswdigit(lexer->ucs)) goto stop; } do { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; pgf_lexer_read_ucs(lexer, err); } while (iswdigit(lexer->ucs)); if (lexer->ucs == '.') { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; pgf_lexer_read_ucs(lexer, err); while (iswdigit(lexer->ucs)) { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; pgf_lexer_read_ucs(lexer, err); } } } else { gu_ucs_write(lexer->ucs, wtr, err); if (gu_exn_is_raised(err)) goto stop; pgf_lexer_read_ucs(lexer, err); } stop: lexer->tok = gu_string_buf_freeze(buf, lexer->pool); gu_pool_free(tmp_pool); return lexer->tok; }
JNIEXPORT jlong JNICALL Java_org_grammaticalframework_pgf_Pool_alloc(JNIEnv* env, jclass clazz) { return p2l(gu_new_pool()); }
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 < 4 || argc > 5) { fprintf(stderr, "usage: %s pgf-file start-cat cnc-lang [heuristics]\n(0.0 <= heuristics < 1.0, default: 0.95)\n", argv[0]); status = EXIT_FAILURE; goto fail; } char* filename = argv[1]; GuString cat = argv[2]; GuString lang = argv[3]; double heuristics = 0.95; if (argc == 5) { heuristics = atof(argv[4]); } // Create an exception frame that catches all errors. GuExn* err = gu_new_exn(pool); clock_t start = clock(); // 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* concr = pgf_get_language(pgf, lang); if (!concr) { fprintf(stderr, "Unknown language\n"); status = EXIT_FAILURE; goto fail; } clock_t end = clock(); double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fprintf(stderr, "(%.0f ms) Ready to parse [heuristics=%.2f]!\n", 1000.0 * cpu_time_used, heuristics); // 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; // The interactive PARSING loop. // XXX: This currently reads stdin directly, so it doesn't support // encodings properly. TODO: use a locale reader for input for (int ctr = 0; true; ctr++) { // We release the last results if (ppool != NULL) { gu_pool_free(ppool); ppool = NULL; } /* 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 -> skip continue; } // 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); PgfCallbacksMap* callbacks = pgf_new_callbacks_map(concr, ppool); GuEnum* result = pgf_parse_with_heuristics(concr, cat, line, heuristics, callbacks, parse_err, ppool, ppool); PgfExprProb* ep = NULL; if (gu_ok(parse_err)) ep = gu_next(result, PgfExprProb*, ppool); clock_t end = clock(); double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; gu_printf(out, err, "%d (%.0f ms): ", ctr, 1000.0 * cpu_time_used); if (ep != NULL) { gu_printf(out, err, "[%.4f] (", ep->prob); pgf_print_expr(ep->expr, NULL, 0, out, err); gu_printf(out, err, ")\n"); } else { gu_printf(out, err, "---\n"); } gu_out_flush(out, err); } fail: gu_pool_free(pool); return status; }
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 || argc > 6) { fprintf(stderr, "usage: %s pgf cat from-lang to-lang [probs-file]\n", argv[0]); status = EXIT_FAILURE; goto fail; } char* filename = argv[1]; GuString cat = gu_str_string(argv[2], pool); GuString from_lang = gu_str_string(argv[3], pool); GuString to_lang = gu_str_string(argv[4], pool); // Create an exception frame that catches all errors. GuExn* err = gu_new_exn(NULL, gu_kind(type), 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; } if (argc == 6) { char* meta_probs_filename = argv[5]; pgf_load_meta_child_probs(pgf, meta_probs_filename, pool, err); if (!gu_ok(err)) { fprintf(stderr, "Loading meta child probs 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 pgf_parser_add_literal(from_concr, gu_str_string("Symb", pool), &pgf_nerc_literal_callback); // Create an output stream for stdout GuOut* out = gu_file_out(stdout, pool); // Locale-encoding writers are currently unsupported // GuWriter* wtr = gu_locale_writer(out, pool); // Use a writer with hard-coded utf-8 encoding for now. GuWriter* wtr = gu_new_utf8_writer(out, 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, wtr, 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(); GuReader *rdr = gu_string_reader(gu_str_string(line, ppool), ppool); PgfLexer *lexer = pgf_new_lexer(rdr, ppool); clock_t start = clock(); GuEnum* result = pgf_parse(from_concr, cat, lexer, ppool); if (result == NULL) { PgfToken tok = pgf_lexer_current_token(lexer); if (gu_string_eq(tok, gu_empty_string)) gu_puts("Couldn't begin parsing", wtr, err); else { gu_puts("Unexpected token: \"", wtr, err); gu_string_write(tok, wtr, err); gu_puts("\"\n", wtr, 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, wtr, err, ppool); continue; fail_parse: // Free all resources allocated during parsing and linearization gu_pool_free(ppool); ppool = NULL; result = NULL; }
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; }
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; } }