diff --git a/src/grew_fs.ml b/src/grew_fs.ml index a7c7632f889dd19a01082bc207f78720b4b41a9f..afdd3f8b1227171c972bff3facd144cfdbfb76ca 100644 --- a/src/grew_fs.ml +++ b/src/grew_fs.ml @@ -296,10 +296,12 @@ module G_fs = struct | s -> sprintf "<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\">\n%s\n</TABLE>\n" s (* ---------------------------------------------------------------------- *) - let to_word ?main_feat t = - match get_main ?main_feat t with - | (None, _) -> "#" - | (Some (_,atom), _) -> string_of_value atom + let to_word (t:t) = + match List_.sort_assoc "phon" t with + | Some s -> Some (string_of_value s) + | None -> match List_.sort_assoc "form" t with + | Some s -> Some (string_of_value s) + | None -> None (* ---------------------------------------------------------------------- *) let escape_sharp s = diff --git a/src/grew_fs.mli b/src/grew_fs.mli index fa254a735ae42085f00ee96002a1f05648d7b02a..63b32fa776138efa52271b040af09196c4db667c 100644 --- a/src/grew_fs.mli +++ b/src/grew_fs.mli @@ -47,7 +47,7 @@ module G_fs: sig val get_float_feat: string -> t -> float option val to_gr: t -> string val to_dot: ?decorated_feat:(string * string list) -> ?main_feat: string -> t -> string - val to_word: ?main_feat: string -> t -> string + val to_word: t -> string option 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 diff --git a/src/grew_graph.ml b/src/grew_graph.ml index e7235a69b235673af440e3ab52d4da524862c3f2..7fe9c0b202c1ef49d5802f42843e2df22b8ac62b 100644 --- a/src/grew_graph.ml +++ b/src/grew_graph.ml @@ -756,29 +756,60 @@ module G_graph = struct Buffer.contents buff (* -------------------------------------------------------------------------------- *) - let to_sentence ?main_feat graph = + let fusion_item_space_after fi = + try if List.assoc "SpaceAfter" fi.efs = "No" then "" else " " + with Not_found -> " " + + let space_after gnode = + match G_fs.get_string_atom "_MISC_SpaceAfter" (G_node.get_fs gnode) with + | Some "No" -> "" + | _ -> " " + + let esc s = Str.global_replace (Str.regexp "<") "<" s + + let to_sentence ?main_feat ?(deco=G_deco.empty) graph = + + let is_highlighted_gid gid = List.mem_assoc gid deco.nodes in + + let inside fusion_item gid = + let first = Gid_map.find fusion_item.first graph.map in + let last = Gid_map.find fusion_item.last graph.map in + let node = Gid_map.find gid graph.map in + match (G_node.get_position first, G_node.get_position node, G_node.get_position last) with + | (Ordered f, Ordered n, Ordered l) when f <=n && n <= l -> true + | _ -> false in + + let is_highlighted_fusion_item fusion_item = + List.exists (fun (gid,_) -> inside fusion_item gid) deco.nodes in + let nodes = Gid_map.fold (fun id elt acc -> (id,elt)::acc) graph.map [] in let snodes = List.sort (fun (_,n1) (_,n2) -> G_node.position_comp n1 n2) nodes in - let words = List.map - (fun (id, node) -> G_fs.to_word ?main_feat (G_node.get_fs node) - ) snodes in - List.fold_left - (fun acc (regexp,repl) -> - Str.global_replace (Str.regexp_string regexp) repl acc - ) - (String.concat " " words) - [ - " -t-", "-t-"; - "_-_", "-"; - "_", " "; - "' ", "'"; - " ,", ","; - " .", "."; - "( ", "("; - " )", ")"; - "\\\"", "\""; - ] + let rec loop skip = function + | [] -> "" + | (gid, gnode)::gtail when skip = None -> + begin + match List.find_opt (fun fusion_item -> fusion_item.first=gid) graph.fusion with + | Some fusion_item -> + (if is_highlighted_fusion_item fusion_item + then sprintf "<span class=\"highlight\">%s</span>" (esc fusion_item.word) + else (esc fusion_item.word)) + ^ (fusion_item_space_after fusion_item) + ^ (loop (Some fusion_item.last) gtail) + | None -> + match G_fs.to_word (G_node.get_fs gnode) with + | None -> (loop None gtail) + | Some text -> + (if is_highlighted_gid gid + then sprintf "<span class=\"highlight\">%s</span>" (esc text) + else esc (text)) + ^ (space_after gnode) + ^ (loop None gtail) + end + | (gid, gnode)::gtail when skip = Some gid -> loop None gtail + | (gid, gnode)::gtail -> loop skip gtail in + + Sentence.fr_clean_spaces (loop None snodes) (* -------------------------------------------------------------------------------- *) let to_dep ?domain ?filter ?main_feat ?(deco=G_deco.empty) graph = diff --git a/src/grew_graph.mli b/src/grew_graph.mli index 65b156c0cce77439d7f37163cb9ec934e5d656e5..3d993ca37dbe69c5554d5e04b9f536de94cd4c9d 100644 --- a/src/grew_graph.mli +++ b/src/grew_graph.mli @@ -197,7 +197,7 @@ module G_graph: sig (* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *) val to_gr: ?domain:Domain.t -> t -> string 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_sentence: ?main_feat:string -> ?deco:G_deco.t -> t -> string val to_dep: ?domain:Domain.t -> ?filter: (string -> bool) -> ?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 diff --git a/src/libgrew.ml b/src/libgrew.ml index 14d6d25819ab4f15e607286fca449bc7acdcc800..ce47109a3447e476810d29faeef5c8d2d14e9662 100644 --- a/src/libgrew.ml +++ b/src/libgrew.ml @@ -205,10 +205,10 @@ type t = Grew_graph.G_graph.t let to_conll_string ?domain graph = handle ~name:"Graph.to_conll_string" (fun () -> Grew_graph.G_graph.to_conll_string ?domain graph) () - let to_sentence ?main_feat gr = + let to_sentence ?main_feat ?deco gr = handle ~name:"Graph.to_sentence" (fun () -> - Grew_graph.G_graph.to_sentence ?main_feat gr + Grew_graph.G_graph.to_sentence ?main_feat ?deco gr ) () let save_conll ?domain filename graph = diff --git a/src/libgrew.mli b/src/libgrew.mli index efc51a0fa679bb00c15ecae22c7b25d7f6e86194..73f2d16fa91a49922fd4a0c8437d4f5b8c914c19 100644 --- a/src/libgrew.mli +++ b/src/libgrew.mli @@ -87,7 +87,7 @@ module Graph : sig val of_pst: ?domain:Domain.t -> string -> t val sentence_of_pst: ?domain:Domain.t -> string -> string - val to_sentence: ?main_feat:string -> t -> string + val to_sentence: ?main_feat:string -> ?deco:Deco.t -> t -> string val to_dot : ?domain:Domain.t -> ?main_feat:string -> ?deco:Deco.t -> t -> string