import Foundation import SprachmittlerEngines import SprachmittlerText /// chrF and BLEU metrics with no Python dependency (`docs/MODEL_EVALUATION.md` ยง3). public enum EvaluationMetrics { /// Character n-gram F-score. The beta is 2; spaces and punctuation count as characters. public static func chrF(reference: String, hypothesis: String) -> Double { let ref = Array(reference.lowercased()) let hyp = Array(hypothesis.lowercased()) guard !ref.isEmpty || !hyp.isEmpty else { return 100 } var scores: [Double] = [] for size in 1 ... 6 { let refGrams = ngrams(ref, size: size) let hypGrams = ngrams(hyp, size: size) guard !refGrams.isEmpty, !hypGrams.isEmpty else { continue } let matched = min(min(refGrams.count, hypGrams.count), matchingCount(refGrams, hypGrams)) let precision = Double(matched) / Double(hypGrams.count) let recall = Double(matched) / Double(refGrams.count) guard precision + recall > 0 else { scores.append(0) continue } let beta = 2.0 let betaSquared = beta * beta scores.append((1 + betaSquared) * precision * recall / (betaSquared * precision + recall)) } guard !scores.isEmpty else { return 100 } return 100 * scores.reduce(0, +) / Double(scores.count) } /// A corpus-style BLEU percentage for one hypothesis/reference pair. public static func bleu(reference: String, hypothesis: String) -> Double { let ref = reference.lowercased().split(whereSeparator: \.isWhitespace).map(String.init) let hyp = hypothesis.lowercased().split(whereSeparator: \.isWhitespace).map(String.init) guard !hyp.isEmpty else { return 0 } var logSum = 0.0 for size in 1 ... 4 { let hypGrams = wordNgrams(hyp, size: size) let refGrams = wordNgrams(ref, size: size) guard !hypGrams.isEmpty else { return 0 } let matched = min(min(hypGrams.count, refGrams.count), matchingCount(hypGrams, refGrams)) guard matched > 0 else { return 0 } logSum += log(Double(matched) / Double(hypGrams.count)) } let penalty = hyp.count < ref.count ? exp(1 - Double(ref.count) / Double(hyp.count)) : 1 return 100 * penalty * exp(logSum / 4) } /// Share of protected-token kinds that survive exactly; 1.0 when the source has none. public static func tokenRetention(source: String, translation: String) -> Double { let tokens = ProtectedTokenScanner().scan(source) guard !tokens.isEmpty else { return 1 } let retained = tokens.filter { token in translation.components(separatedBy: token.value).count - 1 >= source.components(separatedBy: token.value).count - 1 } return Double(retained.count) / Double(tokens.count) } /// Share of source numeric values present after locale normalisation; 1.0 when none. public static func numericConsistency(source: String, translation: String) -> Double { let sourceNumbers = Set(NumericTokenNormalizer.numbers(in: source)) guard !sourceNumbers.isEmpty else { return 1 } let translated = Set(NumericTokenNormalizer.numbers(in: translation)) return Double(sourceNumbers.intersection(translated).count) / Double(sourceNumbers.count) } private static func ngrams(_ characters: [Character], size: Int) -> [String] { guard characters.count >= size else { return [] } return (0 ... characters.count - size).map { index in String(characters[index ..< index + size]) } } private static func wordNgrams(_ words: [String], size: Int) -> [String] { guard words.count >= size else { return [] } return (0 ... words.count - size).map { index in words[index ..< index + size].joined(separator: " ") } } private static func matchingCount(_ lhs: [String], _ rhs: [String]) -> Int { var rhsCounts: [String: Int] = [:] for gram in rhs { rhsCounts[gram, default: 0] += 1 } var matched = 0 for gram in lhs { guard let count = rhsCounts[gram], count > 0 else { continue } matched += 1 rhsCounts[gram] = count - 1 } return matched } }