(* Algorithme ID3 *)

(* Encodage des données *)

(* attr0 : Meteo - valeurs : soleil 0, nuageux 1, pluie 2 *)
(* attr1 : Temperature - chaud 0, moyen 1, froid 2 *)
(* attr2 : Humidité - normal 0, elevé 1 *)
(* attr3 : Vent - non 0, oui 1 *)

(* classe : NON 0, OUI 1 *)

let nb_cl = 2;; (* nombre de classes ; de 0 à nb_cl - 1 *)

let noms_attr = [| "Meteo"; "Temperature"; "Humidité"; "Vent" |];;

let nb_val = [| 3; 3; 2; 2|];; (* nombre de valeurs par attribut ; de 0 à na - 1 *)

let data = [ ([|0; 0; 1; 0|], 0); (* les 14 exemples *)
             ([|0; 0; 1; 1|], 0);
             ([|1; 0; 1; 0|], 1);
             ([|2; 1; 1; 0|], 1);
             ([|2; 2; 0; 0|], 1);
             ([|2; 2; 0; 1|], 0);
             ([|1; 2; 0; 1|], 1);
             ([|0; 1; 1; 0|], 0);
             ([|0; 2; 0; 0|], 1);
             ([|2; 1; 0; 0|], 1);
             ([|0; 1; 0; 1|], 1);
             ([|1; 1; 1; 1|], 1);
             ([|1; 0; 0; 0|], 1);
             ([|2; 1; 1; 1|], 0) ];;

type arbre = 
    F of (int * float) (* la classe et la proportion associée *)
  | N of (int * arbre array) (* l'attribut et le tableau des sous arbres *)


let arbre_test = N (0, [| F (0, 1.); F (1, 0.7); F (-1, 1.)|]);;

let affiche_arbre arb =
  let rec affiche_espace n = 
    if n > 0 then begin print_string " "; affiche_espace (n-1) end
  in
  let rec affiche_aux decalage arb = affiche_espace decalage; match arb with
    | F (c, prob) -> Printf.printf "[Classe %d (%f)]\n" c prob
    | N (attr, fils_tab) -> begin
        Printf.printf "%s \n" noms_attr.(attr);
        Array.iter (fun a -> affiche_aux (decalage+3) a) fils_tab;
    end
  in
  affiche_aux 0 arb
;;

affiche_arbre arbre_test;;