Rouge.lean

Lean 4 source, lean/DataMiningAsObservation/Rouge.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
Rouge.lean. Chapter 0 section 0.11, ROUGE.

ROUGE-1 recall counts the reference's words that the candidate also contains, term by term
with multiplicity, over the reference's length. It lies in the unit interval, it is one
when the candidate is the reference, and it is one for any rearrangement of the reference,
since it reads bags and not order. That last fact is why a ROUGE score certifies less than
it seems to, and why chapter 8 carries the 13.7 ROUGE-L number beside its re-validation.
-/
import Mathlib
import DataMiningAsObservation.BagOfWords

namespace DMO.Rouge

open DMO.BagOfWords

variable {α : Type*} [DecidableEq α]

/-- The overlap of a candidate with a reference, term by term with multiplicity. -/
def overlap (cand ref : List α) : ℕ := ∑ t ∈ ref.toFinset, min (bag cand t) (bag ref t)

/-- **ROUGE-1 recall.** -/
noncomputable def rouge1 (cand ref : List α) : ℝ := (overlap cand ref : ℝ) / ref.length

/-- The overlap never exceeds the reference's length. -/
theorem overlap_le (cand ref : List α) : overlap cand ref ≤ ref.length := by
  unfold overlap
  calc ∑ t ∈ ref.toFinset, min (bag cand t) (bag ref t)
      ≤ ∑ t ∈ ref.toFinset, bag ref t := Finset.sum_le_sum fun t _ => min_le_right _ _
    _ = ref.length := bag_sum ref

/-- **In the unit interval** for a nonempty reference. -/
theorem rouge1_mem_unit (cand ref : List α) (h : ref ≠ []) :
    0 ≤ rouge1 cand ref ∧ rouge1 cand ref ≤ 1 := by
  unfold rouge1
  have hpos : (0 : ℝ) < ref.length := by exact_mod_cast List.length_pos_iff.2 h
  refine ⟨by positivity, ?_⟩
  rw [div_le_one hpos]
  exact_mod_cast overlap_le cand ref

/-- **The reference scores one against itself.** -/
theorem rouge1_self (ref : List α) (h : ref ≠ []) : rouge1 ref ref = 1 := by
  unfold rouge1 overlap
  simp only [min_self]
  rw [bag_sum ref]
  exact div_self (by exact_mod_cast List.length_pos_iff.2 h |>.ne')

/-- **Any rearrangement of the reference scores one**, since ROUGE reads bags and not order. -/
theorem rouge1_perm (cand ref : List α) (h : ref ≠ []) (hp : cand.Perm ref) : rouge1 cand ref = 1 := by
  unfold rouge1 overlap
  rw [bag_perm cand ref hp]
  simp only [min_self]
  rw [bag_sum ref]
  exact div_self (by exact_mod_cast List.length_pos_iff.2 h |>.ne')

end DMO.Rouge