mirror of
https://github.com/Jetsparrow/antiantiswearingbot.git
synced 2026-01-21 07:16:08 +03:00
Make learn and unlearn commands simpler
This commit is contained in:
parent
6021c7d7dc
commit
799128667f
@ -21,18 +21,10 @@ namespace AntiAntiSwearingBot.Commands
|
|||||||
|
|
||||||
if (!Regex.IsMatch(word, @"[а-яА-Я]+"))
|
if (!Regex.IsMatch(word, @"[а-яА-Я]+"))
|
||||||
return null;
|
return null;
|
||||||
var res = Dict.Unlearn(word);
|
if (Dict.Unlearn(word))
|
||||||
|
|
||||||
switch (res)
|
|
||||||
{
|
|
||||||
case SearchDictionary.UnlearnResult.Demoted:
|
|
||||||
return $"Понизил слово \"{word}\"";
|
|
||||||
case SearchDictionary.UnlearnResult.Removed:
|
|
||||||
return $"Удалил слово \"{word}\"";
|
return $"Удалил слово \"{word}\"";
|
||||||
case SearchDictionary.UnlearnResult.NotFound:
|
else
|
||||||
default:
|
|
||||||
return $"Не нашел слово \"{word}\"";
|
return $"Не нашел слово \"{word}\"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,11 +18,6 @@
|
|||||||
public struct SearchDictionarySettings
|
public struct SearchDictionarySettings
|
||||||
{
|
{
|
||||||
public string DictionaryPath { get; private set; }
|
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 struct ProxySettings
|
public struct ProxySettings
|
||||||
|
|||||||
@ -13,11 +13,6 @@ namespace AntiAntiSwearingBot
|
|||||||
path = s.DictionaryPath;
|
path = s.DictionaryPath;
|
||||||
tmppath = path + ".tmp";
|
tmppath = path + ".tmp";
|
||||||
|
|
||||||
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();
|
words = File.ReadAllLines(path).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,52 +57,27 @@ namespace AntiAntiSwearingBot
|
|||||||
int index = words.IndexOf(word);
|
int index = words.IndexOf(word);
|
||||||
if (index > 0)
|
if (index > 0)
|
||||||
{
|
{
|
||||||
int newIndex = (int)(index * learnNudgeFactor);
|
words.Move(index, 0);
|
||||||
words.Move(index, newIndex);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
words.Insert((int)(words.Count * learnInitialRating), word);
|
words.Insert(0, word);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum UnlearnResult { NotFound, Demoted, Removed }
|
public bool Unlearn(string word)
|
||||||
public UnlearnResult Unlearn(string word)
|
|
||||||
{
|
{
|
||||||
lock (SyncRoot)
|
lock (SyncRoot)
|
||||||
{
|
return words.Remove(word);
|
||||||
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
|
#region service
|
||||||
|
|
||||||
readonly string path, tmppath;
|
readonly string path, tmppath;
|
||||||
|
|
||||||
double learnInitialRating = 0.75;
|
|
||||||
double learnNudgeFactor = 0.5;
|
|
||||||
double unlearnNudgeFactor = 0.66;
|
|
||||||
int minUnlearnNudge = 5;
|
|
||||||
|
|
||||||
object SyncRoot = new object();
|
object SyncRoot = new object();
|
||||||
List<string> words;
|
List<string> words;
|
||||||
|
|
||||||
|
|||||||
@ -5,10 +5,6 @@
|
|||||||
"MinAmbiguousWordLength": 5
|
"MinAmbiguousWordLength": 5
|
||||||
},
|
},
|
||||||
"SearchDictionary": {
|
"SearchDictionary": {
|
||||||
"DictionaryPath": "dict/ObsceneDictionaryRu.txt",
|
"DictionaryPath": "dict/ObsceneDictionaryRu.txt"
|
||||||
"LearnNudgeFactor": 0.5,
|
|
||||||
"LearnInitialRating": 0.75,
|
|
||||||
"MinUnlearnNudge": 5,
|
|
||||||
"UnlearnNudgeFactor": 0.66
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user