示例#1
0
        /// <summary>
        /// Classify the given songs and output a JSON string with the results.
        /// </summary>
        /// <remarks>
        /// Results will be in the following format:
        ///
        ///     { "ClassifierResults":
        ///         [ { "song":
        ///             {
        ///                 "title": "path\to\file\1.mp3",
        ///                 "energy": 0.40120655758066681,
        ///                 "positivity": 0.47041366490774172
        ///             }
        ///           },
        ///           { "song":
        ///             {
        ///                 "title": "path\to\file\2.mp3",
        ///                 "energy": 0.34171391252526506,
        ///                 "positivity": 0.28074189017885803
        ///             }
        ///           },
        ///           ...
        ///         ]
        ///     }
        /// </remarks>
        ///
        ///
        /// <param name="songPaths">Paths to the songs to classify.</param>
        public string Classify(string[] songPaths)
        {
            if (posSvm == null || energySvm == null)
            {
                throw new Exception("SVMs have not yet been trained!");
            }

            //Get features for all the songs
            List <SongDataDTO> songsWithFeatures = getFeatures(songPaths);

            //Classify each song
            List <ClassifierResult> classifierResults = new List <ClassifierResult>();

            ConsoleManager.Show();
            for (int i = 0; i < songsWithFeatures.Count; i++)
            {
                SongDataDTO songData = songsWithFeatures[i];

                ClassifierResult result = new ClassifierResult();
                Song             song   = new Song();
                //song.title = songData.getFilename(); Turns the database into .wavs
                song.title = songPaths[i];

                //Convert dto to double arrays (so svm can use them)
                double[] posFeatures    = new double[bextractPosCols.Count()];
                double[] energyFeatures = new double[bextractEnergyCols.Count()];
                ConvertSongDataDtoToDoubleArrays(songData, ref posFeatures, ref energyFeatures);

                //Run classification
                song.positivity = posSvm.Score(posFeatures);
                song.energy     = energySvm.Score(energyFeatures);

                result.song = song;
                //Fun Debugging info

                Console.WriteLine("Song: {0}\n Positivity: {1}\n Energy: {2} \n\n", song.title, song.positivity, song.energy);
                classifierResults.Add(result);
            }
            Console.WriteLine("Press Enter to continue");
            Console.ReadLine();
            ConsoleManager.Hide();

            EmotionSpaceDTOList emotionSpaceDtoList = new EmotionSpaceDTOList();

            emotionSpaceDtoList.ClassifierResults = classifierResults;
            return(JsonSerializer.serializeToJson(emotionSpaceDtoList));
        }
 public static string serializeToJson(EmotionSpaceDTOList dto)
 {
     return(new JavaScriptSerializer().Serialize(dto));
 }