diff --git a/maca_common/include/json_parser.h b/maca_common/include/json_parser.h index 116c26b23838423fdc843e579903890177af9a08..6b4814d550894227e14ec7bf864ec9aa499cc26f 100644 --- a/maca_common/include/json_parser.h +++ b/maca_common/include/json_parser.h @@ -52,9 +52,12 @@ typedef struct { }json_parser_ctx; +json_struct *json_parse(char *filename); +json_struct *json_parse_full(char *filename, int trace_xml); json_struct *structure(json_parser_ctx *ctx); json_parser_ctx *json_parser_init(char *filename); +json_parser_ctx *json_parser_init_full(char *filename, int trace_xml); diff --git a/maca_common/include/json_tree.h b/maca_common/include/json_tree.h index 180eab9fd93683fd3b925e4099dd135f25e4b9eb..41facd39ff2497d9e02ed3f1051cdd4a36f4ac06 100644 --- a/maca_common/include/json_tree.h +++ b/maca_common/include/json_tree.h @@ -1,11 +1,11 @@ #ifndef __JSON_TREE__ #define __JSON_TREE__ -#define JSON_OBJECT 1 -#define JSON_LIST 2 -#define JSON_STRING 3 -#define JSON_NUMBER 4 -#define JSON_CONSTANT 5 +#define JSON_LIST 1 +#define JSON_STRING 2 +#define JSON_NUMBER 3 +#define JSON_CONSTANT 4 +#define JSON_AVL 5 /* attribute value list */ #define JSON_CONST_TRUE 1 #define JSON_CONST_FALSE 2 @@ -27,10 +27,11 @@ struct _json_struct_ { struct _json_struct_ *next; }; +/* typedef struct _json_object_ { json_attr_val *list; } json_object; - +*/ /* struct _json_struct_list_ { int nbelem; @@ -48,12 +49,13 @@ json_struct *json_new_string(char *string); json_struct *json_new_number(float number); json_struct *json_new_constant(int constant); json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next); -json_struct *json_new_object(json_attr_val *avl); +//json_struct *json_new_object(json_attr_val *avl); +json_struct *json_new_avl(json_attr_val *avl); json_struct *json_new_list(json_struct *s); void json_print_struct(FILE *f, json_struct *s); -void json_print_object(FILE *f, json_struct *s); +void json_print_avl(FILE *f, json_struct *s); void json_print_list(FILE *f, json_struct *s); void json_print_string(FILE *f, json_struct *s); void json_print_number(FILE *f, json_struct *s); diff --git a/maca_common/src/json_parser.c b/maca_common/src/json_parser.c index 26e0aebf88ea0de48b7954f7335a446d027ad48a..6f8875a793bc40b4a9c919dfaa2d0bc0fc7e6272 100644 --- a/maca_common/src/json_parser.c +++ b/maca_common/src/json_parser.c @@ -20,11 +20,31 @@ void initialise_premiers(json_parser_ctx *ctx); void initialise_suivants(json_parser_ctx *ctx); int yylex(json_parser_ctx *ctx); + +json_struct *json_parse(char *filename) +{ + json_parser_ctx *ctx=json_parser_init(filename); + return structure(ctx); +} + +json_struct *json_parse_full(char *filename, int trace_xml) +{ + json_parser_ctx *ctx=json_parser_init_full(filename, trace_xml); + json_struct *root = structure(ctx); + /* il faut libérer ctx */ + return root; +} + json_parser_ctx *json_parser_init(char *filename) +{ + return json_parser_init_full(filename, 0); +} + +json_parser_ctx *json_parser_init_full(char *filename, int trace_xml) { json_parser_ctx *ctx = (json_parser_ctx *) malloc(sizeof(json_parser_ctx)); ctx->nb_ligne = 1; - ctx->trace_xml = 0; + ctx->trace_xml = trace_xml; ctx->indent_xml = 0; ctx->indent_step = 1; initialise_premiers(ctx); @@ -413,7 +433,7 @@ json_struct *structure(json_parser_ctx *ctx) if(est_premier(ctx, ctx->uc, _object_)) { json_attr_val *avl = object(ctx); affiche_balise_fermante(ctx, __FUNCTION__); - return json_new_object(avl); + return json_new_avl(avl); } if(ctx->uc == STRING){ diff --git a/maca_common/src/json_tree.c b/maca_common/src/json_tree.c index 74a9f46b8e131e0f871f412a85b265547e0f48ab..6f49a64c43dce19c59403b87684b1b6c9a67aa81 100644 --- a/maca_common/src/json_tree.c +++ b/maca_common/src/json_tree.c @@ -50,9 +50,9 @@ json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next return av; } -json_struct *json_new_object(json_attr_val *avl) +json_struct *json_new_avl(json_attr_val *avl) { - json_struct *c = json_new_struct(JSON_OBJECT); + json_struct *c = json_new_struct(JSON_AVL); c->u.attr_val_list = avl; return c; } @@ -60,14 +60,14 @@ json_struct *json_new_object(json_attr_val *avl) void json_print_struct(FILE *f, json_struct *s) { if(s == NULL) return; - if(s->type == JSON_OBJECT) json_print_object(f, s); + if(s->type == JSON_AVL) json_print_avl(f, s); if(s->type == JSON_LIST) json_print_list(f, s); if(s->type == JSON_STRING) json_print_string(f, s); if(s->type == JSON_NUMBER) json_print_number(f, s); if(s->type == JSON_CONSTANT) json_print_constant(f, s); } -void json_print_object(FILE *f, json_struct *s) +void json_print_avl(FILE *f, json_struct *s) { json_attr_val *avl; int first = 1; @@ -97,7 +97,10 @@ void json_print_list(FILE *f, json_struct *s) void json_print_string(FILE *f, json_struct *s) { - fprintf(f, "%s", s->u.string); + if(s->u.string) + fprintf(f, "%s", s->u.string); + else + fprintf(f, ""); } void json_print_number(FILE *f, json_struct *s) @@ -152,7 +155,7 @@ void json_free_attr_val_list(json_attr_val *avl) void json_free_struct(json_struct *s) { if(s == NULL) return; - if(s->type == JSON_OBJECT) json_free_attr_val_list(s->u.attr_val_list); + if(s->type == JSON_AVL) json_free_attr_val_list(s->u.attr_val_list); if(s->type == JSON_LIST) json_free_list(s->u.first); if(s->type == JSON_STRING) free(s->u.string); free(s); diff --git a/maca_tools/src/json2mcf.c b/maca_tools/src/json2mcf.c index e65ac93ccc5bfa27924fc632b10349a79f2c9c8f..c19d3b78e6ded56a8ba2eec5825d12cf0f048bc9 100644 --- a/maca_tools/src/json2mcf.c +++ b/maca_tools/src/json2mcf.c @@ -223,18 +223,15 @@ int main(int argc, char *argv[]) int index_first_word; int index_last_word; int sentence_nb = 0; - json_parser_ctx *parser_ctx = NULL; json_struct *root = NULL; json_struct *document = NULL; json_attr_val *avl = NULL; json2mcf_check_options(ctx); wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct); - - parser_ctx = json_parser_init(ctx->json_filename); - root = structure(parser_ctx); - - if(root->type != JSON_OBJECT){ + root = json_parse_full(ctx->json_filename, 0); + + if(root->type != JSON_AVL){ fprintf(stderr, "erreur le json doit être un objet\n"); exit(1); } @@ -245,15 +242,9 @@ int main(int argc, char *argv[]) } } - /*json_print_struct(stdout, root); - json_free_struct(root);*/ - - - + //json_print_struct(stdout, root); + json_free_struct(root); json2mcf_context_free(ctx); - - - return 0; } diff --git a/maca_tools/src/mcf2json.c b/maca_tools/src/mcf2json.c index 0f1e76b19202fde242b0b93ec3403f736d1b493f..ba56ded57b6ce1262a9d87265f8ce9d7e004eb16 100644 --- a/maca_tools/src/mcf2json.c +++ b/maca_tools/src/mcf2json.c @@ -137,7 +137,7 @@ void print_header(FILE *output_file, mcd *mcd_struct) dico *dico_label = mcd_struct->dico_array[label_col]; int i; - printf("dico label : %d dico = %d\n", label_col, dico_label); + // printf("dico label : %d dico = %d\n", label_col, dico_label); fprintf(output_file, "{\n"); fprintf(output_file, "\"header\":{\n"); @@ -154,7 +154,7 @@ void print_header(FILE *output_file, mcd *mcd_struct) for(i=0; i < dico_label->nbelem; i++){ fprintf(output_file, " %s", dico_label->array[i]); } - fprintf(output_file, "\",\n"); + fprintf(output_file, "\"\n"); fprintf(output_file, "},\n"); @@ -163,6 +163,8 @@ void print_header(FILE *output_file, mcd *mcd_struct) fprintf(output_file, "\"time_start\": \"\",\n"); fprintf(output_file, "\"time_end\": \"\"\n"); fprintf(output_file, "},\n"); + fprintf(output_file, "\"documents\": ["); + } void print_link(FILE *output_file, word *w, int index, int gov_col, int label_col) @@ -332,7 +334,6 @@ int main(int argc, char *argv[]) wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct); print_header(output_file, ctx->mcd_struct); - fprintf(output_file, "\"documents\": ["); do{ w = word_buffer_b0(wb); if(new_sentence){