From 91008be48dc9d6071b202b863a44ea06df9e505e Mon Sep 17 00:00:00 2001 From: xsftk Date: Sat, 11 Nov 2017 14:06:48 +0700 Subject: [PATCH 1/4] .crca fix? https://github.com/Kwoth/NadekoBot/issues/1455 GetWordPosition should ignore non valid characters now most thai/chinese/japanese words arent separated by space iirc --- .../CustomReactions/Extensions/Extensions.cs | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs index 9d6f6f6c..6c8cc4cb 100644 --- a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs +++ b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs @@ -2,16 +2,16 @@ using AngleSharp.Dom.Html; using Discord; using Discord.WebSocket; +using NadekoBot.Common; +using NadekoBot.Common.Replacements; +using NadekoBot.Core.Services.Database.Models; using NadekoBot.Extensions; using NadekoBot.Modules.CustomReactions.Services; -using NadekoBot.Core.Services.Database.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; -using NadekoBot.Common; -using NadekoBot.Common.Replacements; namespace NadekoBot.Modules.CustomReactions.Extensions { @@ -89,7 +89,7 @@ namespace NadekoBot.Modules.CustomReactions.Extensions public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client) => cr.Trigger.ResolveTriggerString(ctx, client); - public static Task ResponseWithContextAsync(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool containsAnywhere) + public static Task ResponseWithContextAsync(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool containsAnywhere) => cr.Response.ResolveResponseStringAsync(ctx, client, cr.Trigger.ResolveTriggerString(ctx, client), containsAnywhere); public static async Task Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs) @@ -127,14 +127,35 @@ namespace NadekoBot.Modules.CustomReactions.Extensions public static WordPosition GetWordPosition(this string str, string word) { - if (str.StartsWith(word + " ")) - return WordPosition.Start; - else if (str.EndsWith(" " + word)) - return WordPosition.End; - else if (str.Contains(" " + word + " ")) - return WordPosition.Middle; - else + var indexOfWord = str.IndexOf(word); + if (indexOfWord == -1) return WordPosition.None; + + if (indexOfWord == 0) // on start + { + if (word.Length < str.Length &&str.isInvalidWordChar(word.Length)) // filter char after word index + return WordPosition.Start; + } + else if ((indexOfWord + word.Length) == str.Length) // on end + { + if (str.isInvalidWordChar(indexOfWord-1)) // filter char before word index + return WordPosition.End; + } + else if (str.isInvalidWordChar(indexOfWord-1) && str.isInvalidWordChar(indexOfWord + word.Length)) // on middle + return WordPosition.Middle; + + return WordPosition.None; + } + + private static bool isInvalidWordChar(this string str, int index) + { + var ch = str[index]; + if ((byte)ch > 64 && (byte)ch <= 90) // must be A-Z + return false; + if ((byte)ch > 97 && (byte)ch <= 122) // a-z + return false; + + return true; } } @@ -145,4 +166,4 @@ namespace NadekoBot.Modules.CustomReactions.Extensions Middle, End, } -} +} \ No newline at end of file From 4f3e2565a49e0c82647ed246c869eef089913599 Mon Sep 17 00:00:00 2001 From: xsftk Date: Sat, 11 Nov 2017 15:04:58 +0700 Subject: [PATCH 2/4] .crca fix? https://github.com/Kwoth/NadekoBot/issues/1455 GetWordPosition should ignore special characters now chinese sentence doesnt have space as word separator nor a word separator at all, same goes for thai,etc. iirc :) --- .../Modules/CustomReactions/Extensions/Extensions.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs index 6c8cc4cb..7e24cff6 100644 --- a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs +++ b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs @@ -133,15 +133,15 @@ namespace NadekoBot.Modules.CustomReactions.Extensions if (indexOfWord == 0) // on start { - if (word.Length < str.Length &&str.isInvalidWordChar(word.Length)) // filter char after word index + if (word.Length < str.Length && str.isInvalidWordChar(word.Length)) // filter char after word index return WordPosition.Start; } else if ((indexOfWord + word.Length) == str.Length) // on end { - if (str.isInvalidWordChar(indexOfWord-1)) // filter char before word index + if (str.isInvalidWordChar(indexOfWord - 1)) // filter char before word index return WordPosition.End; } - else if (str.isInvalidWordChar(indexOfWord-1) && str.isInvalidWordChar(indexOfWord + word.Length)) // on middle + else if (str.isInvalidWordChar(indexOfWord - 1) && str.isInvalidWordChar(indexOfWord + word.Length)) // on middle return WordPosition.Middle; return WordPosition.None; @@ -149,10 +149,10 @@ namespace NadekoBot.Modules.CustomReactions.Extensions private static bool isInvalidWordChar(this string str, int index) { - var ch = str[index]; - if ((byte)ch > 64 && (byte)ch <= 90) // must be A-Z + var ch = (ushort)str[index]; + if (ch > 64 && ch <= 90) // must be A-Z return false; - if ((byte)ch > 97 && (byte)ch <= 122) // a-z + if (ch > 97 && ch <= 122) // a-z return false; return true; From adb850d3068be46059f329930672c2fd60a3ca15 Mon Sep 17 00:00:00 2001 From: xsftk Date: Sat, 11 Nov 2017 15:19:18 +0700 Subject: [PATCH 3/4] lil fix --- .../Modules/CustomReactions/Extensions/Extensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs index 7e24cff6..1711b95c 100644 --- a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs +++ b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs @@ -150,9 +150,9 @@ namespace NadekoBot.Modules.CustomReactions.Extensions private static bool isInvalidWordChar(this string str, int index) { var ch = (ushort)str[index]; - if (ch > 64 && ch <= 90) // must be A-Z + if (ch >= 65 && ch <= 90) // must be A-Z return false; - if (ch > 97 && ch <= 122) // a-z + if (ch >= 97 && ch <= 122) // a-z return false; return true; From e8fc244bb60486691ca384334a0dfeacba57ec40 Mon Sep 17 00:00:00 2001 From: xsftk Date: Wed, 15 Nov 2017 13:51:00 +0700 Subject: [PATCH 4/4] some renaming and removed comment --- .../CustomReactions/Extensions/Extensions.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs index 1711b95c..b88fd3c0 100644 --- a/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs +++ b/NadekoBot.Core/Modules/CustomReactions/Extensions/Extensions.cs @@ -127,32 +127,32 @@ namespace NadekoBot.Modules.CustomReactions.Extensions public static WordPosition GetWordPosition(this string str, string word) { - var indexOfWord = str.IndexOf(word); - if (indexOfWord == -1) + var wordIndex = str.IndexOf(word); + if (wordIndex == -1) return WordPosition.None; - if (indexOfWord == 0) // on start + if (wordIndex == 0) { - if (word.Length < str.Length && str.isInvalidWordChar(word.Length)) // filter char after word index + if (word.Length < str.Length && str.isValidWordDivider(word.Length)) return WordPosition.Start; } - else if ((indexOfWord + word.Length) == str.Length) // on end + else if ((wordIndex + word.Length) == str.Length) { - if (str.isInvalidWordChar(indexOfWord - 1)) // filter char before word index + if (str.isValidWordDivider(wordIndex - 1)) return WordPosition.End; } - else if (str.isInvalidWordChar(indexOfWord - 1) && str.isInvalidWordChar(indexOfWord + word.Length)) // on middle + else if (str.isValidWordDivider(wordIndex - 1) && str.isValidWordDivider(wordIndex + word.Length)) return WordPosition.Middle; return WordPosition.None; } - private static bool isInvalidWordChar(this string str, int index) + private static bool isValidWordDivider(this string str, int index) { - var ch = (ushort)str[index]; - if (ch >= 65 && ch <= 90) // must be A-Z + var ch = str[index]; + if (ch >= 'a' && ch <= 'z') return false; - if (ch >= 97 && ch <= 122) // a-z + if (ch >= 'A' && ch <= 'Z') return false; return true;