Skip to content

Commit

Permalink
disabled content-based modeling
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobobryant committed Aug 12, 2017
1 parent 6e9929c commit 0c92b67
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 55 deletions.
106 changes: 54 additions & 52 deletions reco/src/reco/reco.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
ratio n score
content-ratio content-n content-score])

(defrecord Song [artist album title source spotify_id duration
danceability
energy
mode
speechiness
acousticness
instrumentalness
liveness
valence])
(defrecord Song [artist album title source spotify_id duration])
;danceability
;energy
;mode
;speechiness
;acousticness
;instrumentalness
;liveness
;valence])

(defn dbg [desc arg]
(when debug
Expand Down Expand Up @@ -87,7 +87,9 @@

(defn ses-append [sessions song-id skipped create-session]
(if create-session
(cons {song-id skipped} sessions)
; song id -1 represents the empty session. It gives the model something to
; work with when a session is justs getting started.
(cons {-1 false song-id skipped} sessions)
(if (contains? (first sessions) song-id)
sessions
(cons (assoc (first sessions) song-id skipped)
Expand Down Expand Up @@ -142,49 +144,50 @@
(* sim (if skipped -1 1)))))


(defn dot-product [vectors]
(reduce + (apply map * vectors)))

(defn norm [v]
(Math/sqrt (reduce + (map #(* % %) v))))

(defn cosine [vectors]
(/ (dot-product vectors)
(apply * (map norm vectors))))

(defn feature-sim [library a b skipped]
(let [features [:danceability
:energy
:mode
:speechiness
:acousticness
:instrumentalness
:liveness
:valence]
vectors (map (fn [song-id]
(map (fn [feature] (get-in library [song-id feature]))
features))
[a b])
distance (cosine vectors)
sim (- (* distance 2) 1)]
;(println "feature distance between" (get-in library [a :title]) "and"
; (get-in library [b :title]) "is" distance)
(if (and skipped (< sim 0))
nil
(* sim (if skipped -1 1)))))
;(defn dot-product [vectors]
; (reduce + (apply map * vectors)))
;
;(defn norm [v]
; (Math/sqrt (reduce + (map #(* % %) v))))
;
;(defn cosine [vectors]
; (/ (dot-product vectors)
; (apply * (map norm vectors))))
;
;(defn feature-sim [library a b skipped]
; (let [features [:danceability
; :energy
; :mode
; :speechiness
; :acousticness
; :instrumentalness
; :liveness
; :valence]
; vectors (map (fn [song-id]
; (map (fn [feature] (get-in library [song-id feature]))
; features))
; [a b])
; distance (cosine vectors)
; sim (- (* distance 2) 1)]
; ;(println "feature distance between" (get-in library [a :title]) "and"
; ; (get-in library [b :title]) "is" distance)
; (if (and skipped (< sim 0))
; nil
; (* sim (if skipped -1 1)))))

(defn update-cand [[cand-id data] library model other-id skipped]
[cand-id
(let [sim (sim-score model other-id cand-id skipped)
cand-artist (get-in library [cand-id :artist])
other-artist (get-in library [other-id :artist])
content-sim (if (every? some? (map #(get-in library [% :mode])
[cand-id other-id]))
(feature-sim library cand-id other-id skipped)
(let [s (sim-score model other-artist cand-artist skipped)]
(if s
(min s (if (= other-artist cand-artist) 1 0.8))
nil)))]
content-sim (sim-score model other-artist cand-artist skipped)]
;(if (every? some? (map #(get-in library [% :mode])
; [cand-id other-id]))
; (feature-sim library cand-id other-id skipped)
; (let [s (sim-score model other-artist cand-artist skipped)]
; (if s
; (min s (if (= other-artist cand-artist) 1 0.8))
; nil)))]
(cond-> data
sim (update-score :collaborative sim)
content-sim (update-score :content content-sim)))])
Expand All @@ -198,18 +201,17 @@
(into {} (map #(update-cand % library model song-id skipped) candidates))))

(defn penalty [delta strength]
(let [ret (- 1 (/ 1 (Math/exp (/ delta strength))))]
ret))
(- 1 (/ 1 (Math/exp (/ delta strength)))))

(defn predicted [deltas strength]
(let [ret (reduce * (map #(penalty % strength) deltas))]
ret))
(reduce * (map #(penalty % strength) deltas)))

(defn total-error [input-data strength]
(reduce + (map #(Math/pow (- (predicted (:deltas %) strength)
(:observed %)) 2) input-data)))

(def strength-set [0.2 0.5 1 1.5 2 3 5 8 13 21])
;(def strength-set [0.2 0.5 1 1.5 2 3 5 8 13 21])
(def strength-set [0.5 3 14])
(defn calc-freshness [event-vec]
(let [get-deltas (fn [i event] {:observed (if (:skipped event) 0 1)
:deltas (map #(- (:day event) (:day %))
Expand Down
5 changes: 3 additions & 2 deletions src/com/jacobobryant/moody/Moody.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private void init() {

// get library
SQLiteDatabase db = new Database(context).getReadableDatabase();
result = db.rawQuery("SELECT * FROM songs", null);
result = db.rawQuery("SELECT _id, artist, album, title, source, " +
"spotify_id, duration FROM songs", null);
rec = new reco(cursor_to_maps(result));
result.close();

Expand Down Expand Up @@ -138,7 +139,7 @@ public void update(Song last_song, boolean skipped) {
String.valueOf(algorithm)});
rec.add_event(id, skipped);
} else {
Log.e(C.TAG, "couldn't add song event");
Log.e(C.TAG, "couldn't find song in database");
}
result.close();
db.close();
Expand Down
2 changes: 1 addition & 1 deletion src/com/jacobobryant/moody/vanilla/SongTimeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ private void shiftCurrentSongInternal(int delta, boolean skipped)
if (num != 0) {
break;
}
Log.e(C.TAG, "couldn't add song: " + next);
Log.e(C.TAG, "couldn't add song: " + next.title);
moody.rec.add_to_blacklist(next.id);
}
} while (true);
Expand Down

0 comments on commit 0c92b67

Please sign in to comment.