-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhelper.ml
78 lines (71 loc) · 2.69 KB
/
testhelper.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(*This file utilizes some functions in oscartest.ml, changed up in some ways
*to take in input instead of the mutable list variables, so that testing
*will work on these helper functions *)
open Emoji
open Str
(** [explode s] takes in a string s and converts it into a char list. *)
let explode s =
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) []
(** [split_string s] takes in string s and
* splits it into a string list by spaces *)
let split_string s =
s |> String.split_on_char ' ' |> List.filter (fun s -> s <> "")
(** [replace_with_underscore_words h] is a helper function that takes
* in a string and replaces spaces with underscores. *)
let replace_with_underscore_words h =
let exploded = explode h in
let rec helper lst acc =
match exploded with
| [] -> acc
| h::t ->
if h <> ' ' then
helper t (acc ^ Char.escaped h)
else helper t (acc ^ "_")
in helper exploded ""
(** [replace_with_underscore_list e] is a function that works on
* the list for underscores to make print_emojis work *)
let replace_with_underscore_list e =
let rec repl e acc=
match e with
| [] -> acc
| h::t -> repl t [replace_with_underscore_words h]@acc
in repl e []
(** [text_emoji_handling str] takes in a user text and changes some
* standard symbols into emojis if any. *)
let text_emoji_handling str =
let array = Str.split_delim (Str.regexp " ") str in
let matched = List.map (fun x ->
if x = ":)" then Emoji.smiling_face
else if x = ";)" then Emoji.winking_face
else if x = ":|" then Emoji.neutral_face
else if x = ":(" then Emoji.disappointed_face
else if x = "<3" then Emoji.sparkling_heart
else if x = ":'(" then Emoji.crying_face
else if x = "</3" then Emoji.broken_heart
else if x = ":D" then Emoji.grinning_face
else if x = "(^^^)" then Emoji.shark
else if x = ":o" || x = ":O" then Emoji.astonished_face
else if x = ":\\" then Emoji.slightly_frowning_face
else x) array
in
String.concat " " matched
(** [counting x lst c] is a tail-recursive function that
* returns the index of the first element in list lst
* that is equal to x where c is the accumulator. *)
let rec counting x lst c =
match lst with
| [] -> raise(Failure "Not Found")
| h::t -> if (h=x) then c else counting x t (c+1)
let handle_message_single msg =
let arrays = Str.split_delim (Str.regexp " ") msg in
if List.length arrays = 1 then
match String.lowercase_ascii(List.hd arrays) with
| "emojis" -> "Emojis"
| "checkers" -> "Checkers"
| "read" -> "Read"
| "recent" -> "Recent"
| _ -> "Unknown command"
else
"Not single"