-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefault.aspx.cs
More file actions
192 lines (186 loc) · 7.04 KB
/
Default.aspx.cs
File metadata and controls
192 lines (186 loc) · 7.04 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Syncfusion.SpellChecker.Base;
namespace WebApplication2
{
public partial class _Default : Page
{
internal static SpellCheckerBase baseDictionary, customDictionary;
private readonly static string _customFilePath = HttpContext.Current.Server.MapPath("~/App_Data/Custom.dic"); // Here we need to specify the corresponding file name
private readonly static List<Status> errorWords = new List<Status>();
protected void Page_Load(object sender, EventArgs e)
{
if (baseDictionary == null)
{
string filePath = HttpContext.Current.Server.MapPath("~/App_Data/Default.dic");
// Here we need to specify the corresponding file name
Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
baseDictionary = new SpellCheckerBase(stream);
}
CustomFileRead();
}
[WebMethod]
public static object AddToDictionary(object data)
{
var serializer = new JavaScriptSerializer();
var args = (Actions)serializer.Deserialize(data.ToString(), typeof(Actions));
if (args.CustomWord != null)
{
AddToCustomDictionary(args.CustomWord);
}
return args.CustomWord;
}
[WebMethod]
public static object CheckWords(object data)
{
var serializer = new JavaScriptSerializer();
Actions args = (Actions)serializer.Deserialize(data.ToString(), typeof(Actions));
if (args.RequestType == "checkWords")
{
baseDictionary.IgnoreAlphaNumericWords = args.Model.IgnoreAlphaNumericWords;
baseDictionary.IgnoreEmailAddress = args.Model.IgnoreEmailAddress;
baseDictionary.IgnoreMixedCaseWords = args.Model.IgnoreMixedCaseWords;
baseDictionary.IgnoreUpperCaseWords = args.Model.IgnoreUpperCase;
baseDictionary.IgnoreUrl = args.Model.IgnoreUrl;
baseDictionary.IgnoreFileNames = args.Model.IgnoreFileNames;
var errorWordsCollection = SplitWords(args.Text);
return errorWordsCollection;
}
else if (args.RequestType == "getSuggestions")
{
return baseDictionary.GetSuggestions(args.ErrorWord);
}
return "";
}
private static void AddToCustomDictionary(string word)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(_customFilePath, true))
{
file.Write(word + Environment.NewLine);
}
CustomFileRead();
}
private static List<Status> SplitWords(string text)
{
var words = text.Split(null);
foreach (var word in words)
{
string textWord;
Uri uriResult;
bool checkUrl = Uri.TryCreate(word, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (checkUrl)
{
textWord = word;
if (CheckWord(textWord))
{
GetStatus(textWord);
}
}
else if (Regex.IsMatch(word,
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase))
{
textWord = word;
if (CheckWord(textWord))
{
GetStatus(textWord);
}
}
else if (Regex.IsMatch(word, @"[a-zA-Z0-9_$\-\.\\]*\\[a-zA-Z0-9_$\-\.\\]+"))
{
textWord = word;
if (CheckWord(textWord))
{
GetStatus(textWord);
}
}
else
{
if (word.EndsWith(".") || word.EndsWith(","))
{
textWord = word.Remove(word.Length - 1);
}
else if (word.Contains('\t') || word.Contains('\n') || word.Contains('\r'))
{
textWord = Regex.Replace(word, @"\t|\n|\r", "");
}
else
{
textWord = word;
}
GetStatus(textWord);
}
}
return errorWords;
}
private static void CustomFileRead()
{
Stream stream1 = new FileStream(_customFilePath, FileMode.Open, FileAccess.ReadWrite);
customDictionary = new SpellCheckerBase(stream1);
stream1.Dispose();
}
private static bool CheckWord(string word)
{
var flag = false;
if (baseDictionary.HasError(word))
{
flag = true;
using (StreamReader sr = new StreamReader(_customFilePath))
{
var contents = sr.ReadToEnd();
if (contents != "")
{
flag = customDictionary.HasError(word) ? true : false;
}
}
}
return flag;
}
private static List<Status> GetStatus(string textWord)
{
var splitWords = Regex.Replace(textWord, @"[^0-9a-zA-Z\'_]", " ").Split(null);
foreach (var inputWord in splitWords)
{
if (CheckWord(inputWord))
{
errorWords.Add(new Status
{
ErrorWord = inputWord
});
}
}
return errorWords;
}
}
public class Status
{
public string ErrorWord { get; set; }
}
public class Actions
{
public string Text { get; set; }
public string CustomWord { get; set; }
public SpellModel Model { get; set; }
public string RequestType { get; set; }
public string ErrorWord { get; set; }
}
public class SpellModel
{
public Boolean IgnoreAlphaNumericWords { get; set; }
public Boolean IgnoreEmailAddress { get; set; }
public Boolean IgnoreMixedCaseWords { get; set; }
public Boolean IgnoreUpperCase { get; set; }
public Boolean IgnoreUrl { get; set; }
public Boolean IgnoreFileNames { get; set; }
}
}