From 920858a46a5809b6a5076b0a3055770b2ed5f9ce Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 25 Jan 2016 07:55:46 +0100 Subject: [PATCH] speed typing done, levenshtein moved to extensions Also fixed scirpt --- NadekoBot/Classes/Extensions.cs | 39 +++++++++++++++++++++++ NadekoBot/Classes/Trivia.cs | 48 +---------------------------- NadekoBot/Commands/SpeedTyping.cs | 9 ++++-- NadekoBot/Modules/Administration.cs | 36 ++++++++++++++++++++-- scripts/scrape-google-books.pl | 2 +- 5 files changed, 81 insertions(+), 53 deletions(-) diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index a906d172..b0759500 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -135,5 +135,44 @@ namespace NadekoBot.Extensions action(element); } } + + //http://www.dotnetperls.com/levenshtein + public static int LevenshteinDistance(this string s, string t) { + int n = s.Length; + int m = t.Length; + int[,] d = new int[n + 1, m + 1]; + + // Step 1 + if (n == 0) { + return m; + } + + if (m == 0) { + return n; + } + + // Step 2 + for (int i = 0; i <= n; d[i, 0] = i++) { + } + + for (int j = 0; j <= m; d[0, j] = j++) { + } + + // Step 3 + for (int i = 1; i <= n; i++) { + //Step 4 + for (int j = 1; j <= m; j++) { + // Step 5 + int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; + + // Step 6 + d[i, j] = Math.Min( + Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), + d[i - 1, j - 1] + cost); + } + } + // Step 7 + return d[n, m]; + } } } diff --git a/NadekoBot/Classes/Trivia.cs b/NadekoBot/Classes/Trivia.cs index b6cdae69..31837619 100644 --- a/NadekoBot/Classes/Trivia.cs +++ b/NadekoBot/Classes/Trivia.cs @@ -204,7 +204,7 @@ namespace NadekoBot return true; } - int levDistance = ComputeLevenshteinDistance(guess, answer); + int levDistance = guess.LevenshteinDistance(answer); return Judge(guess.Length, answer.Length, levDistance); } @@ -238,52 +238,6 @@ namespace NadekoBot return str; } - //http://www.dotnetperls.com/levenshtein - private int ComputeLevenshteinDistance(string s, string t) - { - int n = s.Length; - int m = t.Length; - int[,] d = new int[n + 1, m + 1]; - - // Step 1 - if (n == 0) - { - return m; - } - - if (m == 0) - { - return n; - } - - // Step 2 - for (int i = 0; i <= n; d[i, 0] = i++) - { - } - - for (int j = 0; j <= m; d[0, j] = j++) - { - } - - // Step 3 - for (int i = 1; i <= n; i++) - { - //Step 4 - for (int j = 1; j <= m; j++) - { - // Step 5 - int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; - - // Step 6 - d[i, j] = Math.Min( - Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), - d[i - 1, j - 1] + cost); - } - } - // Step 7 - return d[n, m]; - } - public async void GetHint(MessageEventArgs e) { if (timeout != null && !isQuit && stopwatch.ElapsedMilliseconds > 10000) await e.Send( currentQuestion.Answer.Scramble()); diff --git a/NadekoBot/Commands/SpeedTyping.cs b/NadekoBot/Commands/SpeedTyping.cs index a9f527b6..a801da9c 100644 --- a/NadekoBot/Commands/SpeedTyping.cs +++ b/NadekoBot/Commands/SpeedTyping.cs @@ -86,15 +86,20 @@ namespace NadekoBot { var guess = e.Message.RawText; - if (currentSentence == guess && !finishedUserIds.Contains(e.User.Id)) { + var distance = currentSentence.LevenshteinDistance(guess); + var decision = Judge(distance, guess.Length); + if (decision && !finishedUserIds.Contains(e.User.Id)) { finishedUserIds.Add(e.User.Id); - await channel.Send($"{e.User.Mention} finished in **{sw.Elapsed.Seconds}** seconds, **{ currentSentence.Length / TypingGame.WORD_VALUE / sw.Elapsed.Seconds * 60 }** WPM!"); + await channel.Send($"{e.User.Mention} finished in **{sw.Elapsed.Seconds}** seconds with { distance } errors, **{ currentSentence.Length / TypingGame.WORD_VALUE / sw.Elapsed.Seconds * 60 }** WPM!"); if (finishedUserIds.Count % 2 == 0) { await e.Send($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{currentSentence}**:book:"); } } } + + private bool Judge(int errors, int textLength) => errors <= textLength / 25; + } class SpeedTyping : DiscordCommand { diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 12ea9f9d..10097520 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -8,6 +8,7 @@ using System.Timers; using NadekoBot.Extensions; using System.Threading.Tasks; using NadekoBot.Commands; +using System.IO; namespace NadekoBot.Modules { @@ -334,8 +335,7 @@ namespace NadekoBot.Modules //await e.Send("```\n" + e.User.ServerPermissions.+"\n```"); }); - - //todo maybe add .opencomms and then make send always send to that user? + Server commsServer = null; User commsUser = null; @@ -372,8 +372,38 @@ namespace NadekoBot.Modules await e.Send("Sending failed."); } }); - }); + /* + cgb.CreateCommand(".jsontype") + .Do(async e => { + Newtonsoft.Json.Linq.JArray data = Newtonsoft.Json.Linq.JArray.Parse(File.ReadAllText("data.json")); + if (data == null || data.Count == 0) return; + + var wer = data.Where(jt => jt["Description"].ToString().Length > 120); + var list = wer.Select(jt => { + var obj = new Parse.ParseObject("TypingArticles"); + obj["text"] = jt["Description"].ToString(); + return obj; + }); + await Parse.ParseObject.SaveAllAsync(list); + await e.Send("saved to parse"); + + }); + + cgb.CreateCommand(".repeat") + .Do(async e => { + if (e.User.Id != NadekoBot.OwnerID) return; + + string[] notifs = { "Admin use .bye .greet", "Unstable - fixing", "fixing ~ani, ~mang", "join NadekoLog server", "-h is help, .stats",}; + int i = notifs.Length; + while (true) { + await e.Channel.SendMessage($".setgame {notifs[--i]}"); + await Task.Delay(20000); + if (i == 0) i = notifs.Length; + } + }); + */ + }); } bool announcingGreet = false; diff --git a/scripts/scrape-google-books.pl b/scripts/scrape-google-books.pl index af36cd31..d7a96b76 100644 --- a/scripts/scrape-google-books.pl +++ b/scripts/scrape-google-books.pl @@ -29,7 +29,7 @@ for(my $i = 0; $i < $maxPages; $i++) $fullUrl .= "&start=$i" .0; } print "Getting this: $fullUrl\n"; - my $html = `curl -A "$userAgent" -k "$fullUrl"`; + my $html = `curl -A "$userAgent" -k -L "$fullUrl"`; #print "HTML: $html"; #;