From a8e908298890d2a792fa1883722d393fe211f98a Mon Sep 17 00:00:00 2001 From: Jacob Meline Date: Mon, 9 Oct 2017 23:12:21 -0600 Subject: [PATCH] Adding a ton of fixes and stability to the algorithms --- rating-area-parser/project.clj | 1 + .../src/rating_area_parser/core.clj | 56 +++++++++++-------- .../test/rating_area_parser/core_test.clj | 42 +++++++++++--- 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/rating-area-parser/project.clj b/rating-area-parser/project.clj index 891b54d..e6bbbab 100644 --- a/rating-area-parser/project.clj +++ b/rating-area-parser/project.clj @@ -4,6 +4,7 @@ :license {:name "MIT" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.8.0"] + ;[org.clojure/clojure "1.9.0-beta2"] [enlive "1.1.6"] [http-kit "2.2.0"] [org.clojure/data.xml "0.0.8"]] diff --git a/rating-area-parser/src/rating_area_parser/core.clj b/rating-area-parser/src/rating_area_parser/core.clj index 6142b74..7fe4595 100644 --- a/rating-area-parser/src/rating_area_parser/core.clj +++ b/rating-area-parser/src/rating_area_parser/core.clj @@ -88,46 +88,54 @@ (assoc new-map (keyword state) (partition-rating-areas state))) {} states)) (defn determine-type [value] - (cond - (clojure.string/blank? value) "String" - (number? (read-string value)) "Number" - :else "String")) + (if (re-find #"^[0-9]+$" value) "Number" "String")) (defn build-xml-data-tag [value] - (-> {:tag :Data :attrs {"ss:Type" (determine-type value)}} + (-> ((fnil determine-type " ") value) + (#(identity {:tag :Data :attrs {"ss:Type" %}})) (cond-> (not (empty? value)) (assoc :content [value])) (#(identity {:tag :Cell :content [%]})))) -(defn build-xml-cell +(defn build-xml-row [collection] - (-> (into [] (mapv #(build-xml-data-tag %) collection)) + (-> (into [] (map #(build-xml-data-tag %) collection)) (#(identity {:tag :Row :content %})))) (defn build-xml-worksheet - [values-map] - (cons - {:tag :Worksheet :attrs {"ss:Name" "Options"} - :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [ - (build-xml-data-tag "Effective Year") - (build-xml-data-tag "2018")]}]}]}]} - [])) + [state values] + (-> (into [] (map #(build-xml-row %) values)) + (#(identity {:tag :Worksheet :attrs {"ss:Name" (name state)} + :content [{:tag :Table :content %}]})))) +(defn dump-everything + [values-map] + (doseq [[state values] values-map] + (spit "test3" + (with-out-str (xml/emit (build-xml-worksheet state values)))))) +;; ========================================== (comment (reduce (fn [result [key value]] (conj {:tag :Worksheet :attrs {"ss:Name" (name key)} - :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-xml-cell value)}]}]}]} + :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-row value)}]}]}]} result)) [] (sort values-map))) -(defn testheader - [values-map] - (xml/emit-element - {:tag :WorkBook :attrs {:xmlns "urn:schemas-microsoft-com:office:spreadsheet" - :xmlns:o "urn:schemas-microsoft-com:office:office" - :xmlns:x "urn:schemas-microsoft-com:office:excel" - :xmlns:ss "urn:schemas-microsoft-com:office:spreadsheet" - :xmlns:html "http://www.w3.org/TR/REC-html40"} - :content (build-xml-worksheet values-map)})) + +;; ========================================== + +(defn write-generator [filename] + #(spit filename (with-out-str %))) + +(defn dump-to-xml + [values-map filename] + (let [writer (write-generator filename)] + (xml/emit-element + {:tag :WorkBook :attrs {:xmlns "urn:schemas-microsoft-com:office:spreadsheet" + :xmlns:o "urn:schemas-microsoft-com:office:office" + :xmlns:x "urn:schemas-microsoft-com:office:excel" + :xmlns:ss "urn:schemas-microsoft-com:office:spreadsheet" + :xmlns:html "http://www.w3.org/TR/REC-html40"} + :content (build-xml-worksheet values-map)}))) (defn -main "My attempt at writing a rating-area parser in clojure" diff --git a/rating-area-parser/test/rating_area_parser/core_test.clj b/rating-area-parser/test/rating_area_parser/core_test.clj index d7e95a8..f2a82ab 100644 --- a/rating-area-parser/test/rating_area_parser/core_test.clj +++ b/rating-area-parser/test/rating_area_parser/core_test.clj @@ -18,24 +18,50 @@ (testing "sanitize-and-normalize fn" (is (= ["Rating Area 3" " " "445"] (sanitize-and-normalize-data ["Rating Area 3 " " " "" "445"])))) + (testing "determine-type" + (is (= "Number" (determine-type "018")) + (= "String" (determine-type "abc"))) + (is (= "String" (determine-type "Rating Area 1")))) (testing "build-xml-data-tag" + (is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"}}]} + (build-xml-data-tag nil))) (is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]} (build-xml-data-tag "Rating Area 1"))) (is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "Number"} :content ["995"]}]} (build-xml-data-tag "995")))) - (testing "build-xml-cell" - (let [test-list '("Rating Area 1" "Boundary" " ")] + (testing "build-xml-row" + (let [test-list (lazy-seq '("Rating Area 1" "Boundary" "996" " "))] (is (= {:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]} - {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Boundary"]}]} - {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]} - (build-xml-cell test-list)))))) + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Boundary"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "Number"} :content ["996"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]} + (build-xml-row test-list))))) + + (testing "build-xml-worsheet" + (let [test-data {:WA '(("Rating Area 1" "King" " ") + ("Rating Area 2" "Clallam" " ") + ("Rating Area 2" "Cowlitz" " "))} + test-options {:Options '(("Effective Year" "2018"))}] + (= {:tag :Worksheet :attrs {"ss:Name" "Options"} + :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [(build-xml-data-tag "Effective Year") + (build-xml-data-tag "2018")]}]}]}]} + (build-xml-worksheet (ffirst test-options) (second (first test-options)))) + (is (= {:tag :Worksheet :attrs {"ss:Name" "WA"} + :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["King"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]} + {:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 2"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Clallam"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]} + {:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 2"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Cowlitz"]}]} + {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}]}]} + (build-xml-worksheet (ffirst test-data) (second (first test-data)))))))) - (comment testing "build-xml-worksheet" (is (= {:tag :Worksheet :attrs {"ss:Name" "ID"} - :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-xml-cell state)}]}]}]}))) - + :content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-row state)}]}]}]})))