-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathChrF3QualityEstimator.cs
More file actions
344 lines (306 loc) · 14.2 KB
/
ChrF3QualityEstimator.cs
File metadata and controls
344 lines (306 loc) · 14.2 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using SIL.Machine.Corpora;
namespace SIL.Machine.QualityEstimation
{
/// <summary>
/// Provides chrF3 quality estimation for pre-translations.
/// </summary>
public class ChrF3QualityEstimator
{
private readonly double _intercept;
private readonly double _slope;
/// <summary>
/// Initializes a new instance of the ChrF3QualityEstimator class with the specified slope and intercept values.
/// </summary>
/// <param name="slope">The slope value used in the quality estimation calculation.</param>
/// <param name="intercept">The intercept value used in the quality estimation calculation.</param>
public ChrF3QualityEstimator(double slope, double intercept)
{
_slope = slope;
_intercept = intercept;
}
/// <summary>
/// The threshold values used to calculate the usability label for every book or text.
/// </summary>
public Thresholds BookThresholds { get; set; } = new Thresholds(greenThreshold: 0.745, yellowThreshold: 0.62);
/// <summary>
/// The threshold values used to calculate the usability label for every chapter.
/// </summary>
public Thresholds ChapterThresholds { get; set; } =
new Thresholds(greenThreshold: 0.745, yellowThreshold: 0.62);
/// <summary>
/// The threshold values used to calculate the usability label for every segment.
/// </summary>
public Thresholds SegmentThresholds { get; set; } =
new Thresholds(greenThreshold: 0.745, yellowThreshold: 0.62);
/// <summary>
/// The usable parameters to calculate the usable probabilities.
/// </summary>
public UsabilityParameters Usable { get; set; } = UsabilityParameters.Usable;
/// <summary>
/// The unusable parameters to calculate the usable probabilities.
/// </summary>
public UsabilityParameters Unusable { get; set; } = UsabilityParameters.Unusable;
/// <summary>
/// Estimate the quality of the pre-translations from text files.
/// </summary>
/// <param name="confidences">The confidence values.</param>
/// <returns>The usability scores for every segment in the texts, and for the texts.</returns>
public (List<TextSegmentUsability> usabilitySegments, List<TextUsability> usabilityTexts) EstimateQuality(
IEnumerable<(MultiKeyRef key, double confidence)> confidences
)
{
(List<TextSegmentScore> segmentScores, TextScores textScores) = ProjectChrF3(confidences);
return ComputeSegmentUsability(segmentScores, textScores);
}
/// <summary>
/// Estimate the quality of the pre-translations from USFM files.
/// </summary>
/// <param name="confidences">The confidence values.</param>
/// <returns>The usability scores for every verse segment, chapter, and book.</returns>
public (
List<ScriptureSegmentUsability> usabilitySegments,
List<ScriptureChapterUsability> usabilityChapters,
List<ScriptureBookUsability> usabilityBooks
) EstimateQuality(IEnumerable<(ScriptureRef key, double confidence)> confidences)
{
(
List<ScriptureSegmentScore> segmentScores,
ScriptureChapterScores chapterScores,
ScriptureBookScores bookScores
) = ProjectChrF3(confidences);
return ComputeSegmentUsability(segmentScores, chapterScores, bookScores);
}
/// <summary>
/// Calculates the geometric mean for a collection of values.
/// </summary>
/// <param name="values"></param>
/// <returns>The geometric mean.</returns>
private static double GeometricMean(IList<double> values)
{
// Geometric mean requires positive values
if (values == null || !values.Any() || values.Any(x => x <= 0))
return 0;
// Compute the sum of the natural logarithms of all values,
// and divide by the count of numbers and take the exponential
return Math.Exp(values.Sum(Math.Log) / values.Count);
}
private double CalculateUsableProbability(double chrF3)
{
double usableWeight = Math.Exp(-Math.Pow(chrF3 - Usable.Mean, 2) / (2 * Usable.Variance)) * Usable.Count;
double unusableWeight =
Math.Exp(-Math.Pow(chrF3 - Unusable.Mean, 2) / (2 * Unusable.Variance)) * Unusable.Count;
return usableWeight / (usableWeight + unusableWeight);
}
private List<ScriptureBookUsability> ComputeBookUsability(ScriptureBookScores bookScores)
{
var usabilityBooks = new List<ScriptureBookUsability>();
foreach (string book in bookScores.Scores.Keys)
{
Score score = bookScores.GetScore(book);
if (score is null)
continue;
List<double> bookUsabilities = bookScores.GetSegmentUsabilities(book);
double averageProbability = bookUsabilities.Average();
usabilityBooks.Add(
new ScriptureBookUsability(
book,
label: BookThresholds.ReturnLabel(averageProbability),
usability: averageProbability,
projectedChrF3: score.ProjectedChrF3
)
);
}
return usabilityBooks;
}
private List<ScriptureChapterUsability> ComputeChapterUsability(ScriptureChapterScores chapterScores)
{
var usabilityChapters = new List<ScriptureChapterUsability>();
foreach (KeyValuePair<string, Dictionary<int, Score>> chapterScoresByBook in chapterScores.Scores)
{
string book = chapterScoresByBook.Key;
foreach (int chapter in chapterScoresByBook.Value.Keys)
{
Score score = chapterScores.GetScore(book, chapter);
if (score is null)
continue;
List<double> chapterUsabilities = chapterScores.GetSegmentUsabilities(book, chapter);
double averageProbability = chapterUsabilities.Average();
usabilityChapters.Add(
new ScriptureChapterUsability(
book,
chapter,
label: ChapterThresholds.ReturnLabel(averageProbability),
usability: averageProbability,
projectedChrF3: score.ProjectedChrF3
)
);
}
}
return usabilityChapters;
}
private (
List<ScriptureSegmentUsability>,
List<ScriptureChapterUsability>,
List<ScriptureBookUsability>
) ComputeSegmentUsability(
List<ScriptureSegmentScore> segmentScores,
ScriptureChapterScores chapterScores,
ScriptureBookScores bookScores
)
{
var usabilitySegments = new List<ScriptureSegmentUsability>();
foreach (ScriptureSegmentScore segmentScore in segmentScores)
{
double probability = CalculateUsableProbability(segmentScore.ProjectedChrF3);
chapterScores.AppendSegmentUsability(
segmentScore.ScriptureRef.Book,
segmentScore.ScriptureRef.ChapterNum,
probability
);
bookScores.AppendSegmentUsability(segmentScore.ScriptureRef.Book, probability);
usabilitySegments.Add(
new ScriptureSegmentUsability(
scriptureRef: segmentScore.ScriptureRef,
label: SegmentThresholds.ReturnLabel(probability),
usability: probability,
projectedChrF3: segmentScore.ProjectedChrF3
)
);
}
return (usabilitySegments, ComputeChapterUsability(chapterScores), ComputeBookUsability(bookScores));
}
private (List<TextSegmentUsability>, List<TextUsability>) ComputeSegmentUsability(
List<TextSegmentScore> segmentScores,
TextScores textScores
)
{
var usabilitySegments = new List<TextSegmentUsability>();
foreach (TextSegmentScore segmentScore in segmentScores)
{
double probability = CalculateUsableProbability(segmentScore.ProjectedChrF3);
textScores.AppendSegmentUsability(segmentScore.TextId, probability);
usabilitySegments.Add(
new TextSegmentUsability(
segmentRef: segmentScore.SegmentRef,
label: SegmentThresholds.ReturnLabel(probability),
usability: probability,
projectedChrF3: segmentScore.ProjectedChrF3
)
);
}
return (usabilitySegments, ComputeTextUsability(textScores));
}
private List<TextUsability> ComputeTextUsability(TextScores textScores)
{
var usabilityTexts = new List<TextUsability>();
foreach (string textId in textScores.Scores.Keys)
{
Score score = textScores.GetScore(textId);
if (score is null)
continue;
List<double> textUsabilities = textScores.GetSegmentUsabilities(textId);
double averageProbability = textUsabilities.Average();
usabilityTexts.Add(
new TextUsability(
textId,
label: BookThresholds.ReturnLabel(averageProbability),
usability: averageProbability,
projectedChrF3: score.ProjectedChrF3
)
);
}
return usabilityTexts;
}
private (List<TextSegmentScore> segmentScores, TextScores textScores) ProjectChrF3(
IEnumerable<(MultiKeyRef, double)> confidences
)
{
var confidencesByTextId = new Dictionary<string, List<double>>();
var segmentScores = new List<TextSegmentScore>();
foreach ((MultiKeyRef segmentRef, double confidence) in confidences)
{
var score = new TextSegmentScore(_slope, confidence, _intercept, segmentRef);
segmentScores.Add(score);
// Record the confidence by text id
string textId = segmentRef.TextId;
if (!confidencesByTextId.TryGetValue(textId, out List<double> textConfidences))
{
textConfidences = new List<double>();
confidencesByTextId[textId] = textConfidences;
}
textConfidences.Add(confidence);
}
var textScores = new TextScores();
foreach (KeyValuePair<string, List<double>> textIdConfidences in confidencesByTextId)
{
textScores.AddScore(
textIdConfidences.Key,
new Score(_slope, confidence: GeometricMean(textIdConfidences.Value), _intercept)
);
}
return (segmentScores, textScores);
}
private (
List<ScriptureSegmentScore> segmentScores,
ScriptureChapterScores chapterScores,
ScriptureBookScores bookScores
) ProjectChrF3(IEnumerable<(ScriptureRef, double)> confidences)
{
var confidencesByBook = new Dictionary<string, List<double>>();
var confidencesByBookAndChapter = new Dictionary<(string, int), List<double>>();
var segmentScores = new List<ScriptureSegmentScore>();
foreach ((ScriptureRef scriptureRef, double confidence) in confidences)
{
var score = new ScriptureSegmentScore(_slope, confidence, _intercept, scriptureRef);
segmentScores.Add(score);
string book = scriptureRef.Book;
int chapter = scriptureRef.ChapterNum;
// Record the confidence by and chapter
if (
!confidencesByBookAndChapter.TryGetValue(
(book, chapter),
out List<double> bookAndChapterConfidences
)
)
{
bookAndChapterConfidences = new List<double>();
confidencesByBookAndChapter[(book, chapter)] = bookAndChapterConfidences;
}
bookAndChapterConfidences.Add(confidence);
// Record the confidence by book
if (!confidencesByBook.TryGetValue(book, out List<double> bookConfidences))
{
bookConfidences = new List<double>();
confidencesByBook[book] = bookConfidences;
}
bookConfidences.Add(confidence);
}
var chapterScores = new ScriptureChapterScores();
foreach (
KeyValuePair<
(string Book, int Chapter),
List<double>
> bookAndChapterConfidences in confidencesByBookAndChapter
)
{
chapterScores.AddScore(
bookAndChapterConfidences.Key.Book,
bookAndChapterConfidences.Key.Chapter,
new Score(_slope, confidence: GeometricMean(bookAndChapterConfidences.Value), _intercept)
);
}
var bookScores = new ScriptureBookScores();
foreach (KeyValuePair<string, List<double>> bookConfidences in confidencesByBook)
{
bookScores.AddScore(
bookConfidences.Key,
new Score(_slope, confidence: GeometricMean(bookConfidences.Value), _intercept)
);
}
return (segmentScores, chapterScores, bookScores);
}
}
}