Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created June 14, 2011 00:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/1024087 to your computer and use it in GitHub Desktop.
Save swannodette/1024087 to your computer and use it in GitHub Desktop.
dcg.clj
(def-->e verb [v]
([[:v 'eats]] '[eats]))
(def-->e noun [n]
([[:n 'bat]] '[bat])
([[:n 'cat]] '[cat]))
(def-->e det [d]
([[:d 'the]] '[the])
([[:d 'a]] '[a]))
(def-->e noun-phrase [n]
([[:np ?d ?n]] (det ?d) (noun ?n)))
(def-->e verb-phrase [n]
([[:vp ?v ?np]] (verb ?v) (noun-phrase ?np)))
(def-->e sentence [s]
([[:s ?np ?vp]] (noun-phrase ?np) (verb-phrase ?vp)))
(run* [parse-tree]
(sentence parse-tree '[the bat eats a cat] []))
;; ([:s [:np [:d the] [:n bat]] [:vp [:v eats] [:np [:d a] [:n cat]]]])
;; ~70ms
(dotimes [_ 10]
(time
(dotimes [_ 1e3]
(run* [parse-tree]
(sentence parse-tree '[the bat eats a cat] [])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment