ReadabilityFramework/Sources/Readability/Scorers/ColemanLiauIndex.swift
2022-05-20 18:09:12 +08:00

34 lines
921 B
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// File.swift
//
//
// Created by Shibo Lyu on 2022/5/20.
//
import Foundation
public struct RAColemanLiauIndexScorer: RAScorer {
public static let requiresCommonMetrics: Set<RACommonMetric>? = [
.wordCount,
.characterCount,
.sentenceCount
]
public static let meta = RAScorerMeta(
name: "ColemanLiau Index",
creator: "Meri Coleman & T. L. Liau",
citation: "Coleman, M., & Liau, T. L. (1975). A computer readability formula designed for machine scoring. Journal of Applied Psychology, 60(2), 283."
)
public init() {}
public func score(_ text: String, metrics: RACommonMetricsCalculator.Results?) -> Double {
let wordCount = metrics![.wordCount]!
let l = metrics![.characterCount]! * 100 / wordCount
let s = metrics![.sentenceCount]! * 100 / wordCount
let cloze = 141.8401 - (0.214590 * l) + (1.079812 * s)
return (-27.4004 * cloze) + 23.06395
}
}