Last active
January 19, 2018 08:25
-
-
Save ishideo/ea5ae3261bd452583fb9d1eb52034d81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 report.header | |
(:require [clojure.java.io :as io] | |
[clojure.string :as str] | |
;[clojure.data.csv :as csv] | |
[clojure.tools.cli :refer [parse-opts]]) | |
(:gen-class)) | |
(def cli-options | |
[["-f" "--file FILE" "Input file path" | |
:default "input.txt"] | |
["-s" "--skip-lines NUMBER" "Skip line number" | |
:default 0 | |
:parse-fn #(Integer/parseInt %)]]) | |
(defn double-quote? [^String x] | |
(if (re-find #"^\"" x) true false)) | |
(defn chop-both [^String line] | |
(cond (double-quote? line) | |
(.substring line 1 (dec (.length line))) | |
:else line)) | |
(defn read-file [^String path] | |
(line-seq (io/reader path :encoding "UTF-8"))) | |
(defn read-header [^String path ^Integer skip_lines] | |
(nth (line-seq (io/reader path :encoding "UTF-8")) skip_lines)) | |
(defn csv2list [^String line] | |
(str/split line #"\",\"")) | |
(defn uniq [^String words] | |
(distinct words)) | |
(defn autonum [^String uniq-word ^String words] | |
(loop [result [] cnt 0 words words] | |
(if (empty? words) | |
result | |
(recur | |
(conj result | |
(cond (zero? cnt) (first words) | |
(.equals ^String (first words) ^String uniq-word) (str uniq-word "." cnt) | |
:else (first words))) | |
(if (.equals ^String (first words) ^String uniq-word) (inc cnt) cnt) | |
(rest words))))) | |
(defn autonum-loop [^String uniq-words ^String words] | |
(loop [result words uniq-words uniq-words] | |
(if (empty? uniq-words) | |
result | |
(recur | |
(autonum (first uniq-words) result) | |
(rest uniq-words))))) | |
(defn -main [^String & args] | |
(let [{:keys [options arguments errors summary]} | |
(parse-opts args cli-options)] | |
(let [{:keys [file skip-lines]} options] | |
(let [header-words | |
(->> (read-header file skip-lines) | |
(chop-both) | |
(csv2list))] | |
(println (uniq header-words)) | |
(println (autonum-loop (uniq header-words) header-words)))))) | |
;(println (csv2list (chop-both (read-header path (Integer/parseInt skip_lines))))) | |
;(println (uniq ["a" "b" "a" "c" "d" "a"])) | |
;(println (distinct ["ab" "b" "ab" "c" "d" "a"]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment