Make learn and unlearn commands simpler

This commit is contained in:
Basique Evangelist 2019-12-20 18:14:19 +00:00
parent 6021c7d7dc
commit 799128667f
4 changed files with 11 additions and 58 deletions

View File

@ -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))
return $"Удалил слово \"{word}\"";
switch (res) else
{ return $"Не нашел слово \"{word}\"";
case SearchDictionary.UnlearnResult.Demoted:
return $"Понизил слово \"{word}\"";
case SearchDictionary.UnlearnResult.Removed:
return $"Удалил слово \"{word}\"";
case SearchDictionary.UnlearnResult.NotFound:
default:
return $"Не нашел слово \"{word}\"";
}
} }
} }
} }

View File

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

View File

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

View File

@ -2,13 +2,9 @@
"Unbleeper": { "Unbleeper": {
"BleepedSwearsRegex": "[а-яА-Я@\\*#]+", "BleepedSwearsRegex": "[а-яА-Я@\\*#]+",
"MinWordLength": 3, "MinWordLength": 3,
"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
} }
} }