-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractionNormalizer.cs
More file actions
169 lines (162 loc) · 6.98 KB
/
ContractionNormalizer.cs
File metadata and controls
169 lines (162 loc) · 6.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
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace InjectDetect
{
public static class ContractionNormalizer
{
// Expand: contraction -> full form
private static readonly Dictionary<string, string> ExpandMap = new(System.StringComparer.OrdinalIgnoreCase)
{
// negations
{ "don't", "do not" },
{ "dont", "do not" },
{ "doesn't", "does not" },
{ "doesnt", "does not" },
{ "didn't", "did not" },
{ "didnt", "did not" },
{ "won't", "will not" },
{ "wont", "will not" },
{ "wouldn't", "would not" },
{ "wouldnt", "would not" },
{ "shouldn't", "should not" },
{ "shouldnt", "should not" },
{ "couldn't", "could not" },
{ "couldnt", "could not" },
{ "can't", "cannot" },
{ "cant", "cannot" },
{ "isn't", "is not" },
{ "isnt", "is not" },
{ "aren't", "are not" },
{ "arent", "are not" },
{ "wasn't", "was not" },
{ "wasnt", "was not" },
{ "weren't", "were not" },
{ "werent", "were not" },
{ "hasn't", "has not" },
{ "hasnt", "has not" },
{ "haven't", "have not" },
{ "havent", "have not" },
{ "hadn't", "had not" },
{ "hadnt", "had not" },
{ "mustn't", "must not" },
{ "mustnt", "must not" },
{ "needn't", "need not" },
{ "neednt", "need not" },
{ "daren't", "dare not" },
{ "darent", "dare not" },
// be / have / will
{ "i'm", "i am" },
{ "im", "i am" },
{ "you're", "you are" },
{ "youre", "you are" },
{ "he's", "he is" },
{ "hes", "he is" },
{ "she's", "she is" },
{ "shes", "she is" },
{ "it's", "it is" },
{ "its", "it is" },
{ "we're", "we are" },
{ "were", "we are" },
{ "they're", "they are" },
{ "theyre", "they are" },
{ "i've", "i have" },
{ "ive", "i have" },
{ "you've", "you have" },
{ "youve", "you have" },
{ "we've", "we have" },
{ "weve", "we have" },
{ "they've", "they have" },
{ "theyve", "they have" },
{ "i'll", "i will" },
{ "ill", "i will" },
{ "you'll", "you will" },
{ "youll", "you will" },
{ "he'll", "he will" },
{ "hell", "he will" },
{ "she'll", "she will" },
{ "shell", "she will" },
{ "we'll", "we will" },
{ "well", "we will" },
{ "they'll", "they will" },
{ "theyll", "they will" },
{ "i'd", "i would" },
{ "id", "i would" },
{ "you'd", "you would" },
{ "youd", "you would" },
{ "he'd", "he would" },
{ "hed", "he would" },
{ "she'd", "she would" },
{ "shed", "she would" },
{ "we'd", "we would" },
{ "wed", "we would" },
{ "they'd", "they would" },
{ "theyd", "they would" },
// misc
{ "let's", "let us" },
{ "lets", "let us" },
{ "that's", "that is" },
{ "thats", "that is" },
{ "there's", "there is" },
{ "theres", "there is" },
{ "here's", "here is" },
{ "heres", "here is" },
{ "what's", "what is" },
{ "whats", "what is" },
{ "who's", "who is" },
{ "whos", "who is" },
{ "how's", "how is" },
{ "hows", "how is" },
{ "where's", "where is" },
{ "wheres", "where is" },
{ "when's", "when is" },
{ "whens", "when is" },
{ "why's", "why is" },
{ "whys", "why is" },
{ "could've", "could have" },
{ "couldve", "could have" },
{ "should've", "should have" },
{ "shouldve", "should have" },
{ "would've", "would have" },
{ "wouldve", "would have" },
{ "might've", "might have" },
{ "mightve", "might have" },
{ "must've", "must have" },
{ "mustve", "must have" },
{ "ain't", "am not" },
{ "aint", "am not" },
};
// Contract: full form -> contraction (reverse map, longest phrase first)
private static readonly List<(string Full, string Contracted)> ContractList;
static ContractionNormalizer()
{
// Build contract list from expand map, deduplicated, sorted longest first
var seen = new HashSet<string>(System.StringComparer.OrdinalIgnoreCase);
ContractList = new List<(string, string)>();
foreach (var kv in ExpandMap)
{
if (seen.Add(kv.Value))
ContractList.Add((kv.Value, kv.Key));
}
ContractList.Sort((a, b) => b.Full.Length.CompareTo(a.Full.Length));
}
public static string Expand(string input)
{
return Regex.Replace(input, @"[\w']+", m =>
ExpandMap.TryGetValue(m.Value, out string? expanded) ? expanded : m.Value);
}
public static string Contract(string input)
{
string result = input;
foreach (var (full, contracted) in ContractList)
{
result = Regex.Replace(
result,
@"\b" + Regex.Escape(full) + @"\b",
contracted,
RegexOptions.IgnoreCase
);
}
return result;
}
}
}