// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { Artist.ArtistID = id; if (!ModelState.IsValid) { error = string.Join(" | ", ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage)); error += id.ToString(); return(Page()); } Artist.Listens = 0; Artist.Rating = 1; Artist.WordVec = Vspace.ConvertToString(Vspace.Add(_textprocessor.PredictText(Artist.Name), _textprocessor.PredictText(Artist.Description))); await _access.Create(id, Artist); return(RedirectToPage("./Index")); }
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"))); }
public JsonResult OnPostLike(string song_id, string val) { if (HttpContext.User.Identity.IsAuthenticated) { int _id = Int32.Parse(HttpContext.User.Claims.Where(c => c.Type == "UserID").Select(c => c.Value).SingleOrDefault()); _User = _context.User.Find(_id); preference_vector = Vspace.ToArray(_User.PreferenceVector); } int like = Int32.Parse(val); if (_User != null) { History history = _context.History.Where(x => x.User.UserID == _User.UserID && x.Song.SongID == Int32.Parse(song_id)).FirstOrDefault(); Song song = _context.Song.Where(s => s.SongID == Int32.Parse(song_id)).Include(x => x.Album).ThenInclude(x => x.Artist).FirstOrDefault(); if (history == null) { history = new History() { User = _User, Song = song, Progress = 0, Liked = like }; _context.History.Add(history); _context.SaveChanges(); return(new JsonResult("updated 1")); } if (history.Liked != like) { song.Rating += like; Artist artist = _context.Artist.Where(a => a.ArtistID == song.Album.Artist.ArtistID).FirstOrDefault(); artist.Rating += like; history.Liked = like; _User.PreferenceVector = Vspace.ConvertToString( Vspace.Normalize(Vspace.Add(Vspace.Scale(like, Vspace.ToArray(song.WordVec)), Vspace.ToArray(_User.PreferenceVector)))); _context.Song.Update(song); _context.Artist.Update(artist); _context.History.Update(history); _context.User.Update(_User); _context.SaveChanges(); } return(new JsonResult("Like1")); } return(new JsonResult("updated")); }
public JsonResult OnPostIncreaseListen(string song_id) { if (HttpContext.User.Identity.IsAuthenticated) { int _id = Int32.Parse(HttpContext.User.Claims.Where(c => c.Type == "UserID").Select(c => c.Value).SingleOrDefault()); _User = _context.User.Find(_id); } if (_User != null) { History history = _context.History.Where(x => x.User.UserID == _User.UserID && x.Song.SongID == Int32.Parse(song_id)).FirstOrDefault(); Song song = _context.Song.Where(s => s.SongID == Int32.Parse(song_id)).Include(x => x.Album).ThenInclude(x => x.Artist).FirstOrDefault(); if (history == null) { _User.PreferenceVector = Vspace.ConvertToString( Vspace.Normalize(Vspace.Add(Vspace.Scale(0.05f, Vspace.ToArray(song.WordVec)), Vspace.ToArray(_User.PreferenceVector)))); song.Listens += 1; Artist artist = _context.Artist.Where(a => a.ArtistID == song.Album.Artist.ArtistID).FirstOrDefault(); artist.Listens += 1; History new_history = new History() { User = _User, Song = song, Progress = 0, Liked = 0 }; _context.User.Update(_User); _context.Song.Update(song); _context.Artist.Update(artist); _context.History.Add(new_history); _context.SaveChanges(); return(new JsonResult("Listen1")); } return(new JsonResult("Listen2")); } return(new JsonResult("Clicked play " + song_id)); }
public async Task <IActionResult> OnPostAsync(int?id) { file = Path.Combine(_environment.ContentRootPath, "wwwroot", "music", UploadedFile.FileName); using (var fileStream = new FileStream(file, FileMode.Create)) { await UploadedFile.CopyToAsync(fileStream); } string filename = UploadedFile.FileName; filename.Replace(" ", string.Empty); Song.Filepath = "/music/" + filename; User user = _context.User.FirstOrDefault(u => u.Username == User.Identity.Name); Artist artist = _context.Artist.FirstOrDefault(a => a.ArtistID == id); Genre genre = _context.Genre.FirstOrDefault(g => g.Name == Song.Genre.Name); if (genre == null) { genre = new Genre { Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Song.Genre.Name.ToLower()), WordVec = VSpace.ConvertToString(_textprocessor.PredictText(Song.Genre.Name)) }; _context.Genre.Add(genre); } Song.Genre = genre; var album = _context.Album.FirstOrDefault(a => a.Name == Song.Album.Name && a.Artist.ArtistID == id); if (album == null) { Album Album = new Album { Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Song.Album.Name), Artist = artist }; Song.Album = Album; _context.Album.Add(Album); } else { Song.Album = album; } _textprocessor.PredictText(Song.Lyrics); Song.Listens = 1; NaturalLanguage.vector.VectorSpace.ToArray(artist.WordVec); NaturalLanguage.vector.VectorSpace.ToArray(genre.WordVec); NaturalLanguage.vector.VectorSpace.Scale(genre_contrib, NaturalLanguage.vector.VectorSpace.ToArray(genre.WordVec)); Song.WordVec = VSpace.ConvertToString( VSpace.Normalize(VSpace.Add(_textprocessor.PredictText(Song.Lyrics), _textprocessor.PredictText(Song.Name)))); _reader.Read(Path.Combine(Environment.CurrentDirectory, "wwwroot/music", filename)); double[][] res = AudioAnalysis.Fourier.FFT(_reader.GetStream(), 200, _reader.GetSampleRate()); int[] f = AudioAnalysis.Compare.ToIntArray(res[1]); Song.FreqVec = AudioAnalysis.Compare.ToString(f); _context.Song.Add(Song); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { Song s = _context.Song.Where(x => x.SongID == Song.SongID).FirstOrDefault(); User user = _context.User.FirstOrDefault(u => u.Username == User.Identity.Name); Artist artist = _context.Artist.FirstOrDefault(a => a.ArtistID == user.UserID); Album a = _context.Album.Where(x => x.Name == Song.Album.Name && x.Artist.ArtistID == user.UserID).FirstOrDefault(); Genre g = _context.Genre.Where(x => x.Name == Song.Genre.Name).FirstOrDefault(); //if (a == null) //{ // Album Album = new Album // { // Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Song.Album.Name), // Artist = artist // }; // Song.Album = Album; // _context.Album.Add(Album); //} //else //{ // Song.Album = a; //} //if (g == null) //{ // g = new Genre // { // Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Song.Genre.Name.ToLower()), // WordVec = Vspace.ConvertToString(_textprocessor.PredictText(Song.Genre.Name)) // }; // _context.Genre.Add(g); //} //Song.Genre = g; //Song.Filepath = ; Song.WordVec = VSpace.ConvertToString( VSpace.Normalize(VSpace.Add(_textprocessor.PredictText(Song.Lyrics), _textprocessor.PredictText(Song.Name)))); _context.Song.Update(s); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SongExists(Song.SongID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }