   (:require [clojure.edn :as edn])
   (:require [clojure.pprint :refer (pprint)]))
 
+;----- Helper Functions
 
 (defn load-data
   "Loads the content of a file as EDN formatted data structure."

      false
      (Character/isUpperCase (first word)))))

(defn map-group-items [f [k xs]] [k (map f xs)])

(defn map-pair-value [f [k v]] [k (f v)])

(defn sum [xs] (apply + xs))

(defn squared-sum [xs] (apply + (map #(* % %) xs)))


;----------------------------

;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\11.01 Theoretische, technische, praktische, angewandte Informatik.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\12.01.1 Datenstrukturen, Array, Queue, Stack.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\12.01.2 Baum als Datenstruktur.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Algorithmen und Datenstrukturen 001.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Algorithmen und Datenstrukturen 002.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Algorithmen und Datenstrukturen 003.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Algorithmen und Datenstrukturen 004.clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Bernd Senf 3. Bankgeheimnis Geldschöpfung - Monetative als Lösung (720).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Binomialverteilung_Formel von Bernoulli, Stochastik, Nachhilfe online, Hilfe in Mathe (720).clj")
(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Der Lambda-Kalkül (720).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Einseitiger(rechtsseitiger) Hypothesentest_mit Ablesen aus der Tabelle, Stochastik, Nachhilfe online (720).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\IS-Kurve im Vier-Quadrantenschema Die Herleitung (720).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Gauß-Algorithmus_Lineares Gleichungssystem lösen (einfach_schnell erklärt), Nachhilfe online (720).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Makroökonomie online lernen - VWL Tutorium (360).clj")
;(def path "D:\\Daten\\FH\\OLL\\Media\\Audio\\de-DE\\Parabeln_Quadratische Funktion_en Übersicht (Scheitelpunkt,Stauchung,Streckung,etc.) (720).clj")

(def results (load-data path)) ;; Load the recgnition results
(def phrases (map best-alternate results)) ;; Extract the best phrases from the alternates
;(def phrases (mapcat :alternates results)) ;; Take all phrases from the results
(def words (mapcat :words phrases)) ;; Extract the words (text, confidence)
(def nouns (filter noun? words)) ;; Only take words with first character upper case

(def grouped-nouns
  (let
    [groups (group-by :text nouns) ;; Group words by text
     confidences (map (partial map-group-items :confidence) groups) ;; Reject additional text per confidence
     squaresums (map (partial map-pair-value squared-sum) confidences) ;; Build square sums of confidence per word
     ordered (reverse (sort-by #(get % 1) squaresums))] ;; Sort the words by sum of squared confidence
    ordered))

(pprint (take 20 grouped-nouns)) ;; Print the head of the resulting list

  (with-open
    [r (java.io.PushbackReader. (reader path))]
    (edn/read r)))

(defn map-group-items
  "Applies a function to the items of the group collection."
  [f [k xs]] [k (map f xs)])

(defn map-pair-value
  "Applies a function to the value of a key-value-pair."
  [f [k v]] [k (f v)])

(defn multi-filter
  "Applies a number of predicates to a value and returns true if all predicates are true."
  [predicates x]
  (if (empty? predicates)
    true
    (every? true? ((apply juxt predicates) x))))

(defn sum
  "Computes the sum of a sequence."
  [xs] (apply + xs))

(defn squared-sum
  "Computes the sum of the squared items of a sequence."
  [xs] (apply + (map #(* % %) xs)))

;----- Domain Logic

(defn best-alternate
  "Return the best alternate phrase of a recgnition result."
