11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using SIL . Machine . QualityEstimation . Scores ;
5- using SIL . Machine . QualityEstimation . Usability ;
6- using SIL . Scripture ;
4+ using SIL . Machine . Corpora ;
75
86namespace SIL . Machine . QualityEstimation
97{
108 /// <summary>
119 /// Provides chrF3 quality estimation support for pre-translations.
1210 /// </summary>
13- public class QualityEstimation
11+ public class ChrF3QualityEstimation
1412 {
1513 private readonly BookScores _bookScores = new BookScores ( ) ;
1614 private readonly ChapterScores _chapterScores = new ChapterScores ( ) ;
@@ -20,7 +18,7 @@ public class QualityEstimation
2018 private readonly TxtFileScores _txtFileScores = new TxtFileScores ( ) ;
2119 private readonly List < VerseScore > _verseScores = new List < VerseScore > ( ) ;
2220
23- public QualityEstimation ( double slope , double intercept )
21+ public ChrF3QualityEstimation ( double slope , double intercept )
2422 {
2523 _slope = slope ;
2624 _intercept = intercept ;
@@ -81,7 +79,7 @@ public QualityEstimation(double slope, double intercept)
8179 /// Estimate the quality of the pre-translations from text files.
8280 /// </summary>
8381 /// <param name="confidences">The confidence values.</param>
84- public void EstimateQuality ( Dictionary < string , double > confidences )
82+ public void EstimateQuality ( IEnumerable < ( MultiKeyRef key , double confidence ) > confidences )
8583 {
8684 ProjectChrF3 ( confidences ) ;
8785 ComputeSequenceUsability ( ) ;
@@ -91,7 +89,7 @@ public void EstimateQuality(Dictionary<string, double> confidences)
9189 /// Estimate the quality of the pre-translations from USFM files.
9290 /// </summary>
9391 /// <param name="confidences">The confidence values.</param>
94- public void EstimateQuality ( Dictionary < VerseRef , double > confidences )
92+ public void EstimateQuality ( IEnumerable < ( ScriptureRef key , double confidence ) > confidences )
9593 {
9694 ProjectChrF3 ( confidences ) ;
9795 ComputeVerseUsability ( ) ;
@@ -127,20 +125,17 @@ private void ComputeBookUsability()
127125 {
128126 Score score = _bookScores . GetScore ( book ) ;
129127 if ( score is null )
130- {
131128 continue ;
132- }
133129
134130 List < double > bookUsabilities = _bookScores . GetVerseUsabilities ( book ) ;
135131 double averageProbability = bookUsabilities . Average ( ) ;
136132 UsabilityBooks . Add (
137- new BookUsability
138- {
139- Book = book ,
140- Usability = averageProbability ,
141- ProjectedChrF3 = score . ProjectedChrF3 ,
142- Label = BookThresholds . ReturnLabel ( averageProbability ) ,
143- }
133+ new BookUsability (
134+ book ,
135+ label : BookThresholds . ReturnLabel ( averageProbability ) ,
136+ usability : averageProbability ,
137+ projectedChrF3 : score . ProjectedChrF3
138+ )
144139 ) ;
145140 }
146141 }
@@ -154,21 +149,18 @@ private void ComputeChapterUsability()
154149 {
155150 Score score = _chapterScores . GetScore ( book , chapter ) ;
156151 if ( score is null )
157- {
158152 continue ;
159- }
160153
161154 List < double > chapterUsabilities = _chapterScores . GetVerseUsabilities ( book , chapter ) ;
162155 double averageProbability = chapterUsabilities . Average ( ) ;
163156 UsabilityChapters . Add (
164- new ChapterUsability
165- {
166- Book = book ,
167- Chapter = chapter ,
168- Usability = averageProbability ,
169- ProjectedChrF3 = score . ProjectedChrF3 ,
170- Label = ChapterThresholds . ReturnLabel ( averageProbability ) ,
171- }
157+ new ChapterUsability (
158+ book ,
159+ chapter ,
160+ label : ChapterThresholds . ReturnLabel ( averageProbability ) ,
161+ usability : averageProbability ,
162+ projectedChrF3 : score . ProjectedChrF3
163+ )
172164 ) ;
173165 }
174166 }
@@ -180,45 +172,41 @@ private void ComputeTxtFileUsability()
180172 {
181173 Score score = _txtFileScores . GetScore ( targetDraftFileStem ) ;
182174 if ( score is null )
183- {
184175 continue ;
185- }
186176
187177 List < double > txtFileUsabilities = _txtFileScores . GetSequenceUsabilities ( targetDraftFileStem ) ;
188178 double averageProbability = txtFileUsabilities . Average ( ) ;
189179 UsabilityTxtFiles . Add (
190- new TxtFileUsability
191- {
192- TargetDraftFile = targetDraftFileStem ,
193- Usability = averageProbability ,
194- ProjectedChrF3 = score . ProjectedChrF3 ,
195- Label = VerseThresholds . ReturnLabel ( averageProbability ) ,
196- }
180+ new TxtFileUsability (
181+ targetDraftFileStem ,
182+ label : BookThresholds . ReturnLabel ( averageProbability ) ,
183+ usability : averageProbability ,
184+ projectedChrF3 : score . ProjectedChrF3
185+ )
197186 ) ;
198187 }
199188 }
200189
201190 private void ComputeVerseUsability ( )
202191 {
203- foreach ( VerseScore verseScore in _verseScores . Where ( v => v . VerseRef . VerseNum > 0 ) )
192+ foreach ( VerseScore verseScore in _verseScores . Where ( v => v . ScriptureRef . VerseNum > 0 ) )
204193 {
205194 double probability = CalculateUsableProbability ( verseScore . ProjectedChrF3 ) ;
206195 _chapterScores . AppendVerseUsability (
207- verseScore . VerseRef . Book ,
208- verseScore . VerseRef . ChapterNum ,
196+ verseScore . ScriptureRef . Book ,
197+ verseScore . ScriptureRef . ChapterNum ,
209198 probability
210199 ) ;
211- _bookScores . AppendVerseUsability ( verseScore . VerseRef . Book , probability ) ;
200+ _bookScores . AppendVerseUsability ( verseScore . ScriptureRef . Book , probability ) ;
212201 UsabilityVerses . Add (
213- new VerseUsability
214- {
215- Book = verseScore . VerseRef . Book ,
216- Chapter = verseScore . VerseRef . ChapterNum ,
217- Verse = verseScore . VerseRef . Verse ,
218- Usability = probability ,
219- ProjectedChrF3 = verseScore . ProjectedChrF3 ,
220- Label = VerseThresholds . ReturnLabel ( probability ) ,
221- }
202+ new VerseUsability (
203+ book : verseScore . ScriptureRef . Book ,
204+ chapter : verseScore . ScriptureRef . ChapterNum ,
205+ verse : verseScore . ScriptureRef . Verse ,
206+ label : VerseThresholds . ReturnLabel ( probability ) ,
207+ usability : probability ,
208+ projectedChrF3 : verseScore . ProjectedChrF3
209+ )
222210 ) ;
223211 }
224212
@@ -233,36 +221,28 @@ private void ComputeSequenceUsability()
233221 double probability = CalculateUsableProbability ( sequenceScore . ProjectedChrF3 ) ;
234222 _txtFileScores . AppendSequenceUsability ( sequenceScore . TargetDraftFileStem , probability ) ;
235223 UsabilitySequences . Add (
236- new SequenceUsability
237- {
238- TargetDraftFile = sequenceScore . TargetDraftFileStem ,
239- SequenceNumber = sequenceScore . SequenceNumber ,
240- Usability = probability ,
241- ProjectedChrF3 = sequenceScore . ProjectedChrF3 ,
242- Label = VerseThresholds . ReturnLabel ( probability ) ,
243- }
224+ new SequenceUsability (
225+ targetDraftFile : sequenceScore . TargetDraftFileStem ,
226+ sequenceNumber : sequenceScore . SequenceNumber ,
227+ label : VerseThresholds . ReturnLabel ( probability ) ,
228+ usability : probability ,
229+ projectedChrF3 : sequenceScore . ProjectedChrF3
230+ )
244231 ) ;
245232 }
246233
247234 ComputeTxtFileUsability ( ) ;
248235 }
249236
250- private void ProjectChrF3 ( Dictionary < string , double > confidences )
237+ private void ProjectChrF3 ( IEnumerable < ( MultiKeyRef , double ) > confidences )
251238 {
252239 var confidencesByTxtFile = new Dictionary < string , List < double > > ( ) ;
253- foreach ( KeyValuePair < string , double > confidence in confidences )
240+ foreach ( ( MultiKeyRef key , double confidence ) in confidences )
254241 {
255- string [ ] keyParts = confidence . Key . Split ( ':' ) ;
256- if ( keyParts . Length == 2 && int . TryParse ( keyParts [ 1 ] , out int sequenceNumber ) )
242+ if ( key . Keys . Count >= 0 && int . TryParse ( key . Keys [ 0 ] . ToString ( ) , out int sequenceNumber ) )
257243 {
258- string targetDraftFileStem = keyParts [ 0 ] ;
259- var score = new SequenceScore (
260- _slope ,
261- confidence . Value ,
262- _intercept ,
263- sequenceNumber ,
264- targetDraftFileStem
265- ) ;
244+ string targetDraftFileStem = key . TextId ;
245+ var score = new SequenceScore ( _slope , confidence , _intercept , sequenceNumber , targetDraftFileStem ) ;
266246 _sequenceScores . Add ( score ) ;
267247
268248 // Record the confidence by text file
@@ -272,7 +252,7 @@ private void ProjectChrF3(Dictionary<string, double> confidences)
272252 confidencesByTxtFile [ targetDraftFileStem ] = txtFileConfidences ;
273253 }
274254
275- txtFileConfidences . Add ( confidence . Value ) ;
255+ txtFileConfidences . Add ( confidence ) ;
276256 }
277257 }
278258
@@ -285,16 +265,16 @@ private void ProjectChrF3(Dictionary<string, double> confidences)
285265 }
286266 }
287267
288- private void ProjectChrF3 ( Dictionary < VerseRef , double > confidences )
268+ private void ProjectChrF3 ( IEnumerable < ( ScriptureRef , double ) > confidences )
289269 {
290270 var confidencesByBook = new Dictionary < string , List < double > > ( ) ;
291271 var confidencesByBookAndChapter = new Dictionary < ( string , int ) , List < double > > ( ) ;
292- foreach ( KeyValuePair < VerseRef , double > confidence in confidences )
272+ foreach ( ( ScriptureRef key , double confidence ) in confidences )
293273 {
294- var score = new VerseScore ( _slope , confidence . Value , _intercept , confidence . Key ) ;
274+ var score = new VerseScore ( _slope , confidence , _intercept , key ) ;
295275 _verseScores . Add ( score ) ;
296- string book = confidence . Key . Book ;
297- int chapter = confidence . Key . ChapterNum ;
276+ string book = key . Book ;
277+ int chapter = key . ChapterNum ;
298278
299279 // Record the confidence by and chapter
300280 if (
@@ -308,7 +288,7 @@ out List<double> bookAndChapterConfidences
308288 confidencesByBookAndChapter [ ( book , chapter ) ] = bookAndChapterConfidences ;
309289 }
310290
311- bookAndChapterConfidences . Add ( confidence . Value ) ;
291+ bookAndChapterConfidences . Add ( confidence ) ;
312292
313293 // Record the confidence by book
314294 if ( ! confidencesByBook . TryGetValue ( book , out List < double > bookConfidences ) )
@@ -317,7 +297,7 @@ out List<double> bookAndChapterConfidences
317297 confidencesByBook [ book ] = bookConfidences ;
318298 }
319299
320- bookConfidences . Add ( confidence . Value ) ;
300+ bookConfidences . Add ( confidence ) ;
321301 }
322302
323303 foreach ( KeyValuePair < string , List < double > > bookConfidences in confidencesByBook )
0 commit comments