Commit 00ab496e authored by bguillaum's avatar bguillaum

file loading

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/semagramme/libcaml-grew/trunk@8754 7838e531-6607-4d57-9587-6c381814729c
parent 5a825a5d
......@@ -154,6 +154,30 @@ module File = struct
with End_of_file ->
close_in in_ch;
List.rev !rev_lines
let load file =
let ch = open_in file in
let buff = Buffer.create 32 in
try
while true do
let next = input_line ch in
Printf.bprintf buff "%s\n" next
done; assert false
with End_of_file ->
close_in ch;
Buffer.contents buff
exception Found of int
let get_suffix file_name =
let len = String.length file_name in
try
for i = len-1 downto 0 do
if file_name.[i] = '.'
then raise (Found i)
done;
None
with
| Found i -> Some (String.sub file_name i (len-i))
end (* module File *)
(* ================================================================================ *)
......
......@@ -65,11 +65,23 @@ module File: sig
val write: string -> string -> unit
(** [read file_name] read the content of [file_name] line by line.
Blanks lines (empty or only with spaces and tabs) are ignored.
Lines with '%' as the first char are ignored. *)
Blanks lines (empty or only with spaces and tabs) are ignored.
Lines with '%' as the first char are ignored. *)
val read: string -> string list
(** [read_ln file_name] read the content of [file_name] line by line.
Blanks lines (empty or only with spaces and tabs) are ignored.
Lines with '%' as the first char are ignored.
Each line is returned with its position in the original file. *)
val read_ln: string -> (int * string) list
(** [load file_name] load the content of [file_name] as a string. *)
val load: string -> string
(** [get_suffix file_name] returns the suffix in [file_name].
"x.y.z" -> Some ".z"
"xyz" -> None *)
val get_suffix: string -> string option
end
(* ================================================================================ *)
......
......@@ -138,22 +138,27 @@ let of_brown brown =
Instance.from_graph graph
) ()
let load_brown file =
handle ~name:"load_brown"
(fun () ->
let brown = File.load file in
let graph = G_graph.of_brown brown in
Instance.from_graph graph
) ()
let load_graph file =
handle ~name:"load_graph" ~file
(fun () ->
if Filename.check_suffix file ".gr"
then load_gr file
else if Filename.check_suffix file ".conll"
then load_conll file
else
begin
match File.get_suffix file with
| Some ".gr" -> load_gr file
| Some ".conll" -> load_conll file
| Some ".br" | Some ".melt" -> load_brown file
| _ ->
Log.fwarning "Unknown file format for input graph '%s', try to guess..." file;
try load_gr file with
Parsing_err _ ->
try load_conll file with
Parsing_err _ ->
Log.fcritical "[Libgrew.load_graph] Cannot guess input file format of file '%s'. Use .gr or .conll file extension" file
end
let rec loop = function
| [] -> Log.fcritical "[Libgrew.load_graph] Cannot guess input file format of file '%s'. Use .gr or .conll file extension" file
| load_fct :: tail -> try load_fct file with _ -> loop tail in
loop [load_gr; load_conll; load_brown]
) ()
let xml_graph xml =
......
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