public void Given_never_before_seen_text() { var detector = new SpamDetector(); var corpus = @"欧拉"; Assert.AreEqual(detector.IsSpam(corpus), CategorizationResult.Undetermined); }
public PartialViewResult AddChildCommentEvent(int idOfParentId, string content) { if (SpamDetector.IsContentSpam(content)) { return(null); } var parentComment = context.CommentEvents.Find(idOfParentId); GamingEvent gameEvent = context.GamingEvents.Where(a => a.GamingEventId == parentComment.GamingEventId).First(); ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } CommentEvent commentEvent = new CommentEvent() { Body = content, DateOfInsert = DateTime.Now, GamingEventId = gameEvent.GamingEventId, UserId = User.Identity.GetUserId(), User = user, ParentCommentEventId = parentComment.CommentEventId, Parent = parentComment }; var sentimentalInt = new SentimentalInterpreter(); var isContentHappy = sentimentalInt.IsHappy(commentEvent.Body); if (isContentHappy) { commentEvent.IsHappy = true; } parentComment.Children.Add(commentEvent); context.CommentEvents.Add(commentEvent); context.SaveChanges(); CurrentCommentGameEventViewModel vm = new CurrentCommentGameEventViewModel { LoggedUser = user, Com = parentComment }; if (vm.Com.Children != null && vm.Com.Children.Count > 1) { vm.Com.Children = vm.Com.Children.OrderBy(a => a.DateOfInsert).ToList(); } return(PartialView("~/Views/GamingEvent/_ChildrenComments.cshtml", vm)); }
public ActionResult SaveEvent(EventCategoryViewModel vm, HttpPostedFileBase file, string newItem = null) { if (SpamDetector.IsContentSpam(vm.GamingEvent.Title)) { return(RedirectToAction("Index", "Post")); } //Verify whether user has neccessary data ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } int idd = vm.GamingEvent.GamingEventId; var gameEvent = vm.GamingEvent; if (file != null && file.ContentLength > 0) { if (Path.GetExtension(file.FileName).ToLower() == ".jpg" || Path.GetExtension(file.FileName).ToLower() == ".png") { var sourceImage = Image.FromStream(file.InputStream); sourceImage = ResizeImage(sourceImage, 500, 500); //Generowanie plik var fileExt = Path.GetExtension(file.FileName); var filename = Guid.NewGuid() + fileExt; // Unikalny identyfikator + rozszerzenie //W jakim folderze ma byc umiesczony dany plik oraz jego nazwa! Oraz zapis var path = Path.Combine(Server.MapPath(AppConfig.ImagesGamingEventFolder), filename); //file.SaveAs(path); sourceImage.Save(path); gameEvent.MainPicture = filename; } } else if (file == null && newItem != null && newItem != String.Empty) { var fractImg = FractalGenerator.GenereateFractal1(user.Email); var filename = Guid.NewGuid() + ".jpg"; var path = Path.Combine(Server.MapPath(AppConfig.ImagesGamingEventFolder), filename); fractImg.Save(path); gameEvent.MainPicture = filename; } gameEvent.UserId = user.Id; gameEvent.Publisher = user; context.GamingEvents.AddOrUpdate(gameEvent); context.SaveChanges(); return(RedirectToAction("Events", "Manage")); }
public void Given_text_marked_as_spam_then_marked_as_ham() { var detector = new SpamDetector(); var spamCorpus = @"你要说的下一句话是……"; detector.SpamFound(spamCorpus); detector.HamFound(spamCorpus); Assert.AreEqual(detector.IsSpam(spamCorpus), CategorizationResult.Undetermined); }
public void Given_a_corpus_that_is_half_spam_and_half_unknown() { var detector = new SpamDetector(); detector.SpamFound(@"做人了"); var result = detector.IsSpam(@"我不做人了!"); Assert.AreEqual(result, CategorizationResult.Undetermined); }
public void Given_text_that_has_known_ham_and_unknown_words() { var detector = new SpamDetector(); var ham = @"我在短暂的人生里发现...一个人越是玩弄阴谋,就越会感到人类的能力是有极限的...."; var mysteryMeat = @"欧拉欧拉欧拉欧拉欧拉欧拉欧拉"; detector.HamFound(ham); var result = detector.IsSpam(mysteryMeat); Assert.AreEqual(result, CategorizationResult.Undetermined); }
public void Given_text_that_has_an_equivalent_number_of_ham_and_spam() { var detector = new SpamDetector(); var ham = @"不愧是DIO!"; var spam = @"不过,我拒绝"; detector.HamFound(ham); detector.SpamFound(spam); var result = detector.IsSpam(@"不愧是DIO!不过,我拒绝"); Assert.AreEqual(result, CategorizationResult.Undetermined); }
public void Given_text_previously_marked_as_spam() { var detector = new SpamDetector(); var corpus = @"无駄"; var ham = @"我在短暂的人生里发现...一个人越是玩弄阴谋,就越会感到人类的能力是有极限的...."; detector.SpamFound(corpus); detector.HamFound(ham); var isSpam = detector.IsSpam(corpus); Assert.AreEqual(isSpam, CategorizationResult.First); }
public void Test() { var corpus = @"不愧是DIO!我们不敢做的事,他毫不在乎地做了!真是佩服,真是我们的偶像!"; var currentPath = @".\"; var spamPath = Path.Combine(currentPath, @"Resources", @"spam.txt"); var hamPath = Path.Combine(currentPath, @"Resources", @"ham.txt"); var spamDetector = new SpamDetector(); spamDetector.Train(spamPath, hamPath); Assert.AreEqual(spamDetector.IsSpam(corpus), CategorizationResult.Second); }
public void Given_a_corpus_that_is_heavily_weighted_towards_being_spam() { var ham = @"做人了"; var spam = @"我不做人了!"; var detector = new SpamDetector(); detector.HamFound(ham); detector.SpamFound(spam); var result = detector.IsSpam(@"我不做人了!"); Assert.AreEqual(result, CategorizationResult.First); }
public PartialViewResult AddComment(int gamingEventId, string content) { if (SpamDetector.IsContentSpam(content)) { return(null); } var gameEvent = context.GamingEvents.Where(a => a.GamingEventId == gamingEventId).First(); ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } CommentEvent commentEvent = new CommentEvent() { Body = content, DateOfInsert = DateTime.Now, GamingEventId = gameEvent.GamingEventId, UserId = User.Identity.GetUserId(), User = user }; var sentimentalInt = new SentimentalInterpreter(); var isContentHappy = sentimentalInt.IsHappy(commentEvent.Body); if (isContentHappy) { commentEvent.IsHappy = true; } context.CommentEvents.Add(commentEvent); context.SaveChanges(); gameEvent.Comments.Add(commentEvent); CurrentGameEventUserViewModel vm = new CurrentGameEventUserViewModel { LoggedUser = user, GamingEvent = gameEvent }; vm.GamingEvent.Comments = vm.GamingEvent.Comments.OrderBy(a => a.DateOfInsert).ToList(); return(PartialView("_ListOfComments", vm)); }
public void LongTest() { var corpus = @"不愧是DIO!我们不敢做的事,他毫不在乎地做了!真是佩服,真是我们的偶像!"; var currentPath = @".\"; var spamPath = Path.Combine(currentPath, @"Resources", @"spam.txt"); var hamPath = Path.Combine(currentPath, @"Resources", @"ham.txt"); var spamDetector = new SpamDetector(); spamDetector.Train(spamPath, hamPath); var utf8 = new UTF8Encoding(false); var spams = File.ReadAllLines(spamPath, utf8); var hams = File.ReadAllLines(hamPath, utf8); var r0 = 0; Parallel.For(72001, 80000, i => { var result = spamDetector.IsSpam(spams[i]); if (result == CategorizationResult.First) { Interlocked.Increment(ref r0); } }); var r = r0 / 8000d * 100; Console.WriteLine($@"{r}%"); Assert.IsTrue(r > 0.97); var r1 = 0; Parallel.For(648001, 720000, i => { var result = spamDetector.IsSpam(hams[i]); if (result != CategorizationResult.First) { Interlocked.Increment(ref r1); } }); r = r1 / 72000d * 100; Console.WriteLine($@"{r}%"); Assert.IsTrue(r > 0.97); Assert.AreEqual(spamDetector.IsSpam(corpus), CategorizationResult.Second); }
public PartialViewResult ReplyToQuestion(int questionId, string reply) { if (SpamDetector.IsContentSpam(reply)) { return(null); } Question question = context.Questions.Where(a => a.QuestionId == questionId).First(); ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } question.isReadedByReceiver = true; question.isReadedBySender = false; question.ReplyContent = reply; question.ReplyDate = DateTime.Now; context.SaveChanges(); return(PartialView("_Questions", question.Game.Questions)); }
public PartialViewResult AskQuestion(int gameId, string question) { if (SpamDetector.IsContentSpam(question)) { return(null); } Game game = context.Games.Where(a => a.GameId == gameId).First(); ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } var qs = new Question() { UserId = id, Sender = user, GameId = game.GameId, Game = game, QuestionContent = question, ReplyContent = "", isReadedBySender = true, isReadedByReceiver = false, QuestionDate = DateTime.Now, ReplyDate = DateTime.Now }; context.Questions.Add(qs); game.Questions.Add(qs); user.Questions.Add(qs); context.SaveChanges(); return(PartialView("_Questions", game.Questions)); }
public JsonResult IsSpam(string text) { bool isSpam = SpamDetector.IsContentSpam(text); return(Json(isSpam, JsonRequestBehavior.AllowGet)); }
// GET: Message public string SendMessage(string content, string userName) { if (SpamDetector.IsContentSpam(content)) { return(null); } ApplicationUser user = null; string id = GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } ApplicationUser Receiveruser = context.Users.Where(a => a.UserName == userName).First(); var conversation = context.Conversations.Where(a => (a.SenderId == user.Id && a.Receiver.UserName == userName) || (a.ReceiverId == user.Id && a.Sender.UserName == userName)).FirstOrDefault(); if (conversation == null) { //We know that this is their first message so add new Conversation conversation = new Conversation() { ReceiverId = Receiveruser.Id, Receiver = Receiveruser, SenderId = user.Id, Sender = user, SenderReceived = true, ReceiverReceived = false, }; context.Conversations.Add(conversation); } else { //Check who is sender of this message and set flag for notification if (conversation.ReceiverId == user.Id) { conversation.ReceiverReceived = true; conversation.SenderReceived = false; } else { conversation.SenderReceived = true; conversation.ReceiverReceived = false; } } Content con = new Content() { ConversationId = conversation.ConversationId, Conversation = conversation, MessageContent = content, SendDate = DateTime.Now, UserId = user.Id, UserSender = user }; var sentimentalInt = new SentimentalInterpreter(); var isContentHappy = sentimentalInt.IsHappy(con.MessageContent); if (isContentHappy) { con.IsHappy = true; } conversation.LastDateTimeSend = con.SendDate; context.Contents.Add(con); conversation.Contents.Add(con); context.SaveChanges(); return("OK"); }
private static void Main(string[] args) { var detector = new SpamDetector(); var tests = new Dictionary <string, bool> { { "Sdf sdfsdfds fsff Dds fsf", true }, { "wererwwrwerweerer rw rewrwerwererwr", true }, { "hwajt the fdu eh ui futugt tge ufgb ets hewat ti sdo it", true }, { "yrt asre the bertpera sojifsi hwrifle seen fie sue usertaje rwewererwer", true }, { "EiybvisgfdsffbiFALFIFOBIOFUIBFRBsjdkdkdsjf", true }, { "igish ssddf sdf", true }, { "ioacdu uqeidiqdudaisdu a s diudsdi dusu doidu aso diuodsi uoiduaudsiaudiudasd aud", true }, { "zvuoiyvci zoioizuoicu i udaou oiuoiuiweuiou we uoqu urqu ieuiuioqouwu we uuiowu", true }, { "wert yuos", true }, { "uadfa ffo sod fhfofdos fa jisjd efdqw wqddwdww", true }, { "qrqrqr eri erer yqe ryry dfqeroworyqrqer rrqrere wrqo", true }, { ".snoitcurtsni eht yb deificeps snoitarepo )O/I( tuptuo/tupni dna lortnoc ,lacigol ,citemhtira cisab eht gnimrofrep yb margorp retupmoc a fo snoitcurtsni eht tuo seirrac taht retupmoc a nihtiw yrtiucric cinortcele eht si )UPC( tinu gnissecorp lartnec A", true }, { "easd ojd eqorj daiweo jowfie qjo qwej jo w weji", true }, { "nzcna di sidadoitna ffeha ef eq h hqq fqh erhe rr qrhre afe oqiwhi qweihuho f", true }, { "rtesting sdf what sdf re foosdf", true }, { "This is an example", false }, { "Yes", false }, { "for loop", false }, { "Awesome", false }, { "dictionary", false }, { "apple bannana orange", false }, { "Ok thanks", false }, { "A central processing unit (CPU) is the electronic circuitry within a computer that carries out the instructions of a computer program by performing the basic arithmetic, logical, control and input/output (I/O) operations specified by the instructions.", false }, { "This library detects if a phrase is spam or a legitimate sentence", false }, { "Computer programming, often shortened to programming, sometimes called coding", false }, { "In cryptography, encryption is the process of encoding messages or information in such a way that only authorized parties can read it.", false }, { "keyboard spam detector", false }, }; foreach (var test in tests) { var result = detector.IsSpam(test.Key); Console.ForegroundColor = ConsoleColor.Green; if (result != test.Value) { Console.ForegroundColor = ConsoleColor.Red; } Console.Write(result.ToString().ToUpper()); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(" " + test.Key); } Console.WriteLine(); while (true) { var phrase = Console.ReadLine(); var result = detector.IsSpam(phrase); Console.ForegroundColor = ConsoleColor.Green; if (result) { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(result ? "Spam" : "Not Spam"); Console.ForegroundColor = ConsoleColor.Gray; } }
public ActionResult SavePost(PostCategoryViewModel vm, HttpPostedFileBase file, string newItem = null) { #region Spam Detextion if (SpamDetector.IsContentSpam(vm.Post.Title)) { return(RedirectToAction("Index", "Post")); } #endregion #region GetUserAndSaveUserImage //Verify whether user has neccessary data ApplicationUser user = null; string id = User.Identity.GetUserId(); if (id != string.Empty && id != null) { user = context.Users.Where(a => a.Id == id).First(); } int idd = vm.Post.PostId; var post = vm.Post; if (file != null && file.ContentLength > 0) { if (Path.GetExtension(file.FileName).ToLower() == ".jpg" || Path.GetExtension(file.FileName).ToLower() == ".png") { var sourceImage = Image.FromStream(file.InputStream); sourceImage = ResizeImage(sourceImage, 500, 500); //Generowanie plik var fileExt = Path.GetExtension(file.FileName); var filename = Guid.NewGuid() + fileExt; // Unikalny identyfikator + rozszerzenie //W jakim folderze ma byc umiesczony dany plik oraz jego nazwa! Oraz zapis var path = Path.Combine(Server.MapPath(AppConfig.ImagesPostFolder), filename); //file.SaveAs(path); sourceImage.Save(path); post.MainPicture = filename; } } else if (file == null && newItem != null && newItem != String.Empty) { var fractImg = FractalGenerator.GenereateFractal1(user.Email); var filename = Guid.NewGuid() + ".jpg"; var path = Path.Combine(Server.MapPath(AppConfig.ImagesPostFolder), filename); fractImg.Save(path); post.MainPicture = filename; } #endregion #region Sentimental Analisys var sentimentalInt = new SentimentalInterpreter(); var isContentHappy = sentimentalInt.IsHappy(post.Title); if (isContentHappy) { post.IsHappy = true; } #endregion post.DateOfInsert = DateTime.Now; post.UserId = user.Id; post.User = user; if (user.Posts.Where(a => a.PostId == post.PostId).Count() == 0) { user.Posts.Add(post); } context.Posts.AddOrUpdate(post); context.SaveChanges(); //InsertOrUpdate(post); return(RedirectToAction("Post", "Manage")); }