-
Notifications
You must be signed in to change notification settings - Fork 204
/
ToleratingTyposSamplePage.cs
365 lines (311 loc) · 11.6 KB
/
ToleratingTyposSamplePage.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using Android.Util;
using System;
using Android.Views;
using SampleBrowser;
using Android.Widget;
using Com.Syncfusion.Autocomplete;
using Android.Graphics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SampleBrowser
{
public class ToleratingTyposSamplePage : SamplePage
{
LinearLayout mainLayout;
int width,height;
double density;
ListView myList;
UserData userData;
MyCustomListAdapter myCustomListAdapter;
public List<User> ListSource { get; set; }
public override View GetPropertyWindowLayout(Android.Content.Context context)
{
return null;
}
public override View GetSampleContent(Android.Content.Context con)
{
userData = new UserData();
width = con.Resources.DisplayMetrics.WidthPixels;
height = con.Resources.DisplayMetrics.HeightPixels;
density = con.Resources.DisplayMetrics.Density;
mainLayout = new LinearLayout(con);
mainLayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
mainLayout.Orientation = Orientation.Vertical;
mainLayout.SetGravity(GravityFlags.Center);
mainLayout.SetPadding(0,20,0,0);
SearchCountryMethod(con);
AutoCompleteMethod(con);
SearchLabelMethod(con);
ListViewMethod(con);
return mainLayout;
}
private void SearchCountryMethod(Android.Content.Context con)
{
TextView textView = new TextView(con);
textView.LayoutParameters = new LinearLayout.LayoutParams((int)(width * 0.95), (int)(30 * density));
textView.Hint = "Search by Country";
textView.TextSize = 18;
mainLayout.AddView(textView);
}
private void AutoCompleteMethod(Android.Content.Context con)
{
SfAutoComplete toleratingAutoComplete = new SfAutoComplete(con);
toleratingAutoComplete.LayoutParameters = new LinearLayout.LayoutParams((int)(width * 0.95), (int)(50));
toleratingAutoComplete.SetGravity(GravityFlags.Start);
toleratingAutoComplete.DataSource =new Countrylist().Country;
toleratingAutoComplete.Filter = AutoCompleteSearch;
toleratingAutoComplete.SuggestionMode = SuggestionMode.Custom;
toleratingAutoComplete.DropDownItemHeight = 40;
toleratingAutoComplete.MaximumDropDownHeight = 150;
toleratingAutoComplete.Watermark = "Search Here";
CustomAutoCompleteAdapter customAdapter = new CustomAutoCompleteAdapter();
customAdapter.autoComplete1 = toleratingAutoComplete;
toleratingAutoComplete.Adapter = customAdapter;
ListSource =userData.Users;
myCustomListAdapter = new MyCustomListAdapter(ListSource);
toleratingAutoComplete.SelectionChanged += (sender, e) => {
int valueCount = 0;
if (e.Value == null || e.Value.ToString() == "")
valueCount = 0;
else
valueCount=100000;
foreach (var item in ListSource)
{
item.Count = random.Next(valueCount).ToString();
}
myCustomListAdapter.NotifyDataSetChanged();
};
mainLayout.AddView(toleratingAutoComplete);
}
Random random = new Random();
ToleratingTyposHelper helper = new ToleratingTyposHelper();
public bool AutoCompleteSearch(object value1, object value2)
{
var string1 = value1.ToString().ToLower();
var string2 = value2.ToString().ToLower();
if (string1.Length > 0 && string2.Length > 0)
if (string1[0] != string2[0])
return false;
var originalString1 = string.Empty;
var originalString2 = string.Empty;
if (string1.Length < string2.Length)
{
originalString2 = string2.Remove(string1.Length);
originalString1 = string1;
}
if (string2.Length < string1.Length)
{
return false;
}
if (string2.Length == string1.Length)
{
originalString1 = string1;
originalString2 = string2;
}
bool IsMatchSoundex = helper.ProcessOnSoundexAlgorithmn(originalString1) == helper.ProcessOnSoundexAlgorithmn(originalString2);
int Distance = helper.GetDamerauLevenshteinDistance(originalString1, originalString2);
if (IsMatchSoundex || Distance <= 4)
return true;
else
return false;
//int matchValue = 0;
//var allWords = value2.ToString().ToLower().Split(' ');
//var keys = value1.ToString().ToLower().Split(' ');
//foreach (var item in allWords)
//{
// foreach (var key in keys)
// {
// var itemValue = item;
// if (item.Length > key.Length)
// {
// itemValue = item.Remove(key.Length);
// }
// if (key == "" || item == "")
// continue;
// if ((helper.ProcessOnSoundexAlgorithmn(key) == helper.ProcessOnSoundexAlgorithmn(itemValue)))
// matchValue++;
// if ((helper.ProcessOnSoundexAlgorithmn(key) == helper.ProcessOnSoundexAlgorithmn(item)))
// matchValue++;
// }
//}
//int keysCount = 0;
//if (matchValue >= keysCount)
// return true;
//return false;
}
private void SearchLabelMethod(Android.Content.Context con)
{
TextView textViewHeight = new TextView(con);
textViewHeight.LayoutParameters = new LinearLayout.LayoutParams((int)(width * 0.95), (int)(40 * density));
mainLayout.AddView(textViewHeight);
TextView textView = new TextView(con);
textView.LayoutParameters = new LinearLayout.LayoutParams((int)(width * 0.95), (int)(30 * density));
textView.Hint = "Search Results";
textView.TextSize = 15;
mainLayout.AddView(textView);
}
private void ListViewMethod(Android.Content.Context con)
{
myList = new ListView(con);
myList.LayoutParameters=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, (int)(height - (300 * density)));
myList.FastScrollEnabled = true;
myList.Adapter = myCustomListAdapter;
LinearLayout listViewLayout = new LinearLayout(con);
listViewLayout.SetPadding(20,10,20,10);
listViewLayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
listViewLayout.AddView(myList);
mainLayout.AddView(listViewLayout);
}
}
public class ToleratingTyposHelper
{
public ToleratingTyposHelper()
{
soundexTerms.Add("aeiouhyw");
soundexTerms.Add("bfpv");
soundexTerms.Add("cgikqsxz");
soundexTerms.Add("dt");
soundexTerms.Add("l");
soundexTerms.Add("mn");
soundexTerms.Add("r");
}
List<string> soundexTerms = new List<string>();
/// <summary>
/// Based on Soundex Algorithmn and DL Distance Algorithmn
/// </summary>
/// <returns>The matching.</returns>
/// <param name="value1">Value1.</param>
/// <param name="value2">Value2.</param>
public int IsMatching(string value1, string value2)
{
var val1 = ProcessOnSoundexAlgorithmn(value1);
var val2 = ProcessOnSoundexAlgorithmn(value2);
return CalcualteDistance(val1, val2);
}
public int GetMinValue(int[] value)
{
int minValue = 0;
foreach (var item in value)
{
if (item < minValue)
minValue = item;
}
return minValue;
}
public int GetDamerauLevenshteinDistance(string source, string target)
{
var bounds = new { Height = source.Length + 1, Width = target.Length + 1 };
int[,] matrix = new int[bounds.Height, bounds.Width];
for (int height = 0; height < bounds.Height; height++) { matrix[height, 0] = height; };
for (int width = 0; width < bounds.Width; width++) { matrix[0, width] = width; };
for (int height = 1; height < bounds.Height; height++)
{
for (int width = 1; width < bounds.Width; width++)
{
int cost = (source[height - 1] == target[width - 1]) ? 0 : 1;
int insertion = matrix[height, width - 1] + 1;
int deletion = matrix[height - 1, width] + 1;
int substitution = matrix[height - 1, width - 1] + cost;
int distance = Math.Min(insertion, Math.Min(deletion, substitution));
if (height > 1 && width > 1 && source[height - 1] == target[width - 2] && source[height - 2] == target[width - 1])
{
distance = Math.Min(distance, matrix[height - 2, width - 2] + cost);
}
matrix[height, width] = distance;
}
}
return matrix[bounds.Height - 1, bounds.Width - 1];
}
/// <summary>
/// DL Algorithmn Implementation
/// </summary>
/// <returns>The distance.</returns>
/// <param name="value1">Value1.</param>
/// <param name="value2">Value2.</param>
public int CalcualteDistance(string value1, string value2)
{
int lengthValue1 = value1.Length;
int lengthValue2 = value2.Length;
var matrix = new int[lengthValue1 + 1, lengthValue2 + 1];
for (int i = 0; i <= lengthValue1; i++)
matrix[i, 0] = i;
for (int j = 0; j <= lengthValue2; j++)
matrix[0, j] = j;
for (int i = 1; i <= lengthValue1; i++)
{
for (int j = 1; j <= lengthValue2; j++)
{
int cost = value2[j - 1] == value1[i - 1] ? 0 : 1;
var vals = new int[] {
matrix[i - 1, j] + 1,
matrix[i, j - 1] + 1,
matrix[i - 1, j - 1] + cost
};
matrix[i, j] = GetMinValue(vals);
if (i > 1 && j > 1 && value1[i - 1] == value2[j - 2] && value1[i - 2] == value2[j - 1])
matrix[i, j] = Math.Min(matrix[i, j], matrix[i - 2, j - 2] + cost);
}
}
return matrix[lengthValue1, lengthValue2];
}
/// <summary>
/// Soundex Algorithmn Implementation
/// </summary>
/// <returns>The on soundex algorithmn.</returns>
/// <param name="value1">Value1.</param>
/// <param name="moreAccuracy">If set to <c>true</c> more accuracy.</param>
public string ProcessOnSoundexAlgorithmn(string value1, bool moreAccuracy = true)
{
string stringValue = string.Empty;
foreach (var item in value1.ToLower())
{
for (int i = 0; i < soundexTerms.Count; i++)
{
if (soundexTerms[i].Contains(item.ToString()))
{
stringValue += i.ToString();
continue;
}
}
}
if (stringValue.Length > 0)
{
if (moreAccuracy)
{
stringValue = stringValue.Insert(0, value1[0].ToString());
stringValue = stringValue.Replace("0", "");
}
}
return stringValue;
}
}
internal class CustomAutoCompleteAdapter : AutoCompleteAdapter
{
internal SfAutoComplete autoComplete1;
public Android.Views.View GetView(Com.Syncfusion.Autocomplete.SfAutoComplete autoComplete, string text, int index)
{
GC.Collect();
LinearLayout linearLayout = new LinearLayout(autoComplete.Context);
linearLayout.SetPadding((int)(3 * autoComplete.GetDensity()),(int)(3 * autoComplete.GetDensity()),(int)(3 * autoComplete.GetDensity()),(int)(3 * autoComplete.GetDensity()));
linearLayout.LayoutParameters = new ViewGroup.LayoutParams(autoComplete.LayoutParameters.Width, autoComplete.LayoutParameters.Height);
linearLayout.Orientation = Orientation.Horizontal;
TextView imageView = new TextView(autoComplete.Context);
Typeface tf = Typeface.CreateFromAsset(autoComplete.Context.Assets, "icon.ttf");
imageView.Text = "A";
imageView.SetPadding((int)(3 * autoComplete.GetDensity()), (int)(3 * autoComplete.GetDensity()), (int)(3 * autoComplete.GetDensity()), 0);
imageView.Typeface = tf;
imageView.TextSize = 18;
imageView.Gravity = GravityFlags.Center;
imageView.TextAlignment = TextAlignment.Center;
TextView textView = new TextView(autoComplete.Context);
textView.SetPadding((int)(3 * autoComplete.GetDensity()),0,0,0);
textView.Text = text;
textView.TextSize = 18;
textView.Gravity = GravityFlags.Center;
textView.TextAlignment = TextAlignment.Center;
linearLayout.AddView(imageView);
linearLayout.AddView(textView);
return linearLayout;
}
}
}