Commit 01a70f07 authored by bguillaum's avatar bguillaum

raw output (cont) and No_domain

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/semagramme/libcaml-grew/trunk@7532 7838e531-6607-4d57-9587-6c381814729c
parent 59cc5987
......@@ -10,34 +10,41 @@ module Label = struct
type decl = string * string option
(* Global names and colors are recorded in two aligned arrays *)
let full = ref [||]
let full = ref None
let colors = ref [||]
(* Internal representation of labels *)
type t =
| Global of int
| Local of int
| No_domain of string
(* [init string_edge_list] updates global arrays [full] and [colors] *)
let init string_edge_list =
let slist = List.sort (fun (x,_) (y,_) -> compare x y) string_edge_list in
let (labels, cols) = List.split slist in
full := Array.of_list labels;
full := Some (Array.of_list labels);
colors := Array.of_list cols
let to_string ?(locals=[||]) = function
| Global i -> !full.(i)
| Local i -> fst locals.(i)
let to_string ?(locals=[||]) t =
match (!full, t) with
| (None, No_domain s) -> s
| (Some table, Global i) -> table.(i)
| (Some _, Local i) -> fst locals.(i)
| _ -> Error.bug "[Label.to_string] inconsistent data"
let to_int = function
| Global i -> Some i
| Local _ -> None
| _ -> None
let from_string ?loc ?(locals=[||]) string =
try Global (Id.build ?loc string !full)
with Not_found ->
try Local (Array_.dicho_find_assoc string locals)
with Not_found -> Error.build "[Label.from_string] unknown edge label '%s'" string
match !full with
| None -> No_domain string
| Some table ->
try Global (Id.build ?loc string table)
with Not_found ->
try Local (Array_.dicho_find_assoc string locals)
with Not_found -> Error.build "[Label.from_string] unknown edge label '%s'" string
let get_color = function
| Global l -> !colors.(l)
......
......@@ -230,13 +230,14 @@ module G_graph = struct
| _::t -> loop (n+1) t
in loop 0
let to_raw t =
let to_raw graph =
let node_list = ref [] in
Gid_map.iter
(fun pid node ->
node_list := (pid, G_fs.to_raw (G_node.get_fs node)) :: !node_list
)
t;
graph.map;
node_list := List.sort (fun x y -> Gid.compare (fst x) (fst y)) !node_list;
let search pid = list_search (fun (x,_) -> x=pid) !node_list in
let edge_list = ref [] in
Gid_map.iter
......@@ -247,10 +248,8 @@ module G_graph = struct
)
(G_node.get_next node)
)
t;
(List.map snd !node_list, !edge_list)
let equals t t' = Gid_map.equal (fun node1 node2 -> node1 = node2) t t'
graph.map;
(graph.meta, List.map snd !node_list, !edge_list)
(* is there an edge e out of node i ? *)
let edge_out graph node_id p_edge =
......
......@@ -76,7 +76,10 @@ module G_graph: sig
[Not_found] is raised if [gid] is not defined in [graph]. *)
val find: Gid.t -> t -> G_node.t
val to_raw: t -> ((string * string) list list) * (int * string * int) list
val to_raw: t ->
(string * string) list *
(string * string) list list *
(int * string * int) list
val equals: t -> t -> bool
......
......@@ -36,6 +36,7 @@ end
(* [Pid] describes identifier used in pattern graphs *)
module Pid : sig
type t = Pos of int | Neg of int
val compare: t -> t -> int
val to_id: t -> string
val to_string: t -> string
end
......@@ -59,6 +60,8 @@ module Gid : sig
| Old of int
| New of (int * int) (* identifier for "created nodes" *)
val compare: t -> t -> int
val to_string: t -> string
end
......
......@@ -75,22 +75,22 @@ let to_sentence ?main_feat gr =
let get_sequence_names grs = Grs.sequence_names grs
let load_gr file =
if (Sys.file_exists file) then (
try
let gr_ast = Grew_parser.gr_of_file file in
Instance.from_graph (G_graph.build gr_ast)
with
| Grew_parser.Parse_error (msg,Some (sub_file,l)) ->
raise (Parsing_err (sprintf "[file:%s, line:%d] %s" sub_file l msg))
| Grew_parser.Parse_error (msg,None) ->
raise (Parsing_err (sprintf "[file:%s] %s" file msg))
| Error.Build (msg,loc) -> raise (Build (msg,loc))
| Error.Bug (msg, loc) -> raise (Bug (msg,loc))
| exc -> raise (Bug (sprintf "[Libgrew.load_gr] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
) else (
raise (File_dont_exists file)
)
if Sys.file_exists file
then
begin
try
let gr_ast = Grew_parser.gr_of_file file in
Instance.from_graph (G_graph.build gr_ast)
with
| Grew_parser.Parse_error (msg,Some (sub_file,l)) ->
raise (Parsing_err (sprintf "[file:%s, line:%d] %s" sub_file l msg))
| Grew_parser.Parse_error (msg,None) ->
raise (Parsing_err (sprintf "[file:%s] %s" file msg))
| Error.Build (msg,loc) -> raise (Build (msg,loc))
| Error.Bug (msg, loc) -> raise (Bug (msg,loc))
| exc -> raise (Bug (sprintf "[Libgrew.load_gr] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
end
else raise (File_dont_exists file)
let load_conll file =
try
......@@ -120,6 +120,9 @@ let load_graph file =
Log.fcritical "[Libgrew.load_graph] Cannot guess input file format of file '%s'. Use .gr or .conll file extension" file
end
let raw_graph instance =
G_graph.to_raw instance.Instance.graph
let rewrite ~gr ~grs ~seq =
try Grs.rewrite grs seq gr
with
......
......@@ -61,6 +61,16 @@ File extension should be '.gr' or '.conll'.
*)
val load_graph: string -> Instance.t
(** [raw_graph instance] returns all graph information with a triple of basic caml types:
- the meta data
- the list of node (node is a list of feature (feature is string * string))
- the list of edge (src, label, tar) where src and tar refers to the position in the node list
*)
val raw_graph: Instance.t ->
(string * string) list *
(string * string) list list *
(int * string * int) list
val save_index: dirname:string -> base_names: string list -> unit
val write_html:
......
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