<commit_before>(ns pdok.featured.mustache
   (:require [clojure.java.io :as io]
             [pdok.featured.feature  :refer :all]))

(defn read-template [template]
  (slurp (io/resource template)))

(defn variables-from-template [template]
  distinct (re-seq #"\{\{[\S]*\}\}" template))

(defn template-to-pattern [template]
  (let [ variable-list  (distinct (re-seq #"\{\{[\S]*\}\}" template))
         pattern (str (apply str (interpose "|" (map #(java.util.regex.Pattern/quote %) variable-list))))]
    (re-pattern pattern)))

(defn render-template-one-feature [template feature]
  (clojure.string/replace template (template-to-pattern template) feature))

(defn resolve-as-function [function]
  (ns-resolve *ns* (symbol (str "pdok.featured.feature/as-" function))))

(deftype MapProxy [feature]
   clojure.lang.IFn
    	(invoke [_ k]
             (let [feature-key (keyword (clojure.string/replace k #"\{\{|\}\}" ""))]
      				(if (= :_geometry.gml feature-key)
                  (let [render-function (str feature-key)
                        render-function (last (clojure.string/split render-function #"\."))]
                   
                    ((resolve-as-function render-function) (:_geometry feature)))

                 	(or (.valAt feature feature-key) "")))))

(defn render-template[template features]
  (let [template (read-template template)]
    (map #(render-template-one-feature template (->MapProxy %)) features)))

;(with-open [s (file-stream ".test-files/new-features-single-collection-100000.json")] (time (last (features-from-package-stream s))))<commit_msg>PDOK-1025: multimethod for render-template<commit_after>(ns pdok.featured.mustache
   (:require [clojure.java.io :as io]
             [pdok.featured.feature  :refer :all]))

(defn read-template [template]
  (slurp (io/resource template)))

(defn variables-from-template [template]
  distinct (re-seq #"\{\{[\S]*\}\}" template))

(defn template-to-pattern [template]
  (let [ variable-list  (distinct (re-seq #"\{\{[\S]*\}\}" template))
         pattern (str (apply str (interpose "|" (map #(java.util.regex.Pattern/quote %) variable-list))))]
    (re-pattern pattern)))

(defn resolve-as-function [function]
  (ns-resolve *ns* (symbol (str "pdok.featured.feature/as-" function))))

(deftype MapProxy [feature]
   clojure.lang.IFn
      (invoke [_ k]
             (let [feature-key (keyword (clojure.string/replace k #"\{\{|\}\}" ""))]
              (if (= :_geometry.gml feature-key)
                  (let [render-function (str feature-key)
                        render-function (last (clojure.string/split render-function #"\."))]
                   
                    ((resolve-as-function render-function) (:_geometry feature)))

                  (or (.valAt feature feature-key) "")))))

(defmulti render-template (fn [template features] (if (instance? clojure.lang.IFn features) :single-feature :feature-collection)))

(defmethod render-template :single-feature [template feature] 
  (clojure.string/replace template (template-to-pattern template) feature))

(defmethod render-template :feature-collection [template features]
  (let [template (read-template template)]
    (map #(render-template template (->MapProxy %)) features)))

;(with-open [s (file-stream ".test-files/new-features-single-collection-100000.json")] (time (last (features-from-package-stream s))))
{'fraction_non_alphanumeric': 0.17250593824228028, 'fraction_numerical': 0.004750593824228029, 'mean_word_length': 3.1286764705882355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7476760', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1033, 'n_words': 234}