com.cutunes.server
Class Analyzer

java.lang.Object
  extended bycom.cutunes.server.Analyzer

public class Analyzer
extends java.lang.Object

Handles calculating compatibility ratings between users, as well generating reccomendations for new music. The algorithm first calculates a musical similarity rating between all users using the following equation: Similarity = sum over all songs of 100*(1 - Math.abs(A - B)) * A * B; where A and B are percentages that each user plays that song. Reccomendations are calculated by finding music which you dont have in the libraries of other users to whom you are most similar.

Author:
blake

Field Summary
static int SIMILARITY_CUT_OFF
          between 1 and 100, can be used to limit complexity/improve results
 
Constructor Summary
Analyzer(java.lang.String username)
          Connects to database and calculates recs and compatibility
 
Method Summary
 int calculateOrigRating(int[] sims)
          Calculate Musical Originality Rating Currently simply 1 - the average of all of your compatibility percentages
 void clearRecsForUser(java.lang.String type, java.lang.String playCountType)
          Clear recs for current user
 void clearSimForUser()
          Clear the similarity values for current user in db
 void clearSimWeekForUser()
          Clear the this week similarity values for current user in db
 double compareUsers(int userID1, int userID2, java.lang.String type, java.lang.String playCountType)
          This method compares two users It works by finding all of the songs/albums/artists in common And then goes through that list, taking the play count for each, and normalizing it by that users total play count.
 void getNNewItems(int userID2, int n, java.util.Hashtable myItems, java.util.Hashtable newItems, int simRank, java.lang.String type, java.lang.String playCountType)
          Gets N new songs/albums/artists from another user This method should probably be replaced by a more efficient sql query Scores for recs are weighted by similarity with that user
 void getReccomendaton(java.lang.String username, java.lang.String type, java.lang.String playCountType, int[] similarity)
          Gets reccomendations for current user and loads them into the db
 int[] getSimilarityArray(java.lang.String username, java.lang.String type, java.lang.String playCountType)
          Builds the similarity array for the current user vs.
 java.util.Vector getSortedFromHash(java.util.Hashtable h, int n)
          This method returns the recs sorted by their score Note this method is written horribly, probably the worst sorting algorithm ever, however it will be replaced soon when we load the recs back into the DB
 int getTotalPlays(int userID, java.lang.String playCountType)
          Get total play counts for a user
 int getUserID(java.lang.String user)
          Get a user id from a name
 java.lang.String getUserName(int id)
          Get a name from a user id
 java.util.Vector getUserNames()
          get list of usernames
 java.util.Hashtable loadHashtable(int userID1, java.lang.String type, java.lang.String playCountType)
          This method loads a hashtable with all of the users song It is used to identify songs in common, and quickly retrieve play counts
 void loadInfoIntoDB(java.lang.String username)
          Calculates and loads information for user
 void loadRecIntoDB(java.lang.String s, int r, java.lang.String type, java.lang.String playCountType)
          Load a reccomendation into the db for current user
 void loadSimilarityIntoDB(int userID2, int sim)
          Loads a value into the sql database for similarity between two users.
 void loadSimilarityWeekIntoDB(int userID2, int sim)
          Loads a value into the sql database for similarity between two users for the this week timeframe.
static void main(java.lang.String[] args)
          Main Method
 void printAllInfo(java.lang.String username)
          Prints all info for a user: Musical Originality, Recs for Artists, Albums, and Songs both week and all time view
 void printDebugHTML()
          Print some html making it easy to look through peoples recs
 void printrecommendation(java.lang.String username, java.lang.String type, java.lang.String playCountType)
          prints recommendations for a given user by finding new music from similar people
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIMILARITY_CUT_OFF

public static final int SIMILARITY_CUT_OFF
between 1 and 100, can be used to limit complexity/improve results

See Also:
Constant Field Values
Constructor Detail

Analyzer

public Analyzer(java.lang.String username)
Connects to database and calculates recs and compatibility

Parameters:
username - if username = all, calculates recs for all users
Method Detail

loadInfoIntoDB

public void loadInfoIntoDB(java.lang.String username)
Calculates and loads information for user

Parameters:
username -

loadSimilarityIntoDB

public void loadSimilarityIntoDB(int userID2,
                                 int sim)
Loads a value into the sql database for similarity between two users. Note: user1 is the current user (global variable)

Parameters:
userID2 -
sim -

loadSimilarityWeekIntoDB

public void loadSimilarityWeekIntoDB(int userID2,
                                     int sim)
Loads a value into the sql database for similarity between two users for the this week timeframe. Note: user1 is the current user (global variable)

