         (cons (string/replace infinitive from to)
               (do-replace-on infinitive (rest (rest patterns))))
         (do-replace-on infinitive (rest (rest patterns)))))))
+
+(defn group-by-two [remaining]
+  (if (not (empty? (rest remaining)))
+    (cons
+     [(nth remaining 0)
+      (nth remaining 1)]
+     (group-by-two (rest (rest remaining))))))

  (:require [clojure.tools.logging :as log]
            [clojure.string :as string]
            [dag_unify.core :refer [fail? strip-refs unify]]))

(defn analyze [surface-form lexicon replace-patterns]
  "Analyze a single surface form into a set of lexical forms."
  (concat (if (get lexicon surface-form)
            (get lexicon surface-form))
          (mapcat
           (fn [replace-pattern]
             (let [ ;; regular expression that matches the surface form
                   from (nth (:p replace-pattern) 0)]
               (log/debug (str "matching replace-pattern:" replace-pattern " against surface-form: " surface-form))
               (if (re-matches from surface-form)
                 (let [;; expression that is used by string/replace along with the first regexp and the surface form,
                       ;; to create the lexical string
                       to (nth (:p replace-pattern) 1)

                       ;; unifies with the lexical entry to create the inflected form.
                       unify-with (if (:u replace-pattern)
                                    (:u replace-pattern)
                                    :top) ;; default unify-with
                     
                       lex (string/replace surface-form from to)]
                   (filter (fn [result] (not (= :fail result)))
                           (map (fn [lexical-entry]
                                  (unify unify-with lexical-entry))
                                (get lexicon lex)))))))
           replace-patterns)))

(defn conjugate [infinitive unify-with replace-patterns]

                          (string/replace infinitive from to)))))
                  replace-patterns)))))

(defn compile-patterns [unify-with patterns]
  (map (fn [{g :g agr :agr p :p u :u}]
         (let [u (unify unify-with
                        (or agr :top)
                        (or u :top))]
           {:u u
            :g g
            :agr agr
            :p p}))
       patterns))

(defn do-replace-on [infinitive patterns]
