diff --git a/src/grew_fs.ml b/src/grew_fs.ml index 324ecaf88d0db7eef008c2e60b630f1f1112e853..0f8d36a9549f57b01db1efbc24643c1f4bec9184 100644 --- a/src/grew_fs.ml +++ b/src/grew_fs.ml @@ -381,6 +381,9 @@ module G_fs = struct (fun feat1 feat2 -> Pervasives.compare (String.lowercase_ascii (G_feature.get_name feat1)) (String.lowercase_ascii (G_feature.get_name feat2))) reduced_t in List.map (fun (fn, fv) -> (fn, string_of_value fv)) ud_ordering + + let to_json t = `Assoc (List.map (fun (fn, fv) -> (fn, `String (string_of_value fv))) t) + end (* module G_fs *) (* ================================================================================ *) diff --git a/src/grew_fs.mli b/src/grew_fs.mli index 63b32fa776138efa52271b040af09196c4db667c..18945f52e67a07a463200dd6e7c22f442215fb34 100644 --- a/src/grew_fs.mli +++ b/src/grew_fs.mli @@ -8,6 +8,7 @@ (* Authors: see AUTHORS file *) (**********************************************************************************) +open Yojson.Basic open Conll open Grew_base @@ -51,7 +52,7 @@ module G_fs: sig val to_dep: ?decorated_feat:(string * string list) -> ?position:float -> ?main_feat: string -> ?filter: (string -> bool) -> t -> string val to_conll_string: ?exclude: string list -> t -> string val to_conll: ?exclude: string list -> t -> (string * string) list - + val to_json: t -> json val to_string: t -> string val build: ?domain:Domain.t -> Ast.feature list -> t diff --git a/src/grew_graph.ml b/src/grew_graph.ml index ba45e1d93a33dea5b3279ff1a266fa23db3f5525..3dc3b9257fb17630e308d6d693f89953b1095c1d 100644 --- a/src/grew_graph.ml +++ b/src/grew_graph.ml @@ -712,6 +712,26 @@ module G_graph = struct | Some new_fs -> Some { graph with map = Gid_map.add node_id (G_node.set_fs new_fs node) graph.map } | None -> None + (* -------------------------------------------------------------------------------- *) + let to_json graph = + let domain = get_domain graph in + + let gr_id id = G_node.get_name id (Gid_map.find id graph.map) in + + let nodes = Gid_map.fold + (fun id node acc -> + let node_id = gr_id id + and fs = G_node.get_fs node + and succ = + Massoc_gid.fold + (fun acc tar edge -> + (`List [`String (G_edge.to_string ?domain edge); `String (gr_id tar)]) :: acc + ) [] (G_node.get_next node) in + (node_id,`List [G_fs.to_json fs; `List succ])::acc + ) graph.map [] in + + `Assoc nodes + (* -------------------------------------------------------------------------------- *) let to_gr graph = let domain = get_domain graph in diff --git a/src/grew_graph.mli b/src/grew_graph.mli index 5a895e16388b9d3bb6cf73a2624e3519578e947d..6d2a4a875df0028270356b84121e3d5a1aa77042 100644 --- a/src/grew_graph.mli +++ b/src/grew_graph.mli @@ -194,6 +194,7 @@ module G_graph: sig val to_dep: ?filter: (string -> bool) -> ?main_feat:string -> ?deco:G_deco.t -> t -> string val to_conll: t -> Conll.t val to_conll_string: t -> string + val to_json: t -> json val cast: ?domain:Domain.t -> t -> t end (* module G_graph *) diff --git a/src/libgrew.ml b/src/libgrew.ml index c7aea4ce2f08ab54a09b025369a92e5f7e0dec79..666211a11c02ff966f1c3af703e85302a7fe5c37 100644 --- a/src/libgrew.ml +++ b/src/libgrew.ml @@ -103,7 +103,7 @@ end module Matching = struct type t = Grew_rule.Rule.matching - let to_python pattern graph t = Grew_rule.Rule.to_python pattern graph t + let to_json pattern graph t = Grew_rule.Rule.to_python pattern graph t end (* ==================================================================================================== *) @@ -205,6 +205,9 @@ module Graph = struct let to_gr graph = Libgrew.handle ~name:"Graph.to_gr" (fun () -> Grew_graph.G_graph.to_gr graph) () + let to_json graph = + Libgrew.handle ~name:"Graph.to_json" (fun () -> Grew_graph.G_graph.to_json graph) () + let to_conll graph = Libgrew.handle ~name:"Graph.to_conll" (fun () -> Grew_graph.G_graph.to_conll graph) () @@ -294,9 +297,11 @@ module Grs = struct Grew_grs.Grs.domain grs ) () - let to_json t = - let json = Grew_grs.Grs.to_json t in - Yojson.Basic.pretty_to_string json + let to_json grs = + Libgrew.handle ~name:"Grs.to_json" + (fun () -> + Grew_grs.Grs.to_json grs + ) () let get_strat_list grs = Libgrew.handle ~name:"Grs.get_strat_list" diff --git a/src/libgrew.mli b/src/libgrew.mli index 2409f613441763672178873257a56c16e4a03ca5..463ecfa492410401172d7fb53f29cb35ef74d91a 100644 --- a/src/libgrew.mli +++ b/src/libgrew.mli @@ -55,7 +55,7 @@ end module Matching: sig type t - val to_python: Pattern.t -> Grew_graph.G_graph.t -> t -> json + val to_json: Pattern.t -> Grew_graph.G_graph.t -> t -> json end (* ==================================================================================================== *) @@ -87,6 +87,7 @@ module Graph : sig val of_conll: ?domain:Domain.t -> Conll.t -> t val of_json: json -> t + val to_json: t -> json val of_brown: ?domain:Domain.t -> ?sentid:string -> string -> t @@ -148,7 +149,7 @@ module Grs : sig val domain: t -> Domain.t option - val to_json: t -> string + val to_json: t -> json val get_strat_list: t -> string list end