Last active
January 30, 2020 21:28
-
-
Save sim-f/94ff32021bc87107b30118d4502718dd to your computer and use it in GitHub Desktop.
clojure regular expression rule matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns main | |
(:require [clojure.string :as s] | |
[clojure.data.csv :as csv] | |
[clojure.java.io :as io]) | |
(:gen-class)) | |
; From java cli with clojure 1.8.0 | |
; On windows: | |
; java -cp "clojure-1.8.0.jar;data.csv-0.1.4.jar;." clojure.main | |
; On linux: | |
; java -cp clojure-1.8.0.jar:data.csv-0.1.4.jar:. clojure.main | |
; In REPL: | |
; (require 'main) | |
; If edit | |
; (require '[main :reload :all]) | |
; (main/-main "in-file.csv" "out-file.csv" | |
; {"Mat1" #{"Missing re"} | |
; "Mat2" #{" Missing re"}}) | |
;; Test data | |
(def results (group-by-key | |
:sn | |
:arts | |
(csv-data->maps | |
(import-csv! "resources/in-file.csv")))) | |
(def items [[" Mat1 " " abc "] | |
[" Mat2 " " 123 "]]) | |
(def mat-res {"Mat1" #{"Missing re"} | |
"Mat2" #{" Missing re"}}) | |
; ; Import | |
(defn import-csv! | |
([filepath] (import-csv! filepath \tab)) | |
([filepath separator] (with-open [reader (io/reader filepath)] | |
(doall | |
(csv/read-csv reader :separator separator))))) | |
; (import-csv! "resources/in-file.csv") | |
; ;; Export | |
(defn export-csv! | |
([filepath items] (export-csv! filepath items \tab)) | |
([filepath items separator] (with-open [writer (io/writer filepath)] | |
(csv/write-csv writer items :separator separator)))) | |
; (export-csv! "resources/out-file.csv" items) | |
(defn csv-data->maps [csv-data] | |
(map zipmap | |
(->> (first csv-data) ;; First row is the header | |
(map keyword) ;; Drop if you want string keys instead | |
repeat) | |
(rest csv-data))) | |
;########################################################## | |
; From "map to csv clojure" | |
; https://stackoverflow.com/questions/18572117/convert-collection-of-hash-maps-to-a-csv-file | |
(def data | |
[{:a "Completed" :b 1 :c "Friday" :d 4} | |
{:a "Started" :b 1 :c "Monday" :d 4} | |
{:a "In Progress" :b 1 :c "Sunday" :d 1} | |
{:a "Completed" :b 3 :c "Tuesday" :d 9}]) | |
(defn write-csv [path row-data] | |
(let [columns [:a :b :c :d] | |
headers (map name columns) | |
rows (mapv #(mapv % columns) row-data)] | |
(with-open [file (io/writer path)] | |
(csv/write-csv file (cons headers rows))))) | |
(write-csv "/tmp/results.csv" data) | |
;########################################################## | |
(defn group-by-key [key group-name coll] | |
(mapv | |
#(zipmap [key group-name] %) | |
(group-by key coll))) | |
; (group-by-key :sn | |
; :arts | |
; (csv-data->maps | |
; (import-csv! "resources/in-file.csv"))) | |
;; functions | |
(defn includes-re [re s] (not (nil? (re-find re s)))) | |
(defn check-all-rules [res mat sn] | |
(let [set-of-rules (seq (get res mat))] | |
(zipmap set-of-rules | |
(map includes-re | |
set-of-rules | |
(repeat sn))))) | |
(defn passing-rules [result] | |
(vec (keys (filter #(true? (val %)) result)))) | |
(defn make-result-map [res [mat sn]] | |
(zipmap [:mat :sn :passing-rules] [mat sn (passing-rules | |
(check-all-rules | |
res | |
mat | |
sn))])) | |
(defn result-map-to-str-vec | |
[{:keys [mat sn passing-rules]}] | |
[mat sn (count passing-rules) (s/join ";" passing-rules)]) | |
;; Main | |
(defn -main | |
"I don't do a whole lot ... yet." | |
[& args] | |
(if-not (empty? args) | |
(let [[in-file out-file res] args | |
result-map (mapv #(make-result-map res %) | |
(import-csv! in-file)) | |
out-string (cons ["mat" "sn" "count passing-rules" "passing-rules"] | |
(map result-map-to-str-vec result-map))] | |
(println in-file) | |
(println out-file) | |
(println result-map) | |
(export-csv! out-file out-string)) | |
; In case there are no command line arguments | |
(throw (Exception. "Need at least one command line argument!")))) | |
; (-main "resources/in-file.csv" "resources/out-file.csv" mat-res) | |
;; delete outfile | |
;; (clojure.java.shell/sh "rm" "resources/out-file.csv") | |
;; Revers the first character String functions | |
(defn reverse-first | |
"Revers the first character String functions" | |
[substring string] | |
(if (s/starts-with? string substring) | |
(s/replace string #"^(.)(.*)" "$2$1") | |
string)) | |
; (reverse-first "." ".test") | |
;; update the art map | |
; (update-in (first results) [:arts 0 :article] #(reverse-first "." %)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment