Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. (* ################### *)
  2. (* # PROJET OCAML T9 # *)
  3. (* ################### *)
  4.  
  5. (* ################################################ *)
  6. (* # Défition des types et de leur prototype vide # *)
  7. (* ################################################ *)
  8.  
  9. (* Type arbre des clés de position *)
  10. type tree_pos = { branches : (int * tree_pos) list};;
  11. let empty_pos = { branches = [] };;
  12.  
  13. (* Type arbre spécifique au T9 *)
  14. type tree = { position : tree_pos ; subtrees : (int * tree) list };;
  15. let empty = { position = empty_pos ; subtrees = [] } ;;
  16.  
  17. (* ############################################################# *)
  18. (* # Transformation des characteres en T9 : position et touche # *)
  19. (* ############################################################# *)
  20.  
  21. (* Charactère vers numéro de la touche *)
  22. let char_to_key = function
  23. | 'a' | 'b' | 'c' -> 2
  24. | 'd' | 'e' | 'f' -> 3
  25. | 'g' | 'h' | 'i' -> 4
  26. | 'j' | 'k' | 'l' -> 5
  27. | 'm' | 'n' | 'o' -> 6
  28. | 'p' | 'q' | 'r' | 's' -> 7
  29. | 't' | 'u' | 'v' -> 8
  30. | 'w' | 'x' | 'y' | 'z' -> 9
  31. | _ -> -1;;
  32.  
  33. (* Charactère vers position sur la touche *)
  34. let char_to_position = function
  35. | 'a' | 'd' | 'g' | 'j' | 'm' | 'p' | 't'| 'w' -> 1
  36. | 'b' | 'e' | 'h' | 'k' | 'n' | 'q' | 'u'| 'x' -> 2
  37. | 'c' | 'f' | 'i' | 'l' | 'o' | 'r' | 'v'| 'y' -> 3
  38. | 's' | 'z' -> 4
  39. | _ -> -1;;
  40.  
  41. (* Chaîne de caractère vers tableau des touches *)
  42. let rec string_to_keys = function
  43. | "" -> []
  44. | c -> (char_to_key (String.get c 0))::(string_to_keys (String.sub c 1 ((String.length c)-1)));;
  45.  
  46. (* Chaîne de caractère vers tableau des positions sur la touche *)
  47. let rec string_to_positions = function
  48. | "" -> []
  49. | c -> (char_to_position (String.get c 0))::(string_to_positions (String.sub c 1 ((String.length c)-1)));;
  50.  
  51. (* ################################################ *)
  52. (* # Constructeur de l'arbre des clés de position # *)
  53. (* ################################################ *)
  54.  
  55. (* Retourne l'arbre associer à la clé parmis les sous arbres : le créer sinon *)
  56. let eject_key_pos key subtrees =
  57. match key,subtrees with
  58. | key,subtrees when List.mem_assoc key subtrees.branches -> (key,(List.assoc key subtrees.branches)),(List.remove_assoc key subtrees.branches)
  59. | key,subtrees -> (key,empty_pos),subtrees.branches;;
  60.  
  61. (* Insert la liste des clés de position dans l'arbre *)
  62. let insert_pos positions tree =
  63. let rec aux keys tree =
  64. match keys,tree with
  65. | [],_ -> empty_pos
  66. | key::tlkeys,tree -> let r_eject_key = eject_key_pos key tree in
  67. let subkeytree = (fst r_eject_key) and othertrees = (snd r_eject_key) in
  68. let rec_subkeytrees = key,{branches = ((aux tlkeys (snd subkeytree)).branches)} in
  69. {branches = rec_subkeytrees::othertrees}
  70. in
  71. aux positions tree;;
  72.  
  73. (* ######################################### *)
  74. (* # Constructeur d'arbre spécifique au T9 # *)
  75. (* ######################################### *)
  76.  
  77. (* Insert la clé de la touche dans l'arbre spécifique au T9 *)
  78. let eject_key key tree =
  79. match key,tree with
  80. | key,tree when List.mem_assoc key tree.subtrees -> (key,(List.assoc key tree.subtrees)),{position = tree.position ; subtrees = (List.remove_assoc key tree.subtrees)}
  81. | key,tree -> (key,{position = empty_pos ; subtrees = []}),tree;;
  82.  
  83. (* Insert une chaîne de charactères dans l'arbre spécifique au T9 *)
  84. let insert word treeT9 =
  85. let rec aux keys positions treeT9 =
  86. match keys,treeT9 with
  87. | [],_ -> {position = (insert_pos positions treeT9.position) ; subtrees = treeT9.subtrees}
  88. | key::ltkeys, treeT9 -> let r_eject_key = eject_key key treeT9 in
  89. let subtreekey = (fst r_eject_key) and tree_without_key_one = (snd r_eject_key) in
  90. let fill_subtreekey = key,(aux ltkeys positions (snd subtreekey)) in
  91. {position = treeT9.position ; subtrees = fill_subtreekey::tree_without_key_one.subtrees}
  92. in
  93. aux (string_to_keys word) (string_to_positions word) treeT9;;
  94.  
  95. (* ######################## *)
  96. (* # Découpage de l'arbre # *)
  97. (* ######################## *)
  98.  
  99. (* Retourne les sous arbres associés aux touches d'un arbre T9 *)
  100. let assoc keys tree =
  101. let rec aux keys tree =
  102. match keys,tree with
  103. | [],tree -> tree
  104. | key::ltkeys,tree when List.mem_assoc key tree.subtrees -> aux ltkeys (List.assoc key tree.subtrees)
  105. | key::ltkeys,tree -> empty
  106. in
  107. aux keys tree;;
  108.  
  109. (* ################################################################## *)
  110. (* # Gestion de reconstitution en chaîne de caractère d'un arbre T9 # *)
  111. (* ################################################################## *)
  112.  
  113. (* Fonction de sortie *)
  114. (* Transforme un couple (touche,position) en un string *)
  115. let key_position_to_string = function
  116. | (2,1) -> "a" | (2,2) -> "b" | (2,3) -> "c"
  117. | (3,1) -> "d" | (3,2) -> "e" | (3,3) -> "f"
  118. | (4,1) -> "g" | (4,2) -> "h" | (4,3) -> "i"
  119. | (5,1) -> "j" | (5,2) -> "k" | (5,3) -> "l"
  120. | (6,1) -> "m" | (6,2) -> "n" | (6,3) -> "o"
  121. | (7,1) -> "p" | (7,2) -> "q" | (7,3) -> "r" | (7,4) -> "s"
  122. | (8,1) -> "t" | (8,2) -> "u" | (8,3) -> "v"
  123. | (9,1) -> "w" | (9,2) -> "x" | (9,3) -> "y" | (9,4) -> "z"
  124. | _ -> "?";;
  125.  
  126. (* Retourne le mot associé aux listes de touches,positions *)
  127. let rec printW keys positions =
  128. match keys, positions with
  129. | [],[] -> ""
  130. | key::ltkeys,pos::ltpositions -> (key_position_to_string (key,pos))^(printW ltkeys ltpositions)
  131. | _ -> failwith "error";;
  132.  
  133. (* Envoie à une fonction de sortie le mot associé aux listes de touches,positions *)
  134. let print_word keys positions =
  135. let rec aux keys tree positions =
  136. match tree.branches with
  137. | [] when (List.length keys) == (List.length positions) && (List.length keys > 0) -> print_string ((printW keys (List.rev positions))^"\n")
  138. | subtrees when (List.length subtrees) > 0 -> let rec map_rec keys sub pos =
  139. match sub with
  140. | [] -> ()
  141. | thetree::othertrees -> begin
  142.  
  143. (aux keys (snd thetree) ((fst thetree)::pos) );
  144. (map_rec keys othertrees pos);
  145. end
  146. in
  147. (map_rec keys subtrees positions)
  148. | _ -> ()
  149. in
  150. aux keys positions [];;
  151.  
  152. (* Envoie à une fonction de sortie tout les mots de l'arbre *)
  153. let save_word tree =
  154. let rec aux keys tree =
  155. match tree.position,tree.subtrees with
  156. | treeposition,[] -> print_word (List.rev keys) treeposition
  157. | treeposition,subtrees -> let rec map_rec ks sub =
  158. match sub with
  159. | [] -> ()
  160. | (node,sub)::othertrees -> (aux (node::ks) sub);
  161. (map_rec ks othertrees);
  162. in
  163. begin
  164. (print_word (List.rev keys) treeposition);
  165. (map_rec keys subtrees);
  166. end
  167. in
  168. aux [] tree;;
  169.  
  170. (* ######################################## *)
  171. (* # Reconstitution d'un fichier en arbre # *)
  172. (* ######################################## *)
  173.  
  174. let path = "/home/petillot/workspace/ocaml_project/dico_test.txt";;
  175.  
  176. let read_to_tree path_file =
  177. let rec aux file tree =
  178. let str = try input_line file with End_of_file -> "" in
  179. match str with
  180. | "" -> tree
  181. | str -> (aux file (insert str tree))
  182. in
  183. aux (open_in path_file) empty;;
  184.  
  185. let dico = read_to_tree path;;
  186. save_word dico;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement