public static void CalculateAllSimilarities(MyDatabase mydb) { List<ArtistInfo> artists = GetArtists(mydb, -1); mydb.newQuery("DELETE FROM ArtistSimilarity"); mydb.ExecuteNonQuery(); Dictionary<KeyValuePair<int, int>, double> totalSimilarity = new Dictionary<KeyValuePair<int, int>, double>(); for (int j = 0; j < MAX_STEPS; ++j) { List<ArtistInfo> prevArtists = new List<ArtistInfo>(); for (int i = 0; i < artists.Count; ++i) { System.Diagnostics.Debug.WriteLine(artists[i].ArtistId + " " + artists[i].ArtistName); Dictionary<int, double> currentSimilarity = CalculateSimilarities(mydb, artists[i], true, prevArtists); foreach (KeyValuePair<int, double> entry in currentSimilarity) { KeyValuePair<int, int> pair = new KeyValuePair<int, int>(entry.Key, artists[i].ArtistId); if (!totalSimilarity.ContainsKey(pair)) totalSimilarity.Add(pair, 0); totalSimilarity[pair] += entry.Value; pair = new KeyValuePair<int, int>(artists[i].ArtistId, entry.Key); if (!totalSimilarity.ContainsKey(pair)) totalSimilarity.Add(pair, 0); totalSimilarity[pair] += entry.Value; } prevArtists.Add(artists[i]); } artists = Shuffle(artists); mydb.newQuery("DELETE FROM ArtistSimilarity"); mydb.ExecuteNonQuery(); } foreach (KeyValuePair<KeyValuePair<int, int>, double> entry in totalSimilarity) StoreSimilarity(mydb, entry.Key.Key, entry.Key.Value, entry.Value / MAX_STEPS); for (int i = 0; i < artists.Count; ++i) StoreSimilarity(mydb, artists[i].ArtistId, artists[i].ArtistId, 1); }
public static Dictionary<int, double> CalculateSimilarities(MyDatabase mydb, ArtistInfo Artist, bool batchMode = false, List<ArtistInfo> prevArtists = null) { if (!batchMode) { mydb.newQuery("DELETE FROM ArtistSimilarity WHERE artist1_id=@artist_id OR artist2_id=@artist_id"); mydb.setParameter("artist_id", Artist.ArtistId); mydb.ExecuteNonQuery(); } Dictionary<String, int> similarArtists = new Dictionary<string, int>(); GetEchonestData(similarArtists, Artist.ArtistName); GetLastFMData(similarArtists, Artist.ArtistName); GetYahooData(similarArtists, Artist.ArtistName); if (!batchMode) prevArtists = GetArtists(mydb, Artist.ArtistId); Dictionary<int, double> similarityNumerator = new Dictionary<int, double>(); Dictionary<int, double> similarityDenominator = new Dictionary<int, double>(); Dictionary<int, double> similarity = new Dictionary<int, double>(); for (int i = 0; i < prevArtists.Count; ++i) { similarityNumerator.Add(prevArtists[i].ArtistId, 0); similarityDenominator.Add(prevArtists[i].ArtistId, 0); if (!similarArtists.ContainsKey(prevArtists[i].ArtistName)) similarity.Add(prevArtists[i].ArtistId, 0); else similarity.Add(prevArtists[i].ArtistId, initialSimilarity(similarArtists[prevArtists[i].ArtistName])); } foreach (KeyValuePair<string, int> entry in similarArtists) { double weight = initialSimilarity(entry.Value); weight *= weight; mydb.newQuery("SELECT s.artist2_id, s.similarity FROM Artist a, ArtistSimilarity s" + " WHERE s.artist1_id = a.artist_id AND a.artist_name=@artist_name AND s.artist2_id!=@artist_id"); mydb.setParameter("artist_name", entry.Key); mydb.setParameter("artist_id", Artist.ArtistId); SqlDataReader reader = mydb.ExecuteReader(); while (reader.Read()) { int artistId = Int32.Parse(reader["artist2_id"].ToString()); double sim = Double.Parse(reader["similarity"].ToString()); similarityNumerator[artistId] += sim * weight; similarityDenominator[artistId] += weight; } } for (int i = 0; i < prevArtists.Count; ++i) { int id = prevArtists[i].ArtistId; similarity[id] += transitiveSimilarity(similarity[id], similarityNumerator[id], similarityDenominator[id]); } for (int i = 0; i < prevArtists.Count; ++i) { StoreSimilarity(mydb, Artist.ArtistId, prevArtists[i].ArtistId, similarity[prevArtists[i].ArtistId]); StoreSimilarity(mydb, prevArtists[i].ArtistId, Artist.ArtistId, similarity[prevArtists[i].ArtistId]); } StoreSimilarity(mydb, Artist.ArtistId, Artist.ArtistId, 1); return similarity; }
public static void StoreSimilarity(MyDatabase mydb, int artist1_id, int artist2_id, double similarity) { mydb.newQuery("INSERT INTO ArtistSimilarity VALUES(@artist1_id, @artist2_id, @similarity)"); mydb.setParameter("artist1_id", artist1_id); mydb.setParameter("artist2_id", artist2_id); mydb.setParameter("similarity", similarity); mydb.ExecuteNonQuery(); }
public static List<ArtistInfo> GetArtists(MyDatabase mydb, int ArtistId) { List<ArtistInfo> artists = new List<ArtistInfo>(); mydb.newQuery("SELECT artist_id, artist_name from Artist WHERE artist_id != @artist_id"); mydb.setParameter("artist_id", ArtistId); SqlDataReader reader = mydb.ExecuteReader(); while (reader.Read()) { int artistId = Int32.Parse(reader["artist_id"].ToString()); string artistName = reader["artist_name"].ToString(); artists.Add(new ArtistInfo(artistId, artistName)); } return artists; }