-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextUtility.cs
69 lines (59 loc) · 1.6 KB
/
TextUtility.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
using System.Globalization;
using System.Text.RegularExpressions;
namespace OMinimoScrapper;
public static class TextUtility
{
// https://www.dotnetperls.com/levenshtein
public static int GetEditDistance(string s, string t)
{
int n = s.Length;
int m = t.Length;
int[,] d = new int[n + 1, m + 1];
// Verify arguments.
if (n == 0)
{
return m;
}
if (m == 0)
{
return n;
}
// Initialize arrays.
for (int i = 0; i <= n; d[i, 0] = i++)
{
}
for (int j = 0; j <= m; d[0, j] = j++)
{
}
// Begin looping.
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
// Compute cost.
int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost);
}
}
// Return cost.
return d[n, m];
}
public static DateTime DateFromString(string str)
{
var regexp = new Regex(@"Em (\d+) de (\w+) de (\d{4})");
var result = regexp.Replace(str, "$1/$2/$3");
var dtfi = CultureInfo.GetCultureInfo("pt-BR").DateTimeFormat;
return DateTime.Parse(result, dtfi);
}
public static string CapitalizeFirst(string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
var chars = str.ToCharArray();
chars[0] = char.ToUpper(chars[0]);
return new string(chars);
}
}