summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf/reader.c
diff options
context:
space:
mode:
authorkr.angelov <kr.angelov@gmail.com>2012-06-12 09:29:51 +0000
committerkr.angelov <kr.angelov@gmail.com>2012-06-12 09:29:51 +0000
commitb27a440ef32758df71639e29d81466f1afaf2c90 (patch)
tree5e5a78006cf5e25db3b01d5d9db0548f48d466d3 /src/runtime/c/pgf/reader.c
parentd989005e019e81599620012c80d13960ae8cb4a1 (diff)
now the robust parser is purely top-down and the meta rules compete on a fair basis with the grammar rules
Diffstat (limited to 'src/runtime/c/pgf/reader.c')
-rw-r--r--src/runtime/c/pgf/reader.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/runtime/c/pgf/reader.c b/src/runtime/c/pgf/reader.c
index 1fee45f83..08cc16096 100644
--- a/src/runtime/c/pgf/reader.c
+++ b/src/runtime/c/pgf/reader.c
@@ -30,6 +30,7 @@
#include <gu/exn.h>
#include <gu/utf8.h>
#include <math.h>
+#include <stdio.h>
#define GU_LOG_ENABLE
#include <gu/log.h>
@@ -656,6 +657,8 @@ pgf_compute_meta_probs(GuMapItor* fn, const void* key, void* value, GuExn* err)
mass += cat->functions[i].prob;
}
cat->meta_prob = - log(fabs(1 - mass));
+ cat->meta_token_prob = INFINITY;
+ cat->meta_child_probs = NULL;
}
static void
@@ -936,3 +939,51 @@ pgf_read(GuIn* in, GuPool* pool, GuExn* err)
gu_return_on_exn(err, NULL);
return pgf;
}
+
+bool
+pgf_load_meta_child_probs(PgfPGF* pgf, const char* fpath, GuPool* pool)
+{
+ FILE *fp = fopen(fpath, "r");
+ if (!fp)
+ return false;
+
+ GuPool* tmp_pool = gu_new_pool();
+
+ for (;;) {
+ char cat1_s[21];
+ char cat2_s[21];
+ float prob;
+
+ if (fscanf(fp, "%20s\t%20s\t%f", cat1_s, cat2_s, &prob) < 3)
+ break;
+
+ prob = - log(prob);
+
+ GuString cat1 = gu_str_string(cat1_s, tmp_pool);
+ PgfCat* abscat1 =
+ gu_map_get(pgf->abstract.cats, &cat1, PgfCat*);
+ if (abscat1 == NULL)
+ return false;
+
+ if (strcmp(cat2_s, "_") == 0) {
+ abscat1->meta_token_prob = prob;
+ } else {
+ GuString cat2 = gu_str_string(cat2_s, tmp_pool);
+ PgfCat* abscat2 = gu_map_get(pgf->abstract.cats, &cat2, PgfCat*);
+ if (abscat2 == NULL)
+ return false;
+
+ if (abscat1->meta_child_probs == NULL) {
+ abscat1->meta_child_probs =
+ gu_map_type_new(PgfMetaChildMap, pool);
+ }
+
+ gu_map_put(abscat1->meta_child_probs, abscat2, float, prob);
+ }
+ }
+
+ gu_pool_free(tmp_pool);
+
+ fclose(fp);
+ return true;
+}