def clusterKmeans(self, force): print 'Clustering' keys = self.data.columns[0:len(self.data.columns)-1] self.kmeans.fit(self.data[keys]) labels = self.kmeans.labels_ self.data['cluster'] = labels if(os.path.isfile('/Users/igor/songAtributesClustered.csv')): if(force): os.remove('/Users/igor/songAtributesClustered.csv') self.data.to_csv('/Users/igor/songAtributesClustered.csv', sep=',', encoding='utf-8') else: self.data.to_csv('/Users/igor/songAtributesClustered.csv', sep=',', encoding='utf-8') db = Database() for index, row in self.data.iterrows(): db.saveCluster(remoteId=row['remote_id'], cluster=labels[index]) print 'Finished clustering' return (True, 'KMeans')
def suggest_similar(): print 'Suggest similar' if request.json: #get song name data = request.json # will be songName = data.get("songName") global ml response = {} response['success'] = False response['message'] = 'Oops, something went wrong. We couldn\'t find song: ' + songName #get song services = Services() s = services.getSongBySongName(songName) #print 'trazena pesam: \n' + s.toString() if(s != None): #classify and save cluster db = Database() persistedSong = db.getSongByRemoteId(s.remoteId) if(persistedSong == None): print 'Pesma nije bila u bazi' s.id = db.insertSongs([s])[0] s = ml.classifyDecisionTreeClassifier(s) print 'Dodeljen klaster: ' + str(s.cluster) db.saveCluster(s.remoteId, s.cluster) else: print 'Pesma je bila u bazi' s.cluster = persistedSong.cluster s.id = persistedSong.id #get songs from same cluster songsFromCluster = db.getSongsFromSameCluster(s) criteriaKey = data.get("criteriaKey") criteriaValue = data.get("criteriaValue") featureWeights = data.get("featureWeights") print "feature weights" print featureWeights if criteriaKey=="year" and criteriaValue!=None: years = criteriaValue.split("-") suggestedSong = ml.getSimilarSongFromYearRange(songsFromCluster, s, int(years[0]), int(years[1])) #return "not supported yet" elif(criteriaKey =="genre" and criteriaValue!=None): suggestedSong = ml.getSimilarSongFromGenre(songsFromCluster, s, criteriaValue) else: suggestedSong = ml.getSimilarSong(songsFromCluster, s, featureWeights) if suggestedSong == None: response['message'] = "Oops, something went wrong. We couldn't find a recommendation. " return jsonify(response) response['success'] = True response['request_song'] = s.toDictionary() response['suggested_song'] = suggestedSong.toDictionary() response['message'] = 'Suggestion successful' print response return jsonify(response) response = {} response['success'] = False response['message'] = 'Bad request.' return jsonify(response)