protected IDataReader ExecutarReader(string command, CommandType commandType, int timeout = 30) { try { Conexao = MyDatabase.CreateConnection(); Conexao.Open(); MyCommand = (commandType == CommandType.StoredProcedure) ? MyDatabase.GetStoredProcCommand(command) : MyDatabase.GetSqlStringCommand(command); MyCommand.CommandTimeout = timeout; VincularParametros(); return(MyDatabase.ExecuteReader(MyCommand)); } catch (Exception e) { throw new Exception(e.Message); } finally { if (MyCommand != null) { MyCommand.Dispose(); } if (Conexao != null) { Conexao.Dispose(); Conexao.Close(); } } }
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 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; }