ishideo
11/1/2017 - 8:21 AM

header.clj

(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))))))