<commit_before>(ns codewars.util
  (:require [clojure.java.io :as io]))

(def ^:private
  language-data
  {"clojure" {:pattern #"^\(ns\s+([A-Z|a-z](?:[a-z|A-Z|0-9|-]|\.[A-Z|a-z])*)\W"
              :extension "clj"}
   "java" {:pattern #"\bclass\s+([A-Z][a-z|A-Z|0-9|_]*)\W"
           :extension "java"}})

(defn class-name
  "Infer the appropriate class or namespace name from code given a language"
  [language code]
  (let [{:keys [:pattern]} (get language-data language)]
    ;; TODO: Filter comments
    (if-let [name (->> code (re-find pattern) second symbol)]
      name
      (throw
       (IllegalArgumentException.
        (format "Could not infer class or namespace name for language %s from code:\n%s\n" (pr-str language) code))))))

(defn class-name-to-file-name
  "Create a file name for a class or namespace name given a language"
  [language class-name]
  (let [{:keys [:extension]} (get language-data language)]
    (-> class-name
        name
        ;; TODO: not platform independent...
        (clojure.string/replace "." "/")
        (str "." extension)
        io/file)))

(defn write-code!
  "Write code to an appropriate file in a specified directory given a language"
  [language dir code]
  (let [class-name (class-name language code)
        base-name (class-name-to-file-name language class-name)
        file-name (io/file dir base-name)]
    (if (.exists file-name)
      (throw
       (UnsupportedOperationException.
        (format "Could not write to file %s, because that file already exists.  Perhaps it already contains the setup or test fixture code?\ncode:\n%s" (pr-str base-name) code)))
      ;; else
      (do
        (-> file-name .getParentFile .mkdirs)
        (spit file-name code)
        {:file-name file-name
         :class-name class-name}))))
<commit_msg>Handling - in clojure namespaces correctly now<commit_after>(ns codewars.util
  (:require [clojure.java.io :as io]))

(def ^:private
  language-data
  {"clojure" {:pattern #"^\(ns\s+([A-Z|a-z](?:[a-z|A-Z|0-9|-]|\.[A-Z|a-z])*)\W"
              :extension "clj"}
   "java" {:pattern #"\bclass\s+([A-Z][a-z|A-Z|0-9|_]*)\W"
           :extension "java"}})

(defn class-name
  "Infer the appropriate class or namespace name from code given a language"
  [language code]
  (let [{:keys [:pattern]} (get language-data language)]
    ;; TODO: Filter comments
    (if-let [name (->> code (re-find pattern) second symbol)]
      name
      (throw
       (IllegalArgumentException.
        (format "Could not infer class or namespace name for language %s from code:\n%s\n" (pr-str language) code))))))

(defn class-name-to-file-name
  "Create a file name for a class or namespace name given a language"
  [language class-name]
  (let [{:keys [:extension]} (get language-data language)]
    (-> class-name
        name
        (clojure.string/replace "-" "_")
        ;; TODO: not platform independent...
        (clojure.string/replace "." "/")
        (str "." extension)
        io/file)))

(defn write-code!
  "Write code to an appropriate file in a specified directory given a language"
  [language dir code]
  (let [class-name (class-name language code)
        base-name (class-name-to-file-name language class-name)
        file-name (io/file dir base-name)]
    (if (.exists file-name)
      (throw
       (UnsupportedOperationException.
        (format "Could not write to file %s, because that file already exists.  Perhaps it already contains the setup or test fixture code?\ncode:\n%s" (pr-str base-name) code)))
      ;; else
      (do
        (-> file-name .getParentFile .mkdirs)
        (spit file-name code)
        {:file-name file-name
         :class-name class-name}))))

{'fraction_non_alphanumeric': 0.153470715835141, 'fraction_numerical': 0.0021691973969631237, 'mean_word_length': 2.711267605633803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636533', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1190, 'n_words': 358}