-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.clj
More file actions
33 lines (27 loc) · 849 Bytes
/
parser.clj
File metadata and controls
33 lines (27 loc) · 849 Bytes
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
(ns slack-lambda.parser
(:require [clojure.string :as string]
[instaparse.core :as insta]))
(def parser
(insta/parser
"<dialogue> = <'bot '> (say-hi | say-goodbye)
say-hi = <'hello '> person
say-goodbye = <'goodbye '> person
person = 'world' | 'jean-louis' | 'suvash'"))
(defn- cleanup [text]
(-> text
string/lower-case
(string/replace #"[,.!?:@]" " ")
(string/replace "’" "'")
(string/replace #"[\s]+" " ")
string/trim))
(defn success? [result]
(not (insta/failure? result)))
(defn failure->string [failure]
(with-out-str (instaparse.failure/print-reason failure)))
(defn normalize [[action & args]]
[action (into {} args)])
(defn parse [text]
(let [result (insta/parse parser (cleanup text))]
(if (success? result)
(normalize (first result))
result)))