-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellCheckerEngine.cs
More file actions
177 lines (153 loc) · 5.98 KB
/
SpellCheckerEngine.cs
File metadata and controls
177 lines (153 loc) · 5.98 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
namespace ViSpell
{
/// <summary>
/// Main core engine to check vietnamese spelling
/// </summary>
public class SpellCheckerEngine
{
private static object _syncRoot = new Object();
private static volatile SpellCheckerEngine _instance;
public static SpellCheckerEngine Instance
{
get
{
if (_instance == null)
{
lock (_syncRoot)
{
if (_instance == null)
_instance = new SpellCheckerEngine();
}
}
return _instance;
}
}
/// <summary>
/// Private constructor for Singleton
/// </summary>
private SpellCheckerEngine()
{ }
/// <summary>
/// Result with marked spelling error
/// </summary>
public string Result { get; set; }
public List<string> ArrayToTrim { get; set; }
public List<string> ArrayDoubting { get; set; }
public SpellCheckerEngineWorkingDictionary DictionaryToWork { get; set; }
public CheckingResult Run(string pSourceText)
{
if(string.IsNullOrEmpty(pSourceText))
{
return null;
}
string[] arrSingleWord = pSourceText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string sSingleWord = string.Empty;
string sDoubleWord = string.Empty;
string sTripleWord = string.Empty;
List<string> arrSingleWordFail = new List<string>();
List<string> arrDoubleWordFail = new List<string>();
List<string> arrTripleWordFail = new List<string>();
for (int i = 0; i < arrSingleWord.Length; i++)
{
sSingleWord = NormalizeWord(arrSingleWord[i]);
if (!arrSingleWordFail.Contains(sSingleWord))
{
if (!DictionaryToWork.SingleWordDic.Contains(sSingleWord))
{
arrSingleWordFail.Add(sSingleWord);
}
// TODO: support more uppercase unicode letter of VNese
else if (Regex.IsMatch(arrSingleWord[i], "[A-Z]{2}"))
{
arrSingleWordFail.Add(arrSingleWord[i]);
}
}
if (i < arrSingleWord.Length - 1)
{
sDoubleWord = sSingleWord + " " + NormalizeWord(arrSingleWord[i + 1]);
if (DictionaryToWork.DoubleWordDic.Contains(sDoubleWord) && !arrDoubleWordFail.Contains(sDoubleWord))
{
arrDoubleWordFail.Add(sDoubleWord);
}
}
else
{
sDoubleWord = string.Empty;
}
if (i < arrSingleWord.Length - 2)
{
sTripleWord = sDoubleWord + " " + NormalizeWord(arrSingleWord[i + 2]);
if (DictionaryToWork.TripleWordDic.Contains(sTripleWord) && !arrTripleWordFail.Contains(sTripleWord))
{
arrTripleWordFail.Add(sTripleWord);
}
}
else
{
sTripleWord = string.Empty;
}
}
foreach (string s in arrTripleWordFail)
{
pSourceText = Regex.Replace(pSourceText, Regex.Escape(s), Regex.Escape(WrapStrongWord(s)), RegexOptions.IgnoreCase);
// sResult = sResult.Replace(s, MakeHTMLStrong(s));
}
foreach (string s in arrDoubleWordFail)
{
pSourceText = Regex.Replace(pSourceText, Regex.Escape(s), Regex.Escape(WrapStrongWord(s)), RegexOptions.IgnoreCase);
//sResult = sResult.Replace(s, MakeHTMLStrong(s));
}
foreach (string s in arrSingleWordFail)
{
pSourceText = Regex.Replace(pSourceText, Regex.Escape(s), Regex.Escape(WrapStrongWord(s)), RegexOptions.IgnoreCase);
//sResult = sResult.Replace(s, MakeHTMLStrong(s));
}
foreach (string s in ArrayDoubting)
{
pSourceText = Regex.Replace(pSourceText, Regex.Escape(s), Regex.Escape(WrapColorWord(s, Color.BlueViolet)), RegexOptions.IgnoreCase);
//sResult = sResult.Replace(s, MakeHTMLColor(s, Color.BlueViolet));
}
return new CheckingResult {
OutputTextWithMark = pSourceText
};
}
/// <summary>
/// to lower, strip html tag, remove mark (in ArrayToTrim dic) from string s
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private string NormalizeWord(string s)
{
// remove tag
s = Lib.HtmlRemoval.StripTagsCharArray(s.ToLower());
// remove mark, space, ... in ignoremark
foreach (string sMark in ArrayToTrim)
{
s = s.Replace(sMark, "");
}
return s;
}
private string WrapColorWord(string s, Color c)
{
if (string.IsNullOrEmpty(s))
{
return s;
}
return "<span style='color:" + c.Name + ";'>" + s + "</span>";
}
private string WrapStrongWord(string s)
{
if (string.IsNullOrEmpty(s))
{
return s;
}
return "<strong>" + s + "</strong>";
}
}
}