summaryrefslogtreecommitdiff
path: root/DSALib/Auxiliary/SpellCorrect.cs
diff options
context:
space:
mode:
Diffstat (limited to 'DSALib/Auxiliary/SpellCorrect.cs')
-rw-r--r--DSALib/Auxiliary/SpellCorrect.cs105
1 files changed, 30 insertions, 75 deletions
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