From ed26623e17e8dfcc036f88cca6de10d5a35697ec Mon Sep 17 00:00:00 2001 From: uzvkl Date: Mon, 20 May 2019 00:54:14 +0200 Subject: Reorganize Code delete ZoBotanica --- DSALib/Auxiliary/Calculator/Argument.cs | 2 +- DSALib/Auxiliary/Calculator/ISolvable.cs | 2 +- DSALib/Auxiliary/Calculator/Operator.cs | 4 +- DSALib/Auxiliary/Calculator/Ops.cs | 2 +- DSALib/Auxiliary/Calculator/StringSolver.cs | 4 +- DSALib/Auxiliary/CommandInfo.cs | 2 +- DSALib/Auxiliary/Dice.cs | 2 +- DSALib/Auxiliary/Extensions.cs | 2 +- DSALib/Auxiliary/IDataObjectEnumerableExtension.cs | 25 +++++ DSALib/Auxiliary/RandomMisc.cs | 2 +- DSALib/Auxiliary/SpellCorrect.cs | 105 ++++++--------------- DSALib/Auxiliary/TalentEnumerableExtension.cs | 27 +++--- DSALib/Auxiliary/WeaponImporter.cs | 6 +- 13 files changed, 83 insertions(+), 102 deletions(-) create mode 100644 DSALib/Auxiliary/IDataObjectEnumerableExtension.cs (limited to 'DSALib/Auxiliary') diff --git a/DSALib/Auxiliary/Calculator/Argument.cs b/DSALib/Auxiliary/Calculator/Argument.cs index 5ed9ee3..e681377 100644 --- a/DSALib/Auxiliary/Calculator/Argument.cs +++ b/DSALib/Auxiliary/Calculator/Argument.cs @@ -1,6 +1,6 @@ using System; -namespace DSACore.Auxiliary.Calculator +namespace DSALib.Auxiliary.Calculator { /// /// Provides an ISolvable class to save numbers. The class handles Argument checking and conversion from string to int. diff --git a/DSALib/Auxiliary/Calculator/ISolvable.cs b/DSALib/Auxiliary/Calculator/ISolvable.cs index 7be4d19..844e9b3 100644 --- a/DSALib/Auxiliary/Calculator/ISolvable.cs +++ b/DSALib/Auxiliary/Calculator/ISolvable.cs @@ -1,4 +1,4 @@ -namespace DSACore.Auxiliary.Calculator +namespace DSALib.Auxiliary.Calculator { /// /// Object has to be able to return an integer as it's value diff --git a/DSALib/Auxiliary/Calculator/Operator.cs b/DSALib/Auxiliary/Calculator/Operator.cs index 31b2a9b..e6aeec6 100644 --- a/DSALib/Auxiliary/Calculator/Operator.cs +++ b/DSALib/Auxiliary/Calculator/Operator.cs @@ -1,7 +1,7 @@ using System; -using DSACorev.Auxiliary.Calculator; +using DSALibv.Auxiliary.Calculator; -namespace DSACore.Auxiliary.Calculator +namespace DSALib.Auxiliary.Calculator { /// /// The Operator Class represents a binary operator with tow Arguments and an Operation type diff --git a/DSALib/Auxiliary/Calculator/Ops.cs b/DSALib/Auxiliary/Calculator/Ops.cs index a5c9a2d..93046d0 100644 --- a/DSALib/Auxiliary/Calculator/Ops.cs +++ b/DSALib/Auxiliary/Calculator/Ops.cs @@ -1,4 +1,4 @@ -namespace DSACorev.Auxiliary.Calculator +namespace DSALibv.Auxiliary.Calculator { /// /// The Different Operations, witch can be performed in execution-order diff --git a/DSALib/Auxiliary/Calculator/StringSolver.cs b/DSALib/Auxiliary/Calculator/StringSolver.cs index b2a7d83..bf903da 100644 --- a/DSALib/Auxiliary/Calculator/StringSolver.cs +++ b/DSALib/Auxiliary/Calculator/StringSolver.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using DSACorev.Auxiliary.Calculator; +using DSALibv.Auxiliary.Calculator; -namespace DSACore.Auxiliary.Calculator +namespace DSALib.Auxiliary.Calculator { /// /// The StringSolver divides the calculation string into operations and SubStringSolvers if the string contains diff --git a/DSALib/Auxiliary/CommandInfo.cs b/DSALib/Auxiliary/CommandInfo.cs index 1472587..d8e2188 100644 --- a/DSALib/Auxiliary/CommandInfo.cs +++ b/DSALib/Auxiliary/CommandInfo.cs @@ -1,6 +1,6 @@ using System.Linq; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public struct CommandInfo { diff --git a/DSALib/Auxiliary/Dice.cs b/DSALib/Auxiliary/Dice.cs index 3dd6562..b07d47f 100644 --- a/DSALib/Auxiliary/Dice.cs +++ b/DSALib/Auxiliary/Dice.cs @@ -1,7 +1,7 @@ using System; using System.Linq; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public static class Dice // roll it! { diff --git a/DSALib/Auxiliary/Extensions.cs b/DSALib/Auxiliary/Extensions.cs index f8e9d8e..7d367a5 100644 --- a/DSALib/Auxiliary/Extensions.cs +++ b/DSALib/Auxiliary/Extensions.cs @@ -1,4 +1,4 @@ -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public static class StringExtension { diff --git a/DSALib/Auxiliary/IDataObjectEnumerableExtension.cs b/DSALib/Auxiliary/IDataObjectEnumerableExtension.cs new file mode 100644 index 0000000..b8a6067 --- /dev/null +++ b/DSALib/Auxiliary/IDataObjectEnumerableExtension.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using DSALib.Auxiliary; +using DSALib.Models.Database; + +namespace DSACore.Auxiliary +{ + public static class DataObjectEnumerableExtension + { + public static IDataObject Match(this IEnumerable dataObjects, string name) + { + return (dataObjects as IOrderedEnumerable ?? throw new InvalidOperationException()).OrderBy(x => SpellCorrect.Compare(name,x.Name)).Last(); + } + + public static bool TryMatch(this IEnumerable dataObjects,out IDataObject data, string name) + { + data = (dataObjects as IOrderedEnumerable ?? throw new InvalidOperationException()).OrderBy(x => SpellCorrect.Compare(name,x.Name)).Last(); + + return SpellCorrect.IsMatch(name, data.Name); + } + } +} diff --git a/DSALib/Auxiliary/RandomMisc.cs b/DSALib/Auxiliary/RandomMisc.cs index 72c2234..2723930 100644 --- a/DSALib/Auxiliary/RandomMisc.cs +++ b/DSALib/Auxiliary/RandomMisc.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Text; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public static class RandomMisc { diff --git a/DSALib/Auxiliary/SpellCorrect.cs b/DSALib/Auxiliary/SpellCorrect.cs index 77d1cf3..79908c4 100644 --- a/DSALib/Auxiliary/SpellCorrect.cs +++ b/DSALib/Auxiliary/SpellCorrect.cs @@ -1,106 +1,61 @@ using System; -using System.Diagnostics; -using System.Linq; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { - public class SpellCorrect : StringComparer + public class SpellCorrect { - public const int ErrorThreshold = 94100; + public const double ErrorThreshold = 1 / 3.0; + private const double Match = 3.0; + private const double Gap = -1.5; + private const double Mismatch = -2.0; - public override int Compare(string x, string y) - { - return CompareEasy(x, y); - } - - public static int CompareEasy(string x, string y) - { - if (string.IsNullOrEmpty(x)) throw new ArgumentException("message", nameof(x)); - - if (string.IsNullOrEmpty(y)) throw new ArgumentException("message", nameof(y)); - - if (x.Equals(y)) return 0; - - x = x.ToLower(); - y = y.ToLower(); - if (x.Equals(y)) return 1; - - var subs = y.Split(' ', '/'); - var score = subs.Count(); - foreach (var s in subs) - if (s.Equals(x)) - score--; - - if (score < subs.Count()) return score + 1; - - return 100000 - (int) (CompareExact(x, y) * 1000.0); - /*if (y.Contains(x)) - return 6;*/ - } - - public override bool Equals(string x, string y) - { - Debug.Assert(x != null, nameof(x) + " != null"); - return x.Equals(y); - } - - public override int GetHashCode(string obj) - { - throw new NotImplementedException(); - } - - public static double CompareExact(string s, string q) + public static double Compare(string s, string q) { s = s.ToLower(); q = q.ToLower(); int i, j; - const double Match = 3.0; - const double Gap = -2.0; - const double Mismatch = -2.0; - - double decay; - + var matrix = new double[s.Length + 1, q.Length + 1]; var max = 0.0; matrix[0, 0] = 0.0; for (i = 1; i < s.Length; i++) - // matrix[i, 0] = 0.0; matrix[i, 0] = i * Gap; for (i = 1; i < q.Length; i++) matrix[0, i] = 0.0; for (i = 1; i <= s.Length; i++) - for (j = 1; j <= q.Length; j++) - { - decay = j / (double) (s.Length * 1000); - var add = s[i - 1] == q[j - 1] ? Match - decay : Mismatch; - var score = matrix[i - 1, j - 1] + add; - - if (score < matrix[i - 1, j] + Gap) score = matrix[i - 1, j] + Gap; + for (j = 1; j <= q.Length; j++) + { + double decay = j / (s.Length * 1000.0); + var add = s[i - 1] == q[j - 1] ? Match - decay : Mismatch; + var score = matrix[i - 1, j - 1] + add; - if (score < matrix[i, j - 1] + Gap) score = matrix[i, j - 1] + Gap; + if (score < matrix[i - 1, j] + Gap) score = matrix[i - 1, j] + Gap; - if (i > 1 && j > 1) - if (s[i - 1] == q[j - 2] && s[i - 2] == q[j - 1]) - { - add = 3 / 2.0 * Match - decay; - if (score < matrix[i - 2, j - 2] + add) score = matrix[i - 2, j - 2] + add; - } + if (score < matrix[i, j - 1] + Gap) score = matrix[i, j - 1] + Gap; - // if (score < 0) - // { - // score = 0; - // } + if (i > 1 && j > 1) + if (s[i - 1] == q[j - 2] && s[i - 2] == q[j - 1]) + { + add = 3 / 2.0 * Match - decay; + if (score < matrix[i - 2, j - 2] + add) score = matrix[i - 2, j - 2] + add; + } - if (max < score && i == s.Length) max = score; + if (max < score && i == s.Length) max = score; - matrix[i, j] = score; - } + matrix[i, j] = score; + } return max; } + + public static bool IsMatch(string s1, string s2) + { + var score = Compare(s1, s2); + return score > ErrorThreshold * s1.Length; + } } } \ No newline at end of file diff --git a/DSALib/Auxiliary/TalentEnumerableExtension.cs b/DSALib/Auxiliary/TalentEnumerableExtension.cs index d83114c..6ec7fcc 100644 --- a/DSALib/Auxiliary/TalentEnumerableExtension.cs +++ b/DSALib/Auxiliary/TalentEnumerableExtension.cs @@ -1,33 +1,34 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using DSACore.DSA_Game.Characters; -using DSALib; +using DSACore.Auxiliary; +using DSALib.DSA_Game.Characters; +using DSALib.Models.Dsa; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public static class TalentEnumerableExtension { - public static string ProbenTest(this IEnumerable List, Character c, string talent, int erschwernis = 0) + public static string ProbenTest(this IEnumerable List, Character c, string talentName, int erschwernis = 0) { var output = new StringBuilder(); var sc = new SpellCorrect(); - var tTalent = List.OrderBy(x => sc.Compare(talent, x.Name)).First(); - if (sc.Compare(talent, tTalent.Name) > SpellCorrect.ErrorThreshold) - return $"{c.Name} kann nicht {talent}..."; + if (!List.TryMatch(out var iTalent, talentName)) + return $"{c.Name} kann nicht {talentName}..."; - var props = tTalent.GetEigenschaften(); // get the required properties - var tap = tTalent.Value; // get taw - var werte = props.Select(p => c.Eigenschaften[c.PropTable[p]]).ToList(); + var talent = (Talent) iTalent; + var props = talent.GetEigenschaften(); // get the required properties + var tap = talent.Value; // get taw + var werte = props.Select(p => c.Eigenschaften[c.PropTable[p]]).ToArray(); output.AppendFormat( "{0} würfelt: {1} \n{2} - {3} taw:{4} {5} \n", c.Name, - tTalent.Name, - tTalent.Probe, + talent.Name, + talent.Probe, string.Join("/", werte), - tTalent.Value, + talent.Value, erschwernis.Equals(0) ? string.Empty : "Erschwernis: " + erschwernis); output.Append(" "); diff --git a/DSALib/Auxiliary/WeaponImporter.cs b/DSALib/Auxiliary/WeaponImporter.cs index 3375236..12d243f 100644 --- a/DSALib/Auxiliary/WeaponImporter.cs +++ b/DSALib/Auxiliary/WeaponImporter.cs @@ -4,10 +4,10 @@ using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using System.Threading.Tasks; -using DSACore.FireBase; -using DSACore.Models.Database.DSA; +using DSALib.Models.Database.DSA; +using DSALib.FireBase; -namespace DSACore.Auxiliary +namespace DSALib.Auxiliary { public class WeaponImporter { -- cgit v1.2.3-54-g00ecf