Anonymous avatar Anonymous committed f83ca0e

split_char

Comments (0)

Files changed (1)

 let regex_space = Str.regexp " ";;
 let regex_colon = Str.regexp ":";;
 
+let split_char sep str =
+  let string_index_from i =
+    try Some (String.index_from str i sep)
+    with Not_found -> None
+  in
+  let rec aux i acc = match string_index_from i with
+    | Some i' ->
+      let w = String.sub str i (i' - i) in
+        aux (succ i') (w::acc)
+    | None ->
+      let w = String.sub str i (String.length str - i) in
+        List.rev (w::acc)
+  in
+  aux 0 []
+
+let parse_line0 line =
+  let l2l token = 
+    match split_char ':' token with
+      | i::v::[] -> ((my_int_of_string i), (float_of_string v));
+      | _ -> raise Bad_line
+  in	
+  match split_char ' ' line with
+    | [] -> raise Bad_line;
+    | hd::tl ->
+      (my_int_of_string hd), (List.map l2l tl);;
+
+(* string -> int, [float] *)
 let parse_line line =
   let l2l token =
     match Str.split regex_colon token with
   let rec train_ w =
     match get_line input_in with
       | Line(line) ->
-	let sign,x = parse_line line in
+	let sign,x = parse_line0 line in
 	let tau = f_tau x (loss sign w x) in
 	train_ (Vector.w_plus_yx w (tau *. float_of_int sign) x);
       | EOF ->
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.