public void ClassifySongsTogether() { string positivitySvmPath = System.IO.Path.Combine(Classifier.ExecutableInformation.getModelsDir(), positiveClassificationMethod); string energySvmPath = System.IO.Path.Combine(Classifier.ExecutableInformation.getModelsDir(), energyClassificationMethod); Classifier.SupportVectorMachine svm = new Classifier.SupportVectorMachine(positivitySvmPath, energySvmPath); List <string> unclassifiedSongs = new List <string>(); foreach (string song in songs) { if (!ServerDatabase.Instance.DoesSongExist(song)) { unclassifiedSongs.Add(song); } } //Don't try to reclassify the same song. Waste of time. string output = svm.Classify(unclassifiedSongs); foreach (ClassifierResult result in JsonDTOMapper.getJsonDTO(output).ClassifierResults) { Song classifiedSong = result.song; EmotionSpaceDTO point = new EmotionSpaceDTO(classifiedSong.energy, classifiedSong.positivity); ServerDatabase.Instance.addSongToDatabase(classifiedSong.title, point); } }
public List <string> getSongs(int numberOfSongs, EmotionSpaceDTO emotionSpaceDTO) { Dictionary <string, EmotionSpaceDTO> songs = new Dictionary <string, EmotionSpaceDTO>(); Action <SQLiteDataReader> addSongsToDict = (rdr) => { while (rdr.Read()) { EmotionSpaceDTO point = new EmotionSpaceDTO(); point.Energy = rdr.GetDouble(1); point.Positivity = rdr.GetDouble(2); songs[rdr.GetString(0)] = point; } }; getAllSongs(addSongsToDict); if (songs.Count < numberOfSongs) { numberOfSongs = songs.Count; } List <SongDistance> closeSongs = new List <SongDistance>(numberOfSongs); double farPoint = double.MinValue; foreach (KeyValuePair <string, EmotionSpaceDTO> kvp in songs) { SongDistance val = new SongDistance(); val.distance = getDistance(emotionSpaceDTO, kvp.Value); val.song = kvp.Key; if (closeSongs.Count < numberOfSongs) { closeSongs.Add(val); if (farPoint < val.distance) { farPoint = val.distance; } } else { if (val.distance < farPoint) { closeSongs.Add(val); closeSongs.Sort(compareDistance); closeSongs.RemoveAt(closeSongs.Count - 1); } } } List <string> ret = new List <string>(); foreach (SongDistance finalVal in closeSongs) { ret.Add(finalVal.song); } return(ret); }
public void Classify() { Classifier clas = new Classifier(); foreach (string song in songs) { if (!ServerDatabase.Instance.DoesSongExist(song)) { //Don't try to reclassify the same song. Waste of time. string output = clas.classify(song); Song classifiedSong = JsonDTOMapper.getJsonDTO(output).ClassifierResults[0].song; EmotionSpaceDTO point = new EmotionSpaceDTO(classifiedSong.energy, classifiedSong.positivity); ServerDatabase.Instance.addSongToDatabase(song, point); } } }
/// <summary> /// Adds a song to the database /// </summary> /// <param name="songpath">The location of the song</param> /// <param name="emotionSpaceDTO">The coordinates in the emotionspace</param> public void addSongToDatabase(string songpath, EmotionSpaceDTO emotionSpaceDTO) { try { using (SQLiteConnection connection = Connect()) { using (SQLiteTransaction tranaction = connection.BeginTransaction()) { using (SQLiteCommand command = connection.CreateCommand()) { command.Transaction = tranaction; command.CommandText = string.Format("INSERT INTO SONGS (Path, Energy, Postitvity) VALUES (\"{0}\",{1},{2})", songpath, emotionSpaceDTO.Energy, emotionSpaceDTO.Positivity); command.ExecuteNonQuery(); } tranaction.Commit(); } } } catch { //This is likely trying to add a song twice. Just silently fail } }
private double getDistance(EmotionSpaceDTO pointA, EmotionSpaceDTO pointB) { return(Math.Sqrt(Math.Pow(pointA.Energy - pointB.Energy, 2) + Math.Pow(pointA.Positivity - pointB.Positivity, 2))); }