Ejemplo n.º 1
0
        public void TestMethod1()
        {
            float[] v1 = { 1.0f, 2.0f, 3.0f };
            float[] v2 = { 1.0f, 2.0f, 3.0f };

            float[] v3 = NaturalLanguage.vector.VectorSpace.Add(v1, v2);

            CollectionAssert.AreEqual(v3, Vspace.Scale(2, v1));
            CollectionAssert.AreEqual(Vspace.Negate(v1), Vspace.Scale(-1, v1));
            Assert.AreEqual(Vspace.DotProduct(v2, v2), 14);
            Assert.AreEqual(Vspace.Magnitude(v2), Math.Sqrt(14), 0.01);
            CollectionAssert.AreEqual(Vspace.Normalize(v1), new float[] { 1 / (float)Math.Sqrt(14), 2 / (float)Math.Sqrt(14), 3 / (float)Math.Sqrt(14) });

            float[] italian_vec = { -3.1635818f, -0.20904206f, 0.29893237f, -0.00035927136f, -0.058570296f, -0.00846739f, 0.3034302f, -0.016199231f, -0.27490035f, -0.1588173f };
            float[] anthem_vec  = { -2.863994f, 0.09603188f, 0.42992353f, 0.0986715f, -0.15630788f, -0.06696623f, 0.43981647f, 0.18234879f, -0.480734f, -0.04069173f };

            float[] niv = Vspace.Normalize(italian_vec);

            W2V w = new W2V();

            float[] predicted = w.PredictText("italian");

            //CollectionAssert.AreEqual(predicted, niv);

            float[] niv2 = Vspace.Normalize(Vspace.Add(italian_vec, anthem_vec));

            string[] ws = NaturalLanguage.Text.RemoveStopWords.RemoveWords("anthem".Trim().ToLower());
            Assert.AreEqual("anthem", ws[0]);

            predicted = w.PredictText("anthem");
            CollectionAssert.AreEqual(predicted, Vspace.Normalize(anthem_vec));

            predicted = w.PredictText("italian anthem");
            CollectionAssert.AreEqual(predicted, niv2);

            Assert.IsFalse(predicted[0] == 0);
            Assert.IsFalse(niv[0] == 0);

            float[] german_vec = { -1.1745248f, -0.084007405f, 0.034549948f, -0.16554825f, -0.14128838f, 0.020536855f, -0.08310235f, 0.13850354f, -0.09974682f, 0.05751427f };

            anthem_vec = new float[] { -2.863994f, 0.09603188f, 0.42992353f, 0.0986715f, -0.15630788f, -0.06696623f, 0.43981647f, 0.18234879f, -0.480734f, -0.04069173f };

            float[] niv3 = Vspace.Normalize(Vspace.Add(german_vec, anthem_vec));

            float[] predicted2 = w.PredictText("german anthem");
            CollectionAssert.AreEqual(predicted2, niv3);

            Assert.IsFalse(predicted2[0] == 0);
            Assert.IsFalse(niv3[0] == 0);

            Assert.IsTrue(Vspace.Loss(w.PredictText("german anthem"), w.PredictText("italian anthem")) < Vspace.Loss(w.PredictText("italian anthem"), w.PredictText("fluffy pancakes")));
        }
Ejemplo n.º 2
0
        public async Task OnGetAsync(int?id)
        {
            preference_vector = Vspace.Ones(10);

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                int _id = Int32.Parse(HttpContext.User.Claims.Where(c => c.Type == "UserID").Select(c => c.Value).SingleOrDefault());
                Playlists         = _context.Playlist.Where(p => p.User.UserID == _id).ToList();
                _User             = _context.User.Find(_id);
                preference_vector = Vspace.ToArray(_User.PreferenceVector);
            }

            if (id != null)
            {
                Song = await _context.Song.Where(s => s.Album.Artist.ArtistID == id).Include(x => x.Genre).Include(x => x.Album).ThenInclude(x => x.Artist).ToListAsync();
            }
            else
            {
                List <Song> songs = _context.Song.Include(x => x.Genre).Include(x => x.Album).ThenInclude(x => x.Artist).ToList();

                if (SearchString.Length > 0)
                {
                    SearchString = SearchString.ToLower();

                    if (!string.IsNullOrEmpty(SearchString))
                    {
                        Song s = _context.Song.Where(x => x.Name.ToLower() == SearchString).FirstOrDefault();

                        if (s != null)
                        {
                            int[] f = AudioAnalysis.Compare.ToArray(s.FreqVec);
                            songs = songs.OrderByDescending(x => AudioAnalysis.Compare.Similarity(f, AudioAnalysis.Compare.ToArray(x.FreqVec)) + .01 * x.Rating / (double)x.Listens).ToList();
                            foreach (var song in songs)
                            {
                                msg += song.Name + ":" + AudioAnalysis.Compare.Similarity(f, AudioAnalysis.Compare.ToArray(song.FreqVec)) + "; ";
                            }
                        }
                        else
                        {
                            Artist a = _context.Artist.Where(x => x.Name.ToLower() == SearchString).FirstOrDefault();

                            if (a != null)
                            {
                                songs = _context.Song.Where(s => s.Album.Artist.ArtistID == a.ArtistID).ToList();
                                songs = songs.OrderByDescending(x => .01 * x.Rating / (double)x.Listens).ToList();
                            }
                            else
                            {
                                Genre g = _context.Genre.Where(x => x.Name.ToLower() == SearchString).FirstOrDefault();

                                if (g != null)
                                {
                                    songs = _context.Song.Where(s => s.Genre.GenreID == g.GenreID).ToList();
                                    songs = songs.OrderByDescending(x => .01 * x.Rating / (double)x.Listens).ToList();
                                }
                                else
                                {
                                    var word_vector = _textprocessor.PredictText(SearchString);


                                    songs.Sort((a, b) =>
                                               (Vspace.Loss(word_vector, Vspace.ToArray(a.WordVec)).CompareTo(Vspace.Loss(word_vector, Vspace.ToArray(b.WordVec)))));
                                }
                            }
                        }
                    }
                }
                Song = songs;
            }
        }