Commit 5a502eb2 authored by Bruno Guillaume's avatar Bruno Guillaume

add function Graph.to_json

parent 04c5dc27
...@@ -381,6 +381,9 @@ module G_fs = struct ...@@ -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))) (fun feat1 feat2 -> Pervasives.compare (String.lowercase_ascii (G_feature.get_name feat1)) (String.lowercase_ascii (G_feature.get_name feat2)))
reduced_t in reduced_t in
List.map (fun (fn, fv) -> (fn, string_of_value fv)) ud_ordering 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 *) end (* module G_fs *)
(* ================================================================================ *) (* ================================================================================ *)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
(* Authors: see AUTHORS file *) (* Authors: see AUTHORS file *)
(**********************************************************************************) (**********************************************************************************)
open Yojson.Basic
open Conll open Conll
open Grew_base open Grew_base
...@@ -51,7 +52,7 @@ module G_fs: sig ...@@ -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_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_string: ?exclude: string list -> t -> string
val to_conll: ?exclude: string list -> t -> (string * string) list val to_conll: ?exclude: string list -> t -> (string * string) list
val to_json: t -> json
val to_string: t -> string val to_string: t -> string
val build: ?domain:Domain.t -> Ast.feature list -> t val build: ?domain:Domain.t -> Ast.feature list -> t
......
...@@ -712,6 +712,26 @@ module G_graph = struct ...@@ -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 } | Some new_fs -> Some { graph with map = Gid_map.add node_id (G_node.set_fs new_fs node) graph.map }
| None -> None | 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 to_gr graph =
let domain = get_domain graph in let domain = get_domain graph in
......
...@@ -194,6 +194,7 @@ module G_graph: sig ...@@ -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_dep: ?filter: (string -> bool) -> ?main_feat:string -> ?deco:G_deco.t -> t -> string
val to_conll: t -> Conll.t val to_conll: t -> Conll.t
val to_conll_string: t -> string val to_conll_string: t -> string
val to_json: t -> json
val cast: ?domain:Domain.t -> t -> t val cast: ?domain:Domain.t -> t -> t
end (* module G_graph *) end (* module G_graph *)
......
...@@ -103,7 +103,7 @@ end ...@@ -103,7 +103,7 @@ end
module Matching = struct module Matching = struct
type t = Grew_rule.Rule.matching 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 end
(* ==================================================================================================== *) (* ==================================================================================================== *)
...@@ -205,6 +205,9 @@ module Graph = struct ...@@ -205,6 +205,9 @@ module Graph = struct
let to_gr graph = let to_gr graph =
Libgrew.handle ~name:"Graph.to_gr" (fun () -> Grew_graph.G_graph.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 = let to_conll graph =
Libgrew.handle ~name:"Graph.to_conll" (fun () -> Grew_graph.G_graph.to_conll graph) () Libgrew.handle ~name:"Graph.to_conll" (fun () -> Grew_graph.G_graph.to_conll graph) ()
...@@ -294,9 +297,11 @@ module Grs = struct ...@@ -294,9 +297,11 @@ module Grs = struct
Grew_grs.Grs.domain grs Grew_grs.Grs.domain grs
) () ) ()
let to_json t = let to_json grs =
let json = Grew_grs.Grs.to_json t in Libgrew.handle ~name:"Grs.to_json"
Yojson.Basic.pretty_to_string json (fun () ->
Grew_grs.Grs.to_json grs
) ()
let get_strat_list grs = let get_strat_list grs =
Libgrew.handle ~name:"Grs.get_strat_list" Libgrew.handle ~name:"Grs.get_strat_list"
......
...@@ -55,7 +55,7 @@ end ...@@ -55,7 +55,7 @@ end
module Matching: sig module Matching: sig
type t 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 end
(* ==================================================================================================== *) (* ==================================================================================================== *)
...@@ -87,6 +87,7 @@ module Graph : sig ...@@ -87,6 +87,7 @@ module Graph : sig
val of_conll: ?domain:Domain.t -> Conll.t -> t val of_conll: ?domain:Domain.t -> Conll.t -> t
val of_json: json -> t val of_json: json -> t
val to_json: t -> json
val of_brown: ?domain:Domain.t -> ?sentid:string -> string -> t val of_brown: ?domain:Domain.t -> ?sentid:string -> string -> t
...@@ -148,7 +149,7 @@ module Grs : sig ...@@ -148,7 +149,7 @@ module Grs : sig
val domain: t -> Domain.t option val domain: t -> Domain.t option
val to_json: t -> string val to_json: t -> json
val get_strat_list: t -> string list val get_strat_list: t -> string list
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment