…endently
The max/second-max tracking loop only updated secondCount when the
running max was dethroned by a later, larger count:
if (count > maxCount) {
secondCount = maxCount.toDouble
maxCount = count
maxGender = gender
}
So secondCount stays at its initial 0.0 whenever the eventual overall
max happens to be the first entry Scala's Map iteration produces for
this page's pronoun counts, even if a genuinely close second-place
count exists. The confidence check maxCount/secondCount > minDifference
then divides by 0.0, evaluates to Infinity, and always passes, exactly
when it should be guarding against a near-tie.
Confirmed with the same counts in both iteration orders: male=10
processed first gives secondCount=0.0 (ratio Infinity, always
"confident"), female=9 processed first gives the correct secondCount=9.0
(ratio 1.11). Same data, different outcome, purely from unspecified map
iteration order.
Sort the counts descending and read off the top two instead, so the
result no longer depends on iteration order.
The max/second-max tracking loop in
GenderExtractoronly updatessecondCountwhen the runningmax is dethroned by a later, larger count:
So
secondCountstays at its initial0.0whenever the eventual overall max happens to be thefirst entry Scala's
Mapiteration produces for a page's pronoun counts — even when a genuinelyclose second-place count exists. The confidence check
maxCount/secondCount > minDifferencethendivides by
0.0, evaluates toInfinity, and always passes, exactly when it should be guardingagainst a near-tie.
Confirmed with the same counts in both iteration orders:
male=10processed first givessecondCount=0.0(ratioInfinity, always "confident");female=9processed first gives thecorrect
secondCount=9.0(ratio1.11). Same data, different outcome, purely from unspecifiedmap iteration order — this is a real, data-dependent nondeterminism, not a hypothetical.
Fix: sort the counts descending and read off the top two, so the result no longer depends on
iteration order.
Summary by CodeRabbit