<commit_before>(ns babel.engine
  (:refer-clojure :exclude [get-in merge])
  (:use [hiccup core page])
  (:require
   [clojure.data.json :as json]
   [clojure.string :as string]
   [clojure.tools.logging :as log]
   [compojure.core :as compojure :refer [GET PUT POST DELETE ANY]]
   [dag-unify.core :refer [fail? get-in merge strip-refs unify unifyc]]

   [babel.forest :as forest]
   [babel.html :refer [tablize]]
   [babel.ug :refer (head-principle)]))

(declare lookup)
(declare generate-from-request)
(declare resolve-model)

(def routes
  (compojure/routes
   (GET "/lookup" request
       (lookup request))

  (GET "/generate" request
       (generate-from-request request))))

;; TODO: use a option map/destructuring thing.
(defn generate [spec language-model & [{do-enrich :do-enrich}]]
  (let [do-enrich (if do-enrich do-enrich true)
        spec (unify spec
                    {:synsem {:subcat '()}})
        language-model (if (future? language-model)
                         @language-model
                         language-model)

        debug (log/debug (str "pre-enriched spec: " spec))

        spec (if (and do-enrich (:enrich language-model))
               ((:enrich language-model)
                spec)
               spec)]
    (log/debug (str "calling forest/generate with spec: " (if (seq? spec)
                                                            (string/join "," spec)
                                                            spec)))
    (forest/generate spec 
                     (:grammar language-model)
                     (:lexicon language-model)
                     (:index language-model))))

(defn generate-from-request [request]
  "respond to an HTTP client's request with a generated sentence, given the client's desired spec, language name, and language model name."
  ;; TODO: 'pred' request param here is deprecated: use spec's {:synsem {:sem {:pred}}} instead.
  (let [pred (keyword (get-in request [:params :pred] :top))
        spec (get-in request [:params :spec])
        spec (if spec (json/read-str spec
                                     :key-fn keyword
                                     :value-fn (fn [k v]
                                                 (cond (string? v)
                                                       (keyword v)
                                                       :else v)))
                 :top)<commit_msg>add ‘morph’ argument so that arguments can be printed as words rather than feature structures<commit_after>(ns babel.engine
  (:refer-clojure :exclude [get-in merge])
  (:use [hiccup core page])
  (:require
   [clojure.data.json :as json]
   [clojure.string :as string]
   [clojure.tools.logging :as log]
   [compojure.core :as compojure :refer [GET PUT POST DELETE ANY]]
   [dag-unify.core :refer [fail? get-in merge strip-refs unify unifyc]]

   [babel.forest :as forest]
   [babel.html :refer [tablize]]
   [babel.ug :refer (head-principle)]))

(declare lookup)
(declare generate-from-request)
(declare resolve-model)

(def routes
  (compojure/routes
   (GET "/lookup" request
       (lookup request))

  (GET "/generate" request
       (generate-from-request request))))

;; TODO: use a option map/destructuring thing.
(defn generate [spec language-model & [{do-enrich :do-enrich}]]
  (let [do-enrich (if do-enrich do-enrich true)
        spec (unify spec
                    {:synsem {:subcat '()}})
        language-model (if (future? language-model)
                         @language-model
                         language-model)
        morph (if (future? language-model)
                (:morph @language-model)
                (:morph language-model))

        debug (log/debug (str "pre-enriched spec: " spec))

        spec (if (and do-enrich (:enrich language-model))
               ((:enrich language-model)
                spec)
               spec)]
    (log/debug (str "calling forest/generate with spec: " (if (seq? spec)
                                                            (string/join "," spec)
                                                            spec)))
    (forest/generate spec 
                     (:grammar language-model)
                     (:lexicon language-model)
                     (:index language-model)
                     morph)))

(defn generate-from-request [request]
  "respond to an HTTP client's request with a generated sentence, given the client's desired spec, language name, and language model name."
  ;; TODO: 'pred' request param here is deprecated: use spec's {:synsem {:sem {:pred}}} instead.
  (let [pred (keyword (get-in request [:params :pred] :top))
        spec (get-in request [:params :spec])
        spec (if spec (json/read-str spec
                                     :key-fn keyword
                                     :value-fn (fn [k v]
                                                 (cond (string? v)
                                                       (keyword v)
                                                       :else v)))
                 :top)
{'fraction_non_alphanumeric': 0.11363636363636363, 'fraction_numerical': 0.0, 'mean_word_length': 1.5795107033639144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7180480', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1325, 'n_words': 420}