      (let [in-host-xpaths-hrefs (state-action page-src url blacklist)
            xpaths-tokenized     (tokenize-actions in-host-xpaths-hrefs)
            xpaths-scored        (score-actions xpaths-tokenized)
-           decision             (first (first (reverse (sort-by second xpaths-scored))))]
+           decision             (first
+                                 (first
+                                  (reverse
+                                   (sort-by second xpaths-scored))))]
        (println :url url)
        (println :decision decision)
        {:links (distinct (map #(-> % :href) (in-host-xpaths-hrefs decision)))
         :action-score (xpaths-scored decision)})))
+
+(defn extract-above-average-richest
+  ([page-src url]
+     (extract-above-average-richest page-src url (set [])))
+
+  ([page-src url blacklist]
+     (let [in-host-xpaths-hrefs (state-action page-src url blacklist)
+           xpaths-tokenized     (tokenize-actions in-host-xpaths-hrefs)
+           xpaths-scored        (score-actions xpaths-tokenized)
+           mean-richness        (/ (apply + (map second xpaths-scored))
+                                   (count xpaths-scored))
+           decision             (filter
+                                 (fn [[xpath score]]
+                                   (> score mean-richness))
+                                 (reverse
+                                  (sort-by second xpaths-scored)))
+           decision-links       (flatten
+                                 (map
+                                  (fn [[a-decision score]]
+                                    (map
+                                     #(-> % :href)
+                                     (in-host-xpaths-hrefs a-decision)))
+                                  decision))
+
+           decision-scores      (/ (reduce
+                                    (fn [acc [a-decision score]]
+                                      (+ acc score))
+                                    0
+                                    decision)
+                                   (count decision))]
+       {:links decision-links
+        :action-score decision-scores
+        :xpaths (map first decisions)})))

       in-host-xhrefs)))

(defn tokenize-actions
  [in-host-xhrefs]
  (into
   {} (map
       (fn [[xpath nodes]]
         [xpath (tokenize-anchor-url nodes)])
       in-host-xhrefs)))

(defn extract-richest
  "Retrieve from possible actions, the xpath with the highest score

  "Implementation of the rich URL extractor"
  (:require [crawler.dom :as dom]
            [crawler.rank :as rank]
            [crawler.similarity :as sim]
            [crawler.utils :as utils]
            [clj-http.client :as client]
            [clojure.set :as clj-set]
            [org.bovinegenius [exploding-fish :as uri]]))

