Skip to content

Commit

Permalink
#33 - reinstated old autocomplete code
Browse files Browse the repository at this point in the history
- the trie library has been fixed
- there was no issue now anyway, since the frontend uses shadow-cljs.edn
  • Loading branch information
simongray committed Oct 26, 2022
1 parent 7f9778b commit a09cafd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
43 changes: 22 additions & 21 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{:paths ["src/main" "src/dev" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/data.csv {:mvn/version "1.0.1"}
org.clojure/data.json {:mvn/version "2.4.0"}
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/data.csv {:mvn/version "1.0.1"}
org.clojure/data.json {:mvn/version "2.4.0"}

arachne-framework/aristotle {:git/url "https://github.com/kuhumcst/aristotle.git"
:sha "7c840ee7ee391963717c18c98789fc1677bcb6ef"}
;; NOTE: version 0.1.3 and above use Jena 4 which is incompatible with Aristotle.
ont-app/igraph-jena {:mvn/version "0.1.3"}
ont-app/vocabulary {:mvn/version "0.1.7"}
org.flatland/ordered {:mvn/version "1.15.10"}
better-cond/better-cond {:mvn/version "2.1.5"}
org.clojure/core.memoize {:mvn/version "1.0.257"}
metosin/reitit {:mvn/version "0.5.18"}}
:aliases {:backend {:extra-deps {io.pedestal/pedestal.service {:mvn/version "0.5.10"}
io.pedestal/pedestal.route {:mvn/version "0.5.10"}
io.pedestal/pedestal.jetty {:mvn/version "0.5.10"}
ring/ring-core {:mvn/version "1.9.5"}
#_#_com.owoga/tightly-packed-trie {:mvn/version "0.2.3"}
org.slf4j/slf4j-simple {:mvn/version "1.7.36"}
com.wsscode/transito {:mvn/version "2021.07.04"}
rum/rum {:mvn/version "0.12.9"}}}
:prototypes {:extra-paths ["src/prototypes"]
arachne-framework/aristotle {:git/url "https://github.com/kuhumcst/aristotle.git"
:sha "7c840ee7ee391963717c18c98789fc1677bcb6ef"}
ont-app/igraph-jena {:mvn/version "0.1.3"}
ont-app/vocabulary {:mvn/version "0.1.7"}

org.flatland/ordered {:mvn/version "1.15.10"}
better-cond/better-cond {:mvn/version "2.1.5"}
org.clojure/core.memoize {:mvn/version "1.0.257"}
metosin/reitit {:mvn/version "0.5.18"} ; for url-encode

io.pedestal/pedestal.service {:mvn/version "0.5.10"}
io.pedestal/pedestal.route {:mvn/version "0.5.10"}
io.pedestal/pedestal.jetty {:mvn/version "0.5.10"}
ring/ring-core {:mvn/version "1.9.5"}
com.owoga/tightly-packed-trie {:mvn/version "0.3.0"}
org.slf4j/slf4j-simple {:mvn/version "1.7.36"}
com.wsscode/transito {:mvn/version "2021.07.04"}
rum/rum {:mvn/version "0.12.9"}}
:aliases {:prototypes {:extra-paths ["src/prototypes"]
:extra-deps {gorillalabs/neo4j-clj {:mvn/version "4.1.0"}
clj-http {:mvn/version "3.10.3"}
ubergraph {:mvn/version "0.8.2"}}}
Expand Down
72 changes: 36 additions & 36 deletions src/main/dk/cst/dannet/web/resources.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[ring.util.response :as ring]
[ont-app.vocabulary.lstr]
[rum.core :as rum]
[com.owoga.trie :as trie]
[dk.cst.dannet.web.i18n :as i18n]
[dk.cst.dannet.prefix :as prefix]
[dk.cst.dannet.db :as db]
Expand Down Expand Up @@ -283,49 +284,48 @@
(def dannet-route
["/dannet" :get dannet-metadata-redirect :route-name ::dannet])

#_(def autocomplete-path
(str (prefix/uri->path prefix/dannet-root) "autocomplete"))
(def autocomplete-path
(str (prefix/uri->path prefix/dannet-root) "autocomplete"))

;; TODO: should be transformed into a tightly packed tried (currently loose)
#_(defonce search-trie
(future
(let [words (q/run (:graph @db) '[?writtenRep] op/written-representations)]
(apply trie/make-trie (mapcat concat words words)))))

#_(defn autocomplete
"Return autocompletions for `s` found in the graph."
[s]
(->> (trie/lookup @search-trie s)
(remove (comp nil? second)) ; remove partial
(map second) ; grab full words
(sort)))

#_(def autocomplete* (memoize autocomplete))

#_(def autocomplete-ic
{:name ::autocomplete
:leave (fn [{:keys [request] :as ctx}]
(let [;; TODO: why is decoding necessary?
;; You would think that the path-params-decoder handled this.
s (-> request
(get-in [:query-params :s])
(decode-query-part))]
(if (> (count s) 2)
(defonce search-trie
(future
(let [words (q/run (:graph @db) '[?writtenRep] op/written-representations)]
(apply trie/make-trie (map str (mapcat concat words words))))))

(defn autocomplete*
"Return autocompletions for `s` found in the graph."
[s]
(->> (trie/lookup @search-trie s)
(remove (comp nil? second)) ; remove partial
(map second) ; grab full words
(sort)))

(def autocomplete (memoize autocomplete*))

(def autocomplete-ic
{:name ::autocomplete
:leave (fn [{:keys [request] :as ctx}]
(let [;; TODO: why is decoding necessary?
;; You would think that the path-params-decoder handled this.
s (get-in request [:query-params :s])]
(when-let [s' (and s (decode-query-part s))]
(if (> (count s') 2)
(-> ctx
(update :response assoc
:status 200
:body (str/join "\n" (autocomplete* s)))
:body (str/join "\n" (autocomplete s')))
(update-in [:response :headers] assoc
"Content-Type" "text/plain"
"Cache-Control" one-day-cache))
(update ctx :response assoc
:status 404
:headers {}))))})
:status 204
:headers {})))))})

#_(def autocomplete-route
[autocomplete-path
:get [autocomplete-ic]
:route-name ::autocomplete])
(def autocomplete-route
[autocomplete-path
:get [autocomplete-ic]
:route-name ::autocomplete])

(comment
(q/expanded-entity (:graph @db) :dn/form-11029540-land)
Expand All @@ -348,9 +348,9 @@
(count (q/run (:graph @db) op/unlabeled-senses))

;; Testing autocompletion
(autocomplete "sar")
(autocomplete "spo")
(autocomplete "tran")
(autocomplete* "sar")
(autocomplete* "spo")
(autocomplete* "tran")

;; Look up synsets based on the lemma "have"
(db/look-up (:graph @db) "have")
Expand Down
2 changes: 1 addition & 1 deletion src/main/dk/cst/dannet/web/service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#{res/root-route
res/dannet-route
res/search-route
#_res/autocomplete-route
res/autocomplete-route
res/external-entity-route
res/unknown-external-entity-route

Expand Down

0 comments on commit a09cafd

Please sign in to comment.