Skip to content

Instantly share code, notes, and snippets.

@danneu
Created June 26, 2014 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/0296ee25df96ca34e582 to your computer and use it in GitHub Desktop.
Save danneu/0296ee25df96ca34e582 to your computer and use it in GitHub Desktop.
(ns drugcritic.slug
(:require [clojure.string :as str]))
;;Stop at '-<number>'
;;Stop at '.'
;;Stop at <number>
;;Stop at 'and'
;;Stop at ';'
;;Stop at '('
;;Stop at 'preservative free'
(defn hyphenize [s]
(-> s
(str/replace #"\+" "-")
(str/replace #"'" "")
(str/replace #"\." " ")
(str/replace #"\/" " ")
(str/replace #"\s+" " ")
(str/trim)
(str/replace #" " "-")))
(defn remove-tail
[s regex]
(-> (str/split s regex)
(first)
(str/join)
(str/trim)))
(defn simplify-name
"Removes chaff from drug name to try and dig out the simple
form of a name."
[s]
(-> s
(remove-tail #"-\d")
(remove-tail #" \.")
(remove-tail #" \d")
(remove-tail #"\(")
(remove-tail #" and")
(remove-tail #";")
(remove-tail #" #")
(remove-tail #" preservative free")
(remove-tail #" in")
(remove-tail #" \"")
(remove-tail #",")
(remove-tail #"-\"")
(remove-tail #" w/")
(remove-tail #" \[")))
(defn to-slug [s]
(hyphenize (simplify-name s)))
;; Drug transformers
(defn assoc-slug [drug]
(let [drug-name (:drug-name drug)]
(assoc drug :slug (to-slug drug-name))))
(defn assoc-simple-name [drug]
(let [simple-name (simplify-name (:drug-name drug))]
(assoc drug :simple-name simple-name)))
;; Tests
(defn assert-slug [a b] (assert (= (to-slug a) b)))
(assert-slug "ocusert pilo-20" "ocusert-pilo")
(assert-slug "ogen .625" "ogen")
(assert-slug "ogen 1.25" "ogen")
(assert-slug "abc def" "abc-def")
(assert-slug "abc def (preservation free)" "abc-def")
(assert-slug "abc def and xyz" "abc-def")
(assert-slug "abc;def" "abc")
(assert-slug "abc ;def" "abc")
(assert-slug "omnipen (amp)" "omnipen")
(assert-slug "omnipen-n" "omnipen-n")
(assert-slug "abc def preservative free" "abc-def")
(assert-slug "abc in plastic container" "abc")
(assert-slug "hydrocortisone in absorbase" "hydrocortisone")
(assert-slug "abcindef" "abcindef")
(assert-slug "h.p. acthar gel" "h-p-acthar-gel")
(assert-slug "hc #1" "hc")
(assert-slug "hy-pam \"25\"" "hy-pam")
(assert-slug
"hydralazine hydrochloride, hydrochlorothiazide and reserpine"
"hydralazine-hydrochloride")
(assert-slug
"hydralazine hydrochloride w/ hydrochlorothiazide 100/50"
"hydralazine-hydrochloride")
(assert-slug "a/t/s" "a-t-s")
(assert-slug "acetated ringer's" "acetated-ringers")
(assert-slug "abc \"yellow\"" "abc")
(assert-slug "clopra-\"yellow\"" "clopra")
(assert-slug "k+10" "k-10")
(assert-slug "somatropin [rdna orgin]" "somatropin")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment