Commit 04c5dc27 authored by Bruno Guillaume's avatar Bruno Guillaume

add function Graph.of_json

parent 34246dc3
...@@ -288,7 +288,6 @@ module G_graph = struct ...@@ -288,7 +288,6 @@ module G_graph = struct
else gr_ast.Ast.nodes else gr_ast.Ast.nodes
and full_edge_list = gr_ast.Ast.edges in and full_edge_list = gr_ast.Ast.edges in
let rec loop already_bound index prec = function let rec loop already_bound index prec = function
| [] -> (Gid_map.empty,[]) | [] -> (Gid_map.empty,[])
...@@ -329,6 +328,31 @@ module G_graph = struct ...@@ -329,6 +328,31 @@ module G_graph = struct
highest_index = (List.length full_node_list) -1 highest_index = (List.length full_node_list) -1
} }
(* -------------------------------------------------------------------------------- *)
let of_json = function
| `Assoc (l : (string * Yojson.Basic.json) list) ->
let (ast_node_list, ast_edge_list) = List.fold_left
(fun (acc_node, acc_edge) -> function
| (id, `List [`Assoc feat_json_list; `List succ]) ->
let fs = List.map (function
| (feat_name, `String value) -> ({Ast.name= feat_name; kind = Ast.Equality [value]}, Loc.empty)
| _ -> Error.build "[Graph.of_json] not an valid feature structure"
) feat_json_list in
let new_edges = List.map
(function
| `List [`String rel; `String tar] -> ({Ast.edge_id=None; edge_label_cst=Ast.Pos_list [rel]; src=id; tar},Loc.empty)
| _ -> Error.build "[Graph.of_json] not an valid succ list"
) succ in
(
({ Ast.node_id=id; position=None; fs}, Loc.empty) :: acc_node,
new_edges @ acc_edge
)
| _ -> Error.build "[Graph.of_json] not an assoc list"
) ([],[]) l in
let graph_ast = { Ast.meta=[]; nodes=ast_node_list; edges=ast_edge_list}
in build ~grewpy:true graph_ast
| _ -> Error.build "[Graph.of_json] not an assoc list"
(* -------------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------------- *)
let of_conll ?domain conll = let of_conll ?domain conll =
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
(**********************************************************************************) (**********************************************************************************)
open Conll open Conll
open Yojson.Basic
open Grew_base open Grew_base
open Grew_types open Grew_types
...@@ -104,6 +105,7 @@ module G_graph: sig ...@@ -104,6 +105,7 @@ module G_graph: sig
val build: ?domain:Domain.t -> ?grewpy: bool -> Ast.gr -> t val build: ?domain:Domain.t -> ?grewpy: bool -> Ast.gr -> t
val of_conll: ?domain:Domain.t -> Conll.t -> t val of_conll: ?domain:Domain.t -> Conll.t -> t
val of_json: json -> t
(** input : "Le/DET/le petit/ADJ/petit chat/NC/chat dort/V/dormir ./PONCT/." (** input : "Le/DET/le petit/ADJ/petit chat/NC/chat dort/V/dormir ./PONCT/."
It supposes that "SUC" is defined in current relations *) It supposes that "SUC" is defined in current relations *)
......
...@@ -559,26 +559,15 @@ module Rule = struct ...@@ -559,26 +559,15 @@ module Rule = struct
} }
let to_python pattern graph m = let to_python pattern graph m =
let node_name gid = G_node.get_name gid (G_graph.find gid graph) in
let node_name gid = let nodes = Pid_map.fold (fun pid gid acc ->
let gnode = G_graph.find gid graph in let pnode = P_graph.find pid (fst pattern).graph in
G_node.get_name gid gnode in (P_node.get_name pnode, `String (node_name gid))::acc
let buff = Buffer.create 32 in ) m.n_match [] in
bprintf buff "%s" "{ "; let edges = List.map (fun (id, (src,lab,tar)) ->
(id, `String (sprintf "%s/%s/%s" (node_name src) (Label.to_string lab) (node_name tar)))
Pid_map.iter ) m.e_match in
(fun pid gid -> `Assoc ( nodes @ edges)
let pnode = P_graph.find pid (fst pattern).graph in
bprintf buff "\"%s\":\"%s\", " (P_node.get_name pnode) (node_name gid)
) m.n_match;
List.iter
(fun (id, (src,lab,tar)) ->
bprintf buff "\"%s\":\"%s/%s/%s\", " id (node_name src) (Label.to_string lab) (node_name tar)
) m.e_match;
bprintf buff "%s" "}";
Buffer.contents buff
let node_matching pattern graph { n_match } = let node_matching pattern graph { n_match } =
Pid_map.fold Pid_map.fold
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
(* Authors: see AUTHORS file *) (* Authors: see AUTHORS file *)
(**********************************************************************************) (**********************************************************************************)
open Yojson.Basic
open Grew_base open Grew_base
open Grew_types open Grew_types
open Grew_domain open Grew_domain
...@@ -101,7 +102,7 @@ module Rule : sig ...@@ -101,7 +102,7 @@ module Rule : sig
val pid_name_list: pattern -> Id.name list val pid_name_list: pattern -> Id.name list
val to_python: pattern -> G_graph.t -> matching -> string val to_python: pattern -> G_graph.t -> matching -> json
val build_pattern: ?domain:Domain.t -> Ast.pattern -> pattern val build_pattern: ?domain:Domain.t -> Ast.pattern -> pattern
......
...@@ -190,6 +190,9 @@ module Graph = struct ...@@ -190,6 +190,9 @@ module Graph = struct
Sentence.fr_clean_spaces (String.concat " " word_list) Sentence.fr_clean_spaces (String.concat " " word_list)
) () ) ()
let of_json json =
Libgrew.handle ~name:"Graph.of_json" (fun () -> Grew_graph.G_graph.of_json json) ()
let of_brown ?domain ?sentid brown = let of_brown ?domain ?sentid brown =
Libgrew.handle ~name:"Graph.of_brown" (fun () -> Grew_graph.G_graph.of_brown ?domain ?sentid brown) () Libgrew.handle ~name:"Graph.of_brown" (fun () -> Grew_graph.G_graph.of_brown ?domain ?sentid brown) ()
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
(**********************************************************************************) (**********************************************************************************)
open Conll open Conll
open Yojson.Basic
(* ==================================================================================================== *) (* ==================================================================================================== *)
(** {2 General definitions} *) (** {2 General definitions} *)
...@@ -54,7 +55,7 @@ end ...@@ -54,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 -> string val to_python: Pattern.t -> Grew_graph.G_graph.t -> t -> json
end end
(* ==================================================================================================== *) (* ==================================================================================================== *)
...@@ -85,13 +86,14 @@ module Graph : sig ...@@ -85,13 +86,14 @@ 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_brown: ?domain:Domain.t -> ?sentid:string -> string -> t val of_brown: ?domain:Domain.t -> ?sentid:string -> string -> t
val of_pst: ?domain:Domain.t -> string -> t val of_pst: ?domain:Domain.t -> string -> t
val sentence_of_pst: ?domain:Domain.t -> string -> string val sentence_of_pst: ?domain:Domain.t -> string -> string
val to_sentence: ?main_feat:string -> ?deco:Deco.t -> t -> string val to_sentence: ?main_feat:string -> ?deco:Deco.t -> t -> string
val to_dot : ?main_feat:string -> ?deco:Deco.t -> t -> string val to_dot : ?main_feat:string -> ?deco:Deco.t -> t -> string
......
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