Initial release

This commit is contained in:
jetsparrow 2019-08-14 00:05:56 +03:00
commit 2b8fd15e8e
18 changed files with 2119 additions and 0 deletions

46
.gitignore vendored Normal file
View File

@ -0,0 +1,46 @@
# Autosave files
*~
# build
[Oo]bj/
[Bb]in/
packages/
TestResults/
# globs
Makefile.in
*.DS_Store
*.sln.cache
*.suo
*.cache
*.pidb
*.userprefs
*.usertasks
config.log
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.user
*.tar.gz
tarballs/
test-results/
Thumbs.db
.vs/
# Mac bundle stuff
*.dmg
*.app
# resharper
*_Resharper.*
*.Resharper
# dotCover
*.dotCover
#secret config
karma.cfg.json
*secrets.ini
*.secret.json

25
AntiAntiSwearingBot.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2036
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AntiAntiSwearingBot", "AntiAntiSwearingBot\AntiAntiSwearingBot.csproj", "{66AFFD7B-5B2D-4C85-8523-770702255511}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{66AFFD7B-5B2D-4C85-8523-770702255511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66AFFD7B-5B2D-4C85-8523-770702255511}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66AFFD7B-5B2D-4C85-8523-770702255511}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66AFFD7B-5B2D-4C85-8523-770702255511}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0B6EAE6-9D61-4E40-B5B8-3269484DB0D2}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,127 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AntiAntiSwearingBot.Commands;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace AntiAntiSwearingBot
{
public class AntiAntiSwearingBot : IDisposable
{
Config Config { get; }
SearchDictionary Dict { get; }
public AntiAntiSwearingBot(Config cfg, SearchDictionary dict)
{
Config = cfg;
Dict = dict;
BleepedSwearsRegex = new Regex(cfg.BleepedSwearsRegex, RegexOptions.Compiled);
NonWordRegex = new Regex("\\W", RegexOptions.Compiled);
MentionRegex = new Regex("@[a-zA-Z0-9_]+", RegexOptions.Compiled);
}
TelegramBotClient Client { get; set; }
ChatCommandRouter Router { get; set; }
User Me { get; set; }
public async Task Init()
{
var httpProxy = new WebProxy($"{Config.Proxy.Url}:{Config.Proxy.Port}")
{
Credentials = new NetworkCredential(Config.Proxy.Login, Config.Proxy.Password)
};
Client = new TelegramBotClient(Config.ApiKey, httpProxy);
Me = await Client.GetMeAsync();
Router = new ChatCommandRouter(Me.Username);
Router.Add(new LearnCommand(Dict), "learn");
Router.Add(new UnlearnCommand(Dict), "unlearn");
Client.OnMessage += BotOnMessageReceived;
Client.StartReceiving();
}
public async Task Stop()
{
Dict.Save();
Dispose();
}
#region service
Regex BleepedSwearsRegex { get; }
Regex NonWordRegex { get; }
Regex MentionRegex { get; }
string UnbleepSwears(string text)
{
if (string.IsNullOrWhiteSpace(text))
return null;
var words = BleepedSwearsRegex.Matches(text)
.Select(m => m.Value)
.Where(m => NonWordRegex.IsMatch(m))
.Where(m => !MentionRegex.IsMatch(m))
.ToArray();
if (words.Any())
{
var response = new StringBuilder();
for (int i = 0; i < words.Length; ++i)
{
var m = Dict.Match(words[i]);
response.AppendLine(new string('*', i + 1) + m.Word + new string('?', m.Distance));
}
return response.ToString();
}
else
return null;
}
void BotOnMessageReceived(object sender, MessageEventArgs args)
{
var msg = args.Message;
if (msg == null || msg.Type != MessageType.Text)
return;
string commandResponse = null;
try { commandResponse = Router.Execute(sender, args); }
catch { }
if (commandResponse != null)
{
Client.SendTextMessageAsync(
args.Message.Chat.Id,
commandResponse,
replyToMessageId: args.Message.MessageId);
}
else
{
var unbleepResponse = UnbleepSwears(msg.Text);
if (unbleepResponse != null)
Client.SendTextMessageAsync(
args.Message.Chat.Id,
unbleepResponse,
replyToMessageId: args.Message.MessageId);
}
}
#endregion
#region IDisposable
public void Dispose()
{
Client.StopReceiving();
}
#endregion
}
}

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="ObsceneDictionary.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Telegram.Bot" Version="14.10.0" />
</ItemGroup>
<ItemGroup>
<None Update="aasb.cfg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="aasb.cfg.secret.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="dict\ObsceneDictionaryRu.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using Telegram.Bot.Args;
using AntiAntiSwearingBot.Commands;
namespace AntiAntiSwearingBot
{
public interface IChatCommand
{
string Execute(CommandString cmd, MessageEventArgs messageEventArgs);
}
public class ChatCommandRouter
{
string Username { get; }
Dictionary<string, IChatCommand> Commands { get; }
public ChatCommandRouter(string username)
{
Username = username;
Commands = new Dictionary<string, IChatCommand>();
}
public string Execute(object sender, MessageEventArgs args)
{
var text = args.Message.Text;
if (CommandString.TryParse(text, out var cmd))
{
if (cmd.UserName != null && cmd.UserName != Username)
return null;
if (Commands.ContainsKey(cmd.Command))
{
try { return Commands[cmd.Command].Execute(cmd, args); }
catch { }
}
}
return null;
}
public void Add(IChatCommand c, params string[] cmds)
{
foreach (var cmd in cmds)
{
if (Commands.ContainsKey(cmd))
throw new ArgumentException($"collision for {cmd}, commands {Commands[cmd].GetType()} and {c.GetType()}");
Commands[cmd] = c;
}
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AntiAntiSwearingBot.Commands
{
public class CommandString
{
public CommandString(string command, string username, params string[] parameters)
{
Command = command;
Parameters = parameters;
}
public string Command { get; }
public string UserName { get; }
public string[] Parameters { get; }
static readonly char[] WS_CHARS = new[] { ' ', '\r', '\n', '\n' };
public static bool TryParse(string s, out CommandString result)
{
result = null;
if (string.IsNullOrWhiteSpace(s) || s[0] != '/')
return false;
string[] words = s.Split(WS_CHARS, StringSplitOptions.RemoveEmptyEntries);
var cmdRegex = new Regex(@"/(?<cmd>\w+)(@(?<name>\w+))?");
var match = cmdRegex.Match(words.First());
if (!match.Success)
return false;
string cmd = match.Groups["cmd"].Captures[0].Value;
string username = match.Groups["name"].Captures.Count > 0 ? match.Groups["name"].Captures[0].Value : null;
string[] parameters = words.Skip(1).ToArray();
result = new CommandString(cmd, username, parameters);
return true;
}
}
}

View File

@ -0,0 +1,29 @@
using System.Linq;
using System.Text.RegularExpressions;
using Telegram.Bot.Args;
namespace AntiAntiSwearingBot.Commands
{
public class LearnCommand : IChatCommand
{
SearchDictionary Dict { get; }
public LearnCommand(SearchDictionary dict)
{
Dict = dict;
}
public string Execute(CommandString cmd, MessageEventArgs args)
{
var word = cmd.Parameters.FirstOrDefault();
if (string.IsNullOrWhiteSpace(word))
return null;
if (!Regex.IsMatch(word, @"[а-яА-Я]+"))
return null;
bool newWord = Dict.Learn(word);
return newWord ? $"Принято слово \"{word}\"" : $"Поднял рейтинг слову \"{word}\"";
}
}
}

View File

@ -0,0 +1,38 @@
using System.Linq;
using System.Text.RegularExpressions;
using Telegram.Bot.Args;
namespace AntiAntiSwearingBot.Commands
{
public class UnlearnCommand : IChatCommand
{
SearchDictionary Dict { get; }
public UnlearnCommand(SearchDictionary dict)
{
Dict = dict;
}
public string Execute(CommandString cmd, MessageEventArgs args)
{
var word = cmd.Parameters.FirstOrDefault();
if (string.IsNullOrWhiteSpace(word))
return null;
if (!Regex.IsMatch(word, @"[а-яА-Я]+"))
return null;
var res = Dict.Unlearn(word);
switch (res)
{
case SearchDictionary.UnlearnResult.Demoted:
return $"Понизил слово \"{word}\"";
case SearchDictionary.UnlearnResult.Removed:
return $"Удалил слово \"{word}\"";
case SearchDictionary.UnlearnResult.NotFound:
default:
return $"Не нашел слово \"{word}\"";
}
}
}
}

View File

@ -0,0 +1,32 @@
namespace AntiAntiSwearingBot
{
public class Config : ConfigBase
{
public string ApiKey { get; private set; }
public string BleepedSwearsRegex { get; private set; }
public struct ProxySettings
{
public string Url { get; private set; }
public int Port { get; private set; }
public string Login { get; private set; }
public string Password { get; private set; }
}
public ProxySettings Proxy { get; private set; }
public struct SearchDictionarySettings
{
public string DictionaryPath { get; private set; }
public double LearnNudgeFactor { get; private set; }
public double LearnInitialRating { get; private set; }
public int MinUnlearnNudge { get; private set; }
public double UnlearnNudgeFactor { get; private set; }
}
public SearchDictionarySettings SearchDictionary { get; private set; }
}
}

View File

@ -0,0 +1,76 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection;
using Newtonsoft.Json.Serialization;
namespace AntiAntiSwearingBot
{
public abstract class ConfigBase
{
public static T Load<T>(params string[] paths) where T : ConfigBase, new()
{
var result = new T();
var configJson = new JObject();
var mergeSettings = new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Union
};
foreach (var path in paths)
{
try { configJson.Merge(JObject.Parse(File.ReadAllText(path)), mergeSettings);}
catch { }
}
using (var sr = configJson.CreateReader())
{
var settings = new JsonSerializerSettings
{
ContractResolver = new PrivateSetterContractResolver()
};
JsonSerializer.CreateDefault(settings).Populate(sr, result);
}
return result;
}
}
public class PrivateSetterContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jProperty = base.CreateProperty(member, memberSerialization);
if (jProperty.Writable)
return jProperty;
jProperty.Writable = member.IsPropertyWithSetter();
return jProperty;
}
}
public class PrivateSetterCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jProperty = base.CreateProperty(member, memberSerialization);
if (jProperty.Writable)
return jProperty;
jProperty.Writable = member.IsPropertyWithSetter();
return jProperty;
}
}
internal static class MemberInfoExtensions
{
internal static bool IsPropertyWithSetter(this MemberInfo member)
{
var property = member as PropertyInfo;
return property?.GetSetMethod(true) != null;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace AntiAntiSwearingBot
{
public static class IListExtensions
{
public static void Move<T>(this IList<T> list, int from, int to)
{
if (from < 0 || from > list.Count)
throw new ArgumentOutOfRangeException("from");
if (to < 0 || to > list.Count)
throw new ArgumentOutOfRangeException("to");
if (from == to)
return;
var item = list[from];
list.RemoveAt(from);
if (to > from) --to; // the actual index could have shifted due to the removal
list.Insert(to, item);
}
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace AntiAntiSwearingBot
{
public static class IReadOnlyDictionaryExtensions
{
public static TValue GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key)
{
TValue res = default(TValue);
if (key != null)
dict.TryGetValue(key, out res);
return res;
}
}
}

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AntiAntiSwearingBot
{
static class Language
{
public static int HammingDistance(string a, string b)
{
if (string.IsNullOrEmpty(a))
{
if (string.IsNullOrEmpty(b))
return 0;
return b.Length;
}
int dist = 0;
int len = Math.Min(a.Length, b.Length);
int leftover = Math.Max(a.Length, b.Length) - len;
for (int i = 0; i < len; ++i)
if (!CharMatch(a[i], b[i]))
++dist;
return leftover + dist;
}
static int min(int a, int b, int c) { return Math.Min(Math.Min(a, b), c); }
public static int LevenshteinDistance(string a, string b)
{
int[] prevRow = new int[b.Length + 1];
int[] thisRow = new int[b.Length + 1];
// init thisRow as
for (int i = 0; i < prevRow.Length; i++) prevRow[i] = i;
for (int i = 0; i < a.Length; i++)
{
thisRow[0] = i + 1;
for (int j = 0; j < b.Length; j++)
{
var cost = CharMatch(a[i], b[j]) ? 0 : 1;
thisRow[j + 1] = min(thisRow[j] + 1, prevRow[j + 1] + 1, prevRow[j] + cost);
}
var t = prevRow;
prevRow = thisRow;
thisRow = t;
}
return prevRow[b.Length];
}
public static bool CharMatch(char a, char b)
=> a == b || !char.IsLetterOrDigit(a) || !char.IsLetterOrDigit(b);
/// <summary>
/// Compute the distance between two strings.
/// </summary>
public static int Compute(string s, string t)
{
int n = s.Length;
int m = t.Length;
int[,] d = new int[n + 1, m + 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

@ -0,0 +1,38 @@
using System;
namespace AntiAntiSwearingBot
{
public static class Program
{
public enum ExitCode : int
{
Ok = 0,
ErrorNotStarted = 0x80,
ErrorRunning = 0x81,
ErrorException = 0x82,
ErrorInvalidCommandLine = 0x100
};
public static int Main(string[] args)
{
try
{
var cfg = Config.Load<Config>("aasb.cfg.json", "aasb.cfg.secret.json");
var dict = new SearchDictionary(cfg);
var bot = new AntiAntiSwearingBot(cfg, dict);
bot.Init().Wait();
Console.WriteLine("AntiAntiSwear started. Press any key to exit...");
Environment.ExitCode = (int)ExitCode.ErrorRunning;
Console.ReadKey();
Console.WriteLine("Waiting for exit...");
bot.Stop().Wait();
return (int)ExitCode.Ok;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return (int)ExitCode.ErrorException;
}
}
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PublishDir>i:\aasb</PublishDir>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AntiAntiSwearingBot
{
public class SearchDictionary
{
public SearchDictionary(Config cfg)
{
var s = cfg.SearchDictionary;
path = s.DictionaryPath;
learnInitialRating = Math.Clamp(s.LearnInitialRating, 0,1);
learnNudgeFactor = Math.Clamp(s.LearnNudgeFactor, 0, 1);
unlearnNudgeFactor = Math.Clamp(s.UnlearnNudgeFactor, 0, 1);
minUnlearnNudge = Math.Max(s.MinUnlearnNudge, 0);
words = File.ReadAllLines(path).ToList();
}
public void Save()
{
File.WriteAllLines(path + ".tmp", words);
File.Move(path + ".tmp", path);
}
public struct WordMatch
{
public string Word;
public int Distance;
public int Rating;
}
public WordMatch Match(string pattern)
=> AllMatches(pattern).First();
public IEnumerable<WordMatch> AllMatches(string pattern)
{
lock (SyncRoot)
{
pattern = pattern.ToLowerInvariant();
return words
.Select((w, i) => new WordMatch { Word = w, Distance = Language.LevenshteinDistance(pattern, w), Rating = i })
.OrderBy(m => m.Distance)
.ThenBy(m => m.Rating);
}
}
public bool Learn(string word)
{
lock (SyncRoot)
{
int index = words.IndexOf(word);
if (index > 0)
{
int newIndex = (int)(index * learnNudgeFactor);
words.Move(index, newIndex);
return false;
}
else
{
words.Insert((int)(words.Count * learnInitialRating), word);
return true;
}
}
}
public enum UnlearnResult { NotFound, Demoted, Removed }
public UnlearnResult Unlearn(string word)
{
lock (SyncRoot)
{
int index = words.IndexOf(word);
if (index < 0)
return UnlearnResult.NotFound;
int indexFromEnd = words.Count - 1 - index;
int change = Math.Max(minUnlearnNudge, (int)(indexFromEnd * unlearnNudgeFactor ));
int newIndex = index + change;
if (newIndex > words.Count)
{
words.RemoveAt(index);
return UnlearnResult.Removed;
}
else
{
words.Move(index, newIndex);
return UnlearnResult.Demoted;
}
}
}
#region service
string path;
double learnInitialRating = 0.75;
double learnNudgeFactor = 0.5;
double unlearnNudgeFactor = 0.66;
int minUnlearnNudge = 5;
object SyncRoot = new object();
List<string> words;
#endregion
}
}

View File

@ -0,0 +1,10 @@
{
"BleepedSwearsRegex": "[а-яА-Я@\\*#]+",
"SearchDictionary": {
"DictionaryPath": "dict/ObsceneDictionaryRu.txt",
"LearnNudgeFactor": 0.5,
"LearnInitialRating": 0.75,
"MinUnlearnNudge": 5,
"UnlearnNudgeFactor": 0.66
}
}

File diff suppressed because it is too large Load Diff