speed typing done, levenshtein moved to extensions

Also fixed scirpt
This commit is contained in:
Master Kwoth 2016-01-25 07:55:46 +01:00
parent e614e2036c
commit 920858a46a
5 changed files with 81 additions and 53 deletions

View File

@ -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];
}
}
}

View File

@ -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());

View File

@ -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 {

View File

@ -8,6 +8,7 @@ using System.Timers;
using NadekoBot.Extensions;
using System.Threading.Tasks;
using NadekoBot.Commands;
using System.IO;
namespace NadekoBot.Modules
{
@ -335,7 +336,6 @@ 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;

View File

@ -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";
#<STDIN>;