-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLocalizedInflectionEngineTests.Fixtures.cs
More file actions
244 lines (228 loc) · 8.41 KB
/
Copy pathLocalizedInflectionEngineTests.Fixtures.cs
File metadata and controls
244 lines (228 loc) · 8.41 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
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
using System.Diagnostics.CodeAnalysis;
public partial class LocalizedInflectionEngineTests
{
[Fact]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026",
Justification = "This test intentionally probes for removed internal fixture types by their historical assembly-qualified names.")]
public void RuntimeAssemblyDoesNotShipFixtureOnlyLexemeBuilders()
{
var assembly = typeof(InflectionBundle).Assembly;
var bundleMethods = typeof(InflectionBundle).GetMethods(
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic);
Assert.Null(assembly.GetType("Humanizer.InflectionDisplayForm"));
Assert.Null(assembly.GetType("Humanizer.InflectionLexeme"));
Assert.DoesNotContain(bundleMethods, method => method.Name == "BuildData");
}
readonly struct InflectionDisplayForm(
CardinalPluralCategory category,
string preferred,
string[] accepted)
{
public CardinalPluralCategory Category { get; } = category;
public string Preferred { get; } = preferred;
public string[] Accepted { get; } = accepted;
}
sealed class InflectionLexeme(
string id,
string singular,
string dictionaryPlural,
string[] acceptedSingular,
string[] acceptedDictionaryPlural,
InflectionDisplayForm[] display,
InflectionCountability countability = InflectionCountability.Count)
{
public string Id { get; } = id;
public string Singular { get; } = singular;
public string DictionaryPlural { get; } = dictionaryPlural;
public string[] AcceptedSingular { get; } = acceptedSingular;
public string[] AcceptedDictionaryPlural { get; } = acceptedDictionaryPlural;
public InflectionDisplayForm[] Display { get; } = display;
public InflectionCountability Countability { get; } = countability;
}
static InflectionBundle CreateBundle(
string owner,
CardinalPluralRuleKind cardinalRule,
InflectionCasing casing,
string[] scripts,
string[] skipSimpleWords,
InflectionLexeme[] lexemes,
InflectionRule[] rules) =>
CreateBundle(
owner,
cardinalRule,
InflectionCapability.DisplayByCategory,
InflectionQuantitySelector.None,
casing,
scripts,
skipSimpleWords,
lexemes,
rules);
static InflectionBundle CreateBundle(
string owner,
CardinalPluralRuleKind cardinalRule,
InflectionCapability capability,
InflectionCasing casing,
string[] scripts,
string[] skipSimpleWords,
InflectionLexeme[] lexemes,
InflectionRule[] rules) =>
CreateBundle(
owner,
cardinalRule,
capability,
InflectionQuantitySelector.None,
casing,
scripts,
skipSimpleWords,
lexemes,
rules);
static InflectionBundle CreateBundle(
string owner,
CardinalPluralRuleKind cardinalRule,
InflectionCapability capability,
InflectionQuantitySelector quantitySelector,
InflectionCasing casing,
string[] scripts,
string[] skipSimpleWords,
InflectionLexeme[] lexemes,
InflectionRule[] rules)
{
var (compactLexemes, entries, candidates) = BuildFixtureData(lexemes, casing);
if (entries.Length > ushort.MaxValue)
{
var indexes = Enumerable.Range(0, entries.Length).ToArray();
return new(
owner,
cardinalRule,
capability,
quantitySelector,
casing,
scripts,
skipSimpleWords,
compactLexemes,
entries,
candidates,
indexes,
indexes,
rules);
}
var compactIndexes = Enumerable.Range(0, entries.Length)
.Select(static index => (ushort)index)
.ToArray();
return new(
owner,
cardinalRule,
capability,
quantitySelector,
casing,
scripts,
skipSimpleWords,
compactLexemes,
entries,
candidates,
compactIndexes,
compactIndexes,
rules);
}
static (
InflectionLexemeRecord[] Lexemes,
InflectionLexemeEntry[] Entries,
InflectionLexemeCandidate[] Candidates) BuildFixtureData(
InflectionLexeme[] lexemes,
InflectionCasing casing)
{
var comparer = casing == InflectionCasing.LowerTitleUpper
? InflectionUnicodeData.SimpleCaseComparer.Instance
: StringComparer.Ordinal;
var candidatesBySurface =
new Dictionary<string, Dictionary<int, InflectionExactRole>>(comparer);
for (var index = 0; index < lexemes.Length; index++)
{
var lexeme = lexemes[index];
AddFixtureCandidates(
candidatesBySurface,
lexeme.AcceptedSingular,
index,
InflectionExactRole.Singular);
AddFixtureCandidates(
candidatesBySurface,
lexeme.AcceptedDictionaryPlural,
index,
InflectionExactRole.DictionaryPlural);
foreach (var display in lexeme.Display)
{
AddFixtureCandidates(
candidatesBySurface,
display.Accepted,
index,
GetFixtureDisplayRole(display.Category));
}
}
var entryIndexBySurface = new Dictionary<string, int>(comparer);
var entries = new List<InflectionLexemeEntry>(candidatesBySurface.Count);
var candidates = new List<InflectionLexemeCandidate>();
foreach (var pair in candidatesBySurface.OrderBy(
static pair => pair.Key,
comparer))
{
entryIndexBySurface.Add(pair.Key, entries.Count);
var candidateOffset = candidates.Count;
foreach (var candidate in pair.Value.OrderBy(
static candidate => candidate.Key))
{
candidates.Add(new(candidate.Key, candidate.Value));
}
entries.Add(new(
pair.Key,
candidateOffset,
candidates.Count - candidateOffset));
}
var compactLexemes = new InflectionLexemeRecord[lexemes.Length];
for (var index = 0; index < lexemes.Length; index++)
{
var lexeme = lexemes[index];
compactLexemes[index] = new(
lexeme.Id,
entryIndexBySurface[lexeme.Singular],
entryIndexBySurface[lexeme.DictionaryPlural],
lexeme.Display
.Select(display => new InflectionLexemeDisplay(
display.Category,
entryIndexBySurface[display.Preferred]))
.ToArray(),
lexeme.Countability);
}
return (compactLexemes, entries.ToArray(), candidates.ToArray());
}
static void AddFixtureCandidates(
Dictionary<string, Dictionary<int, InflectionExactRole>> candidatesBySurface,
string[] forms,
int lexemeIndex,
InflectionExactRole role)
{
foreach (var form in forms)
{
if (!candidatesBySurface.TryGetValue(form, out var candidates))
{
candidates = [];
candidatesBySurface[form] = candidates;
}
candidates.TryGetValue(lexemeIndex, out var roles);
candidates[lexemeIndex] = roles | role;
}
}
static InflectionExactRole GetFixtureDisplayRole(CardinalPluralCategory category) =>
category switch
{
CardinalPluralCategory.Zero => InflectionExactRole.Zero,
CardinalPluralCategory.One => InflectionExactRole.One,
CardinalPluralCategory.Two => InflectionExactRole.Two,
CardinalPluralCategory.Few => InflectionExactRole.Few,
CardinalPluralCategory.Many => InflectionExactRole.Many,
CardinalPluralCategory.Other => InflectionExactRole.Other,
_ => InflectionExactRole.None
};
}