XmlPrinter.ml 2.8 KB
open Xstd
open WalTypes
open LCGtypes

let gf_of_string = function
    "subj" -> SUBJ
  | "obj" -> OBJ
  | "arg"(*""*) -> ARG
  | "core" -> CORE
  | "nosem" -> NOSEM
  | "nogf" -> NOGF
  | "adjunct" -> ADJUNCT
  | "raised" -> RAISED
  | "clause" -> CLAUSE
  | "sentence" -> SENTENCE
  | s -> prerr_endline s; SUBJ
(*  | s -> failwith ("gf_of_string:" ^ s) *)

(*let morf_of_string s =
  let s = Str.split (Str.regexp "[()]") s in
  WalParser.parse_morf_single (List.hd s, List.tl s)*)

let rec lt_of_xml = function
    Xml.Element("node",["pred",pred;"cat",cat;"weight",weight;"id",id],[
          Xml.Element("gs",[],[gs]);
          Xml.Element("agf",[],[Xml.PCData agf]);
          Xml.Element("amorf",[],[amorf]);
          Xml.Element("attrs",[],attrs);
          Xml.Element("args",[],[args])]) ->
             Node{pred=pred; cat=cat; weight=float_of_string weight; id=int_of_string id;
                  gs = lt_of_xml gs;
                  agf = gf_of_string agf;  (* FIXME *)
                  amorf = WalTypes.Phrase(WalTypes.Null);  (* FIXME *)
                  arole = "";  (* FIXME *)
                  arole_attr = "";  (* FIXME *)
                  meaning = "";  (* FIXME *)
                  hipero = StringSet.empty;  (* FIXME *)
                  meaning_weight = -1.;  (* FIXME *)
                  position = WalTypes.{gf = WalTypes.SUBJ; role = ""; role_attr = ""; sel_prefs = [];
                     cr = []; ce = []; dir = WalTypes.Both; morfs = []};  (* FIXME *)
                  attrs=List.map (function Xml.Element("attr",["label",e],[t]) -> e,lt_of_xml t | _ -> failwith "lt_of_xml") attrs;
                  args=lt_of_xml args;}
  | Xml.Element("tuple",[],l) -> Tuple(List.map lt_of_xml l)
  | Xml.Element("val",[],[Xml.PCData s]) -> Val s
  | Xml.Element("variants",["label",e],l) -> Variant(e,List.map (function Xml.Element("variant",["id",i],[t]) -> i, lt_of_xml t | _ -> failwith "lt_of_xml") l)
  | Xml.Element("dot",[],[]) -> Dot
  | Xml.Element("ref",["id",i],[]) -> Ref(int_of_string i)
  | xml -> print_endline (Xml.to_string_fmt xml); failwith "lt_of_xml"

let graph_of_xml xml =
  let establish_indexs graph = 
    let max = Xlist.fold graph 0 (fun acc (n, _) -> if n > acc then n else acc) in
    let table = Array.make (max+1) Dot in
    Xlist.iter graph (fun (n,x) -> table.(n) <- x); table in
  match xml with
    Xml.Element("graph",[],l) ->
      establish_indexs @@ List.map (function Xml.Element("graph_node",["id",i],[xml]) -> int_of_string i, lt_of_xml xml | _ -> failwith "graph_of_xml") l
  | _ -> failwith "graph_of_xml"

let print_xml path name xml =
  let graph = graph_of_xml xml in
  Visualization.print_graph path name graph

let load_and_print_xml path name filename =
  print_xml path name @@ Xml.parse_file filename

(*let _ =
    load_and_print_xml "xml_test/" "test1.0" "xml_test/sentence1.0.xml"*)