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, @"[а-яА-Я]+"))
return null;
var res = Dict.Unlearn(word);
switch (res)
{
case SearchDictionary.UnlearnResult.Demoted:
return $"Понизил слово \"{word}\"";
case SearchDictionary.UnlearnResult.Removed:
if (Dict.Unlearn(word))
return $"Удалил слово \"{word}\"";
case SearchDictionary.UnlearnResult.NotFound:
default:
else
return $"Не нашел слово \"{word}\"";
}
}
}
}

View File

@ -18,11 +18,6 @@
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 struct ProxySettings

View File

@ -13,11 +13,6 @@ namespace AntiAntiSwearingBot
path = s.DictionaryPath;
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();
}
@ -62,52 +57,27 @@ namespace AntiAntiSwearingBot
int index = words.IndexOf(word);
if (index > 0)
{
int newIndex = (int)(index * learnNudgeFactor);
words.Move(index, newIndex);
words.Move(index, 0);
return false;
}
else
{
words.Insert((int)(words.Count * learnInitialRating), word);
words.Insert(0, word);
return true;
}
}
}
public enum UnlearnResult { NotFound, Demoted, Removed }
public UnlearnResult Unlearn(string word)
public bool 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;
}
}
return words.Remove(word);
}
#region service
readonly string path, tmppath;
double learnInitialRating = 0.75;
double learnNudgeFactor = 0.5;
double unlearnNudgeFactor = 0.66;
int minUnlearnNudge = 5;
object SyncRoot = new object();
List<string> words;

View File

@ -5,10 +5,6 @@
"MinAmbiguousWordLength": 5
},
"SearchDictionary": {
"DictionaryPath": "dict/ObsceneDictionaryRu.txt",
"LearnNudgeFactor": 0.5,
"LearnInitialRating": 0.75,
"MinUnlearnNudge": 5,
"UnlearnNudgeFactor": 0.66
"DictionaryPath": "dict/ObsceneDictionaryRu.txt"
}
}