Skip to content

Instantly share code, notes, and snippets.

@llasram
Last active December 22, 2015 12:28
Show Gist options
  • Save llasram/6472144 to your computer and use it in GitHub Desktop.
Save llasram/6472144 to your computer and use it in GitHub Desktop.
lenskit-hello
(ns lenskit-hello.core
(:require [clojure.java.io :as io])
(:import [org.grouplens.lenskit
ItemRecommender ItemScorer Recommender RecommenderBuildException]
[org.grouplens.lenskit.baseline
BaselineScorer ItemMeanRatingItemScorer UserMeanBaseline
UserMeanItemScorer]
[org.grouplens.lenskit.core
LenskitConfiguration LenskitRecommender]
[org.grouplens.lenskit.cursors Cursors]
[org.grouplens.lenskit.data.dao
EventCollectionDAO EventDAO SimpleFileRatingDAO]
[org.grouplens.lenskit.knn.item ItemItemScorer]
[org.grouplens.lenskit.transform.normalize
BaselineSubtractingUserVectorNormalizer UserVectorNormalizer]))
(defn run
[delim inpath & users]
(let [base (SimpleFileRatingDAO. (io/file inpath) delim)
dao (EventCollectionDAO. (-> base .streamEvents Cursors/makeList))
config (doto (LenskitConfiguration.)
(-> (.bind EventDAO) (.to dao))
(-> (.bind ItemScorer) (.to ItemItemScorer))
(-> (.bind BaselineScorer ItemScorer) (.to UserMeanItemScorer))
(-> (.bind UserMeanBaseline, ItemScorer)
(.to ItemMeanRatingItemScorer))
(-> (.bind UserVectorNormalizer)
(.to BaselineSubtractingUserVectorNormalizer)))
rec (LenskitRecommender/build config)
irec (.getItemRecommender rec)]
(doseq [user users, item (.recommend irec user 10)]
(->> item .getId (format "\t%d\n") print))))
(defn -main
[& args]
(let [[delim args] (if (= "-d" (first args))
[(second args) (drop 2 args)]
["\t" args])
inpath (first args)
users (map #(Long/parseLong %) (rest args))]
(apply run delim inpath users)))
(defproject lenskit-hello "0.1.0-SNAPSHOT"
:description "Clojure version of the grouplens lenskit-hello demo."
:url "https://bitbucket.org/grouplens/lenskit-hello"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:main ^:skip-aot lenskit-hello.core
:dependencies [[org.clojure/clojure "1.5.1"]
[org.grouplens.lenskit/lenskit-knn "2.0"]
[org.slf4j/slf4j-api "1.7.5"]
[org.slf4j/slf4j-log4j12 "1.7.5"]
[log4j "1.2.17"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment