-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCorrectTextBox.cs
More file actions
209 lines (173 loc) · 6.42 KB
/
AutoCorrectTextBox.cs
File metadata and controls
209 lines (173 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Documents;
using Windows.System;
using Microsoft.Extensions.Logging;
using TechCommand.Core.Helpers;
using TechCommand.Core.Services;
namespace TechCommand.Controls;
public class AutoCorrectTextBox : TextBox
{
private Thesaurus thesaurus;
private readonly DispatcherTimer typingTimer;
private const int TypingDelay = 500; // milliseconds
private HashSet<string> customDictionary;
public bool IsAutoCorrectEnabled { get; set; } = true;
public AutoCorrectTextBox(Thesaurus thesaurus)
{
this.thesaurus = thesaurus;
customDictionary = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
typingTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(TypingDelay)
};
typingTimer.Tick += TypingTimer_Tick;
this.TextChanged += AutoCorrectTextBox_TextChanged;
}
private void AutoCorrectTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (!IsAutoCorrectEnabled) return;
typingTimer.Stop();
typingTimer.Start();
}
catch (Exception ex)
{
Logger.Instance.Log($"Error in AutoCorrectTextBox_TextChanged {ex.Message}", LogLevel.Error);
}
}
private void TypingTimer_Tick(object sender, object e)
{
try
{
typingTimer.Stop();
ProcessText();
}
catch (Exception ex)
{
Logger.Instance.Log($"Error in TypingTimer_Tick {ex.Message}", LogLevel.Error);
}
}
private void ProcessText()
{
var text = this.Text;
var words = Regex.Split(text, @"\b").Where(w => !string.IsNullOrWhiteSpace(w)).ToList();
var correctedWords = new List<string>();
var misspelledRanges = new List<(int start, int length)>();
for (var i = 0; i < words.Count; i++)
{
var word = words[i];
if (IsValidWord(word))
correctedWords.Add(word);
else
{
var suggestions = GetContextAwareSuggestions(words, i);
var correctedWord = suggestions.FirstOrDefault() ?? word;
correctedWords.Add(correctedWord);
if (correctedWord == word)
{
var startIndex = string.Join("", correctedWords).Length - word.Length;
misspelledRanges.Add((startIndex, word.Length));
}
}
}
var correctedText = string.Join("", correctedWords);
UpdateTextWithUnderlines(correctedText, misspelledRanges);
}
private bool IsValidWord(string word)
{
return thesaurus.IsValidWord(word) || customDictionary.Contains(word);
}
private List<string> GetContextAwareSuggestions(List<string> words, int currentIndex)
{
var currentWord = words[currentIndex];
var context = new List<string>();
// Get up to two words before and after the current word for context
for (var i = Math.Max(0, currentIndex - 2); i < Math.Min(words.Count, currentIndex + 3); i++)
{
if (i != currentIndex)
context.Add(words[i]);
}
var suggestions = thesaurus.GetSynonyms(currentWord);
// Score suggestions based on context
var scoredSuggestions = suggestions.Select(s => new
{
Word = s,
Score = context.Count(c => thesaurus.GetSynonyms(c).Contains(s))
}).OrderByDescending(s => s.Score).ThenBy(s => LevenshteinDistance(currentWord, s.Word));
return scoredSuggestions.Select(s => s.Word).Take(5).ToList();
}
private int LevenshteinDistance(string s, string t)
{
int[,] d = new int[s.Length + 1, t.Length + 1];
for (int i = 0; i <= s.Length; i++)
d[i, 0] = i;
for (int j = 0; j <= t.Length; j++)
d[0, j] = j;
for (int j = 1; j <= t.Length; j++)
for (int i = 1; i <= s.Length; i++)
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + ((s[i - 1] == t[j - 1]) ? 0 : 1));
return d[s.Length, t.Length];
}
private void UpdateTextWithUnderlines(string correctedText, List<(int start, int length)> misspelledRanges)
{
try
{
var selectionStart = this.SelectionStart;
var textBlock = new RichTextBlock();
var paragraph = new Paragraph();
var lastIndex = 0;
foreach (var (start, length) in misspelledRanges)
{
if (start > lastIndex)
paragraph.Inlines.Add(new Run { Text = correctedText.Substring(lastIndex, start - lastIndex) });
var misspelledRun = new Run { Text = correctedText.Substring(start, length) };
misspelledRun.TextDecorations = TextDecorations.Underline;
paragraph.Inlines.Add(misspelledRun);
lastIndex = start + length;
}
if (lastIndex < correctedText.Length)
paragraph.Inlines.Add(new Run { Text = correctedText.Substring(lastIndex) });
textBlock.Blocks.Add(paragraph);
this.Document = textBlock.GetXaml();
this.SelectionStart = selectionStart;
}
catch (Exception ex)
{
Logger.Instance.Log($"Error in UpdateTextWithUnderline {ex.Message}", LogLevel.Error);
this.Text = correctedText;
}
}
public void AddToCustomDictionary(string word)
{
try
{
customDictionary.Add(word.ToLower());
Logger.Instance.Log($"Added '{word}' to custom dictionary");
ProcessText();
}
catch (Exception ex)
{
Logger.Instance.Log($"Error adding '{word}' to custom dictionary", LogLevel.Error);
}
}
public void RemoveFromCustomDictionary(string word)
{
try
{
customDictionary.Remove(word.ToLower());
Logger.Instance.Log($"Removed '{word}' from custom dictionary");
ProcessText();
}
catch (Exception ex)
{
Logger.Instance.Log($"Error removing '{word}' from custom dictionary", LogLevel.Error);
}
}
}