Commit ae8e5b39 authored by Bruno Guillaume's avatar Bruno Guillaume

add Graph.to_conll function

parent 3df35f64
......@@ -866,91 +866,6 @@ module G_graph = struct
| _::t -> loop (n+1) t
in loop 0
(* -------------------------------------------------------------------------------- *)
let to_conll_string ?domain graph = (* Unused code! *)
let nodes = Gid_map.fold
(fun gid node acc -> (gid,node)::acc)
graph.map [] in
(* sort nodes wrt position *)
let snodes = List.sort (fun (_,n1) (_,n2) -> G_node.position_comp n1 n2) nodes in
(* renumbering of nodes to have a consecutive sequence of int 1 --> n, in case of node deletion or addition *)
let snodes = List.mapi
(fun i (gid,node) -> (gid, G_node.set_position (float i) node)
) snodes in
let get_num gid =
let gnode = List.assoc gid snodes in
if G_node.is_conll_root gnode
then 0.
else G_node.get_float (List.assoc gid snodes) in
(* Warning: [govs_labs] maps [gid]s to [num]s *)
let govs_labs =
Gid_map.fold
(fun src_gid node acc ->
let src_num = get_num src_gid in
Massoc_gid.fold
(fun acc2 tar_gid edge ->
let old = try Gid_map.find tar_gid acc2 with Not_found -> [] in
Gid_map.add tar_gid ((sprintf "%g" src_num, G_edge.to_string ?domain edge)::old) acc2
) acc (G_node.get_next node)
) graph.map Gid_map.empty in
let buff = Buffer.create 32 in
List.iter (fun v -> bprintf buff "%s\n" v) graph.meta;
List.iter
(fun (gid, node) ->
begin
try
let (gid_last,fusion) = List.assoc gid graph.fusion in
bprintf buff "%g-%g\t%s\t_\t_\t_\t_\t_\t_\t_\t_\n"
(get_num gid) (get_num gid_last) fusion
with
| Not_found -> ()
end;
if not (G_node.is_conll_root node)
then
let gov_labs = try Gid_map.find gid govs_labs with Not_found -> [] in
let sorted_gov_labs =
List.sort
(fun (g1,l1) (g2,l2) ->
if l1 <> "" && l1.[0] <> 'I' && l1.[0] <> 'D' && l1.[0] <> 'E'
then -1
else if l2 <> "" && l2.[0] <> 'I' && l2.[0] <> 'D' && l2.[0] <> 'E'
then 1
else
match compare (String_.to_float g1) (String_.to_float g2) with
| 0 -> compare l1 l2
| x -> x
) gov_labs in
let (govs,labs) = List.split sorted_gov_labs in
let fs = G_node.get_fs node in
bprintf buff "%g\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t_\t%s\n"
(get_num gid)
(match G_fs.get_string_atom "phon" fs with Some p -> p | None -> "_")
(match G_fs.get_string_atom "lemma" fs with Some p -> p | None -> "_")
(match G_fs.get_string_atom "cat" fs with Some p -> p | None -> "_")
(match G_fs.get_string_atom "pos" fs with Some p -> p | None -> "_")
(G_fs.to_conll_string ~exclude: ["phon"; "lemma"; "cat"; "pos"; "position"] fs)
(match govs with [] -> "_" | _ -> String.concat "|" govs)
(match labs with [] -> "_" | _ -> String.concat "|" labs)
(G_node.string_efs node)
)
snodes;
Buffer.contents buff
(* -------------------------------------------------------------------------------- *)
let to_conll ?domain graph =
let nodes = Gid_map.fold
......
......@@ -163,5 +163,6 @@ module G_graph: sig
val to_dot: ?domain:Domain.t -> ?main_feat:string -> ?deco:G_deco.t -> t -> string
val to_sentence: ?main_feat:string -> t -> string
val to_dep: ?domain:Domain.t -> ?filter : string list -> ?main_feat:string -> ?deco:G_deco.t -> t -> string
val to_conll: ?domain:Domain.t -> t -> Conll.t
val to_conll_string: ?domain:Domain.t -> t -> string
end (* module G_graph *)
......@@ -199,6 +199,9 @@ type t = Grew_graph.G_graph.t
let to_gr ?domain graph =
handle ~name:"Graph.to_gr" (fun () -> Grew_graph.G_graph.to_gr ?domain graph) ()
let to_conll ?domain graph =
handle ~name:"Graph.to_conll" (fun () -> Grew_graph.G_graph.to_conll ?domain graph) ()
let to_conll_string ?domain graph =
handle ~name:"Graph.to_conll_string" (fun () -> Grew_graph.G_graph.to_conll_string ?domain graph) ()
......
......@@ -95,6 +95,8 @@ module Graph : sig
val to_gr: ?domain:Domain.t -> t -> string
val to_conll: ?domain:Domain.t -> t -> Conll.t
val to_conll_string: ?domain:Domain.t -> t -> string
(** [search_pattern pattern graph] returns the list of the possible matching of [pattern] in [graph] *)
......
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