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
|
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class Program
{
#region Fields
private const char CrossSymbol = '+';
private const char HorizontalSeparatorSymbol = '-';
private const int IterationCount = 200000;
private const string MethodHeader = "Method";
private const string PerformanceFormat = "#,##0.00";
private const string PerformanceHeader = "Perf";
private const string RemoveDiacriticsCharReplaceMethodName = "RemoveDiacriticsCharReplace";
private const string RemoveDiacriticsNormalizationDotNet2MethodName = "RemoveDiacriticsNormalizationDotNet2";
private const string RemoveDiacriticsRegexMethodName = "RemoveDiacriticsRegex";
private const string RemoveDiacriticsStringReplaceMethodName = "RemoveDiacriticsStringReplace";
private const string RemoveDiacriticsTrickMethodName = "RemoveDiacriticsTrick";
private const char SpaceSymbol = ' ';
private const string TestString = "à la claîre fontaîne l'élève à fait de grâve erreur où pas ?";
private const string TimeHeader = "Time";
private const char VerticalSeparatorSymbol = '|';
#endregion Fields
#region Properties
private static Dictionary<string, string> Chars
{
get;
set;
}
private static Regex Regex
{
get;
set;
}
#endregion Properties
#region Methods
public static void Main(string[] args)
{
Init();
var oMethodsToTest = new List<Pair<Func<string, string>, string>>();
oMethodsToTest.Add(new Pair<Func<string, string>, string>(RemoveDiacriticsRegex, RemoveDiacriticsRegexMethodName));
oMethodsToTest.Add(new Pair<Func<string, string>, string>(RemoveDiacriticsNormalizationDotNet2, RemoveDiacriticsNormalizationDotNet2MethodName));
oMethodsToTest.Add(new Pair<Func<string, string>, string>(RemoveDiacriticsStringReplace, RemoveDiacriticsStringReplaceMethodName));
oMethodsToTest.Add(new Pair<Func<string, string>, string>(RemoveDiacriticsCharReplace, RemoveDiacriticsCharReplaceMethodName));
oMethodsToTest.Add(new Pair<Func<string, string>, string>(RemoveDiacriticsTrick, RemoveDiacriticsTrickMethodName));
var oResults = oMethodsToTest
.Select(x => new Pair<TimeSpan, string>(TestMethod(x.Fst), x.Snd))
.OrderBy(x => x.Fst)
.ToArray();
var oBest = oResults.First().Fst;
var sTable = new string[oResults.Length + 1][];
for (var i = 0; i < sTable.Length; i++)
{
sTable[i] = new string[3];
}
sTable[0][0] = MethodHeader;
sTable[0][1] = TimeHeader;
sTable[0][2] = PerformanceHeader;
for (var i = 0; i < oResults.Length; i++)
{
sTable[i + 1][0] = oResults[i].Snd;
sTable[i + 1][1] = oResults[i].Fst.ToString();
var fGap = (double)oResults[i].Fst.Ticks / (double)oBest.Ticks;
sTable[i + 1][2] = fGap.ToString(PerformanceFormat);
}
WriteTable(sTable);
System.Console.ReadLine();
}
private static void Init()
{
Chars = new Dictionary<string, string>();
Chars["c"] = "c";
Chars["à"] = "a";
Chars["á"] = "a";
Chars["ä"] = "a";
Chars["â"] = "a";
Chars["ã"] = "a";
Chars["å"] = "a";
Chars["é"] = "e";
Chars["è"] = "e";
Chars["ê"] = "e";
Chars["ë"] = "e";
Chars["ì"] = "i";
Chars["í"] = "i";
Chars["ï"] = "i";
Chars["î"] = "i";
Chars["ò"] = "o";
Chars["ó"] = "o";
Chars["ô"] = "o";
Chars["ö"] = "o";
Chars["û"] = "u";
Chars["ü"] = "u";
Chars["ù"] = "u";
Chars["ú"] = "u";
Chars["ý"] = "y";
Chars["ÿ"] = "y";
Chars["ç"] = "c";
Chars["ñ"] = "n";
var sPattern = string.Join(VerticalSeparatorSymbol.ToString(), Chars.Keys.Select(x => x.ToString()).ToArray());
Regex = new System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled);
}
private static string RemoveDiacriticsCharReplace(string inputString)
{
string result;
StringBuilder builder = new StringBuilder();
char[] car = inputString.ToCharArray();
foreach (char c in car)
{
if (c == 'ä' || c == 'á' || c == 'à' || c == 'â' || c == 'ã' || c == 'å')
builder.Append('a');
else if (c == 'é' || c == 'è' || c == 'ê' || c == 'ë')
builder.Append('e');
else if (c == 'ì' || c == 'í' || c == 'ï' || c == 'î')
builder.Append('i');
else if (c == 'ò' || c == 'ó' || c == 'ô' || c == 'ö')
builder.Append('o');
else if (c == 'ù' || c == 'ú' || c == 'ü' || c == 'û')
builder.Append('u');
else if (c == 'ÿ' || c == 'ý')
builder.Append('y');
else if (c == 'ç')
builder.Append('c');
else if (c == 'ñ')
builder.Append('n');
else builder.Append(c);
}
result = builder.ToString();
return result;
}
private static string RemoveDiacriticsNormalizationDotNet2(string inputString)
{
string result = inputString;
string normalizedString = result.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < normalizedString.Length; i++)
{
char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
result = stringBuilder.ToString();
return result;
}
private static string RemoveDiacriticsRegex(string inputString)
{
return Regex.Replace(inputString, ReplaceMatch);
}
private static string RemoveDiacriticsStringReplace(string inputString)
{
string result = inputString;
result = result.Replace('à', 'a');
result = result.Replace('á', 'a');
result = result.Replace('ä', 'a');
result = result.Replace('â', 'a');
result = result.Replace('ã', 'a');
result = result.Replace('å', 'a');
result = result.Replace('é', 'e');
result = result.Replace('è', 'e');
result = result.Replace('ê', 'e');
result = result.Replace('ë', 'e');
result = result.Replace('ì', 'i');
result = result.Replace('í', 'i');
result = result.Replace('ï', 'i');
result = result.Replace('î', 'i');
result = result.Replace('ò', 'o');
result = result.Replace('ó', 'o');
result = result.Replace('ô', 'o');
result = result.Replace('ö', 'o');
result = result.Replace('û', 'u');
result = result.Replace('ü', 'u');
result = result.Replace('ù', 'u');
result = result.Replace('ú', 'u');
result = result.Replace('ý', 'y');
result = result.Replace('ÿ', 'y');
result = result.Replace('ç', 'c');
result = result.Replace('ñ', 'n');
return result;
}
private static string RemoveDiacriticsTrick(string inputString)
{
byte[] tab = System.Text.Encoding.GetEncoding(1251).GetBytes(inputString);
string result = System.Text.Encoding.ASCII.GetString(tab);
return result;
}
private static string ReplaceMatch(Match match)
{
return Chars[match.Value];
}
private static TimeSpan TestMethod(Func<string, string> method)
{
var oStart = DateTime.Now;
var sStr = TestString;
for (var i = 0; i < IterationCount; i++)
{
method(sStr);
}
var oStop = DateTime.Now;
return oStop - oStart;
}
private static void WriteTable(string[][] values)
{
var iRows = values.Length;
var iCols = values[0].Length;
var iColumnsWidth = new int[iCols];
for (var j = 0; j < iCols; j++)
{
iColumnsWidth[j] = 0;
}
for (var j = 0; j < iCols; j++)
{
for (var i = 0; i < iRows; i++)
{
iColumnsWidth[j] = Math.Max(iColumnsWidth[j], values[i][j].Length);
}
}
for (var i = 0; i < iRows; i++)
{
if (i == 1)
{
for (var j = 0; j < iCols; j++)
{
if (j >= 1)
{
System.Console.Write(CrossSymbol);
}
System.Console.Write(new string(HorizontalSeparatorSymbol, iColumnsWidth[j] + 2));
}
System.Console.WriteLine();
}
for (var j = 0; j < iCols; j++)
{
if (j >= 1)
{
System.Console.Write(VerticalSeparatorSymbol);
}
System.Console.Write(SpaceSymbol);
System.Console.Write(values[i][j]);
System.Console.Write(new string(' ', iColumnsWidth[j] - values[i][j].Length + 1));
}
System.Console.WriteLine();
}
}
#endregion Methods
}
} |