Parameters:
userID2 -
sim -

printAllInfo

public void printAllInfo(java.lang.String username)
Prints all info for a user: Musical Originality, Recs for Artists, Albums, and Songs both week and all time view

Parameters:
username -

getSimilarityArray

public int[] getSimilarityArray(java.lang.String username,
                                java.lang.String type,
                                java.lang.String playCountType)
Builds the similarity array for the current user vs. all other users

Parameters:
username -
type - name (song name), album, or artist
playCountType - play_count or play_count_week
Returns:
similarity

getReccomendaton

public void getReccomendaton(java.lang.String username,
                             java.lang.String type,
                             java.lang.String playCountType,
                             int[] similarity)
Gets reccomendations for current user and loads them into the db

Parameters:
username -
type - name (song name), album, or artist
playCountType - play_count or play_count_week
similarity -

clearSimForUser

public void clearSimForUser()
Clear the similarity values for current user in db


clearSimWeekForUser

public void clearSimWeekForUser()
Clear the this week similarity values for current user in db


clearRecsForUser

public void clearRecsForUser(java.lang.String type,
                             java.lang.String playCountType)
Clear recs for current user

Parameters:
type - name (song name), album, artist
playCountType - play_count or play_count_week

loadRecIntoDB

public void loadRecIntoDB(java.lang.String s,
                          int r,
                          java.lang.String type,
                          java.lang.String playCountType)
Load a reccomendation into the db for current user

Parameters:
s - the reccomendation to load
r - the value for how strong the reccomendation is
type - name (song name), album, artist
playCountType - play_count or play_count_week

printrecommendation

public void printrecommendation(java.lang.String username,
                                java.lang.String type,
                                java.lang.String playCountType)
prints recommendations for a given user by finding new music from similar people

Parameters:
username -
type - name (song name), album, artist
playCountType - play_count or play_count_week

getNNewItems

public void getNNewItems(int userID2,
                         int n,
                         java.util.Hashtable myItems,
                         java.util.Hashtable newItems,
                         int simRank,
                         java.lang.String type,
                         java.lang.String playCountType)
Gets N new songs/albums/artists from another user This method should probably be replaced by a more efficient sql query Scores for recs are weighted by similarity with that user

Parameters:
userID2 - The other user to get recs from
n - number of items to collect
myItems - hashtable of current users music
newItems - hashtable of new items
simRank - similarity with other user
type - name (song name), album, artist
playCountType - play_count or play_count_week

calculateOrigRating

public int calculateOrigRating(int[] sims)
Calculate Musical Originality Rating Currently simply 1 - the average of all of your compatibility percentages

Parameters:
sims -
Returns:
origRating

getSortedFromHash

public java.util.Vector getSortedFromHash(java.util.Hashtable h,
                                          int n)
This method returns the recs sorted by their score Note this method is written horribly, probably the worst sorting algorithm ever, however it will be replaced soon when we load the recs back into the DB

Parameters:
h -
n -
Returns:
sortedList

loadHashtable

public java.util.Hashtable loadHashtable(int userID1,
                                         java.lang.String type,
                                         java.lang.String playCountType)
This method loads a hashtable with all of the users song It is used to identify songs in common, and quickly retrieve play counts

Parameters:
userID1 -
type - name (song name), album, artist
playCountType - play_count or play_count_week
Returns:
userHashTable

compareUsers

public double compareUsers(int userID1,
                           int userID2,
                           java.lang.String type,
                           java.lang.String playCountType)
This method compares two users It works by finding all of the songs/albums/artists in common And then goes through that list, taking the play count for each, and normalizing it by that users total play count. these percentages are then differenced, and then inverted by taking 1 - the resulting difference. this number is then weighted by the normalized play counts for both users, so that items that are more important to the users, get weighted more heavily

Parameters:
userID1 -
userID2 -
type - name (song name), album, artist
playCountType - play_count or play_count_week
Returns:
simRating

getTotalPlays

public int getTotalPlays(int userID,
                         java.lang.String playCountType)
Get total play counts for a user

Parameters:
userID -
playCountType -
Returns:
totalPlays

getUserID

public int getUserID(java.lang.String user)
Get a user id from a name

Parameters:
user -
Returns:
userID

getUserNames

public java.util.Vector getUserNames()
get list of usernames

Returns:
usernames

getUserName

public java.lang.String getUserName(int id)
Get a name from a user id

Parameters:
id -
Returns:
username

printDebugHTML

public void printDebugHTML()
Print some html making it easy to look through peoples recs


main

public static void main(java.lang.String[] args)
Main Method

Parameters:
args - a username of "all"