-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut.erl
87 lines (78 loc) · 2.11 KB
/
tut.erl
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
79
80
81
82
83
84
85
86
87
-module(tut).
-export([ping/1, start/0, pong/0, startm/0, dog/0, rabbit/0, keyword_rh/1]).
ping(0) ->
pong ! finished,
io:format("ping finished~n", []);
ping(Z) ->
timer:sleep(100),
pong ! {ping, self()},
receive
message ->
io:format("ping received pong~n", [])
end,
ping(Z -1).
pong() ->
receive
finished ->
io:format("pong finished~n");
{ping, Ping_PID} ->
io:format("pong received ping~n", []),
Ping_PID ! message,
pong()
end.
start() ->
register(pong, spawn(tut, pong, [])),
spawn(tut, ping, [3]).
dog() ->
receive {Rab_PID, message} ->
io:format("woof woof rabbits ~n"),
Rab_PID ! {dog ,message}
end.
%rab ! {self(), message},
%receive
% {rab, message} ->
%io:format("woof woof rabbits~n")
% end.
%Message = "woof woof rabbits",
%rab ! {self(), Message},
%io:format("~p~n", [Message]).
rabbit() ->
timer:sleep(100),
dog ! {self(), message},
receive
{dog, message} ->
io:format("panic go and hide~n")
end.
startm() ->
register(dog, spawn(tut, dog, [])),
spawn(tut, rabbit, []).
keyword_rh(KWSpec) ->
{resp_handler,
fun (Input) ->
MatchLens = lists:map(
fun ({Variants, Out}) ->
{lists:map(fun erlang:length/1,
lists:filter(fun (Variant) ->
match_keyword(Variant, Input)
end,
Variants)),
Out}
end,
KWSpec),
Matches = lists:filter(fun (M) -> is_match(M) end, MatchLens),
io:format("~s~n",[Matches]),
Sorted = lists:sort(fun ({L1, _}, {L2, _}) -> lists:max(L1) > lists:max(L2) end, Matches),
io:format("~s~n",[Sorted]),
case Sorted of % Sometimes you want to accept "A" and "Africa" as different keywords
[{_Lens, Out} | _] ->
Out;
[] ->
{invalid, {keyword, [KWSpec], Input, not_an_option}}
end
end}.
match_keyword(KeywordRaw, InputRaw) ->
Keyword = string:casefold(KeywordRaw),
Input = string:casefold(string:trim(InputRaw)),
string:find(Input, Keyword) =:= Input.
is_match({[], _}) -> false;
is_match(_) -> true.