SPB Git

Repository Public MIT

RecognitionTests.swift 4.7 KB 81 lines swift

1// ORIX Calc2// File: Packages/ORIXMathCamera/Tests/ORIXMathCameraTests/RecognitionTests.swift3// Description: Normaliser tests and a measured recognition benchmark on rendered images of printed expressions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import CoreGraphics10import CoreText11import ORIXExpression12@testable import ORIXMathCamera1314@Suite struct NormalizerTests {15    @Test func symbols() {16        #expect(MathTextNormalizer.normalize("12 × 4 ÷ 3 − 1 =") == "12 * 4 / 3 - 1")17        #expect(MathTextNormalizer.normalize("3 x 4") == "3*4")18        #expect(MathTextNormalizer.normalize("(2+3)X(4)") == "(2+3)*(4)")19        #expect(MathTextNormalizer.normalize("2x + 1") == "2x + 1")          // a variable, not a multiplication sign20        #expect(MathTextNormalizer.normalize("√16 + π") == "sqrt(16) + pi")21        #expect(MathTextNormalizer.normalize("1 234 + 5 = ?") == "1234 + 5")22        #expect(MathTextNormalizer.normalize("12 : 4") == "12/4")23    }24    @Test func confusionRepair() {25        #expect(MathTextNormalizer.repairConfusions("1O + 2l") == "10 + 21")26        #expect(MathTextNormalizer.repairConfusions("s1n(30)") == "sin(30)")27        #expect(MathTextNormalizer.repairConfusions("log(100)") == "log(100)")28        let ranked = MathRecognizer.rank([("4 + * 2", 0.9), ("4 + 2", 0.6)])29        #expect(ranked.first?.normalized == "4 + 2" && ranked.first?.parses == true)30    }31}3233/// Renders a line of text the way a printed worksheet or a screen would show it.34func render(_ text: String, font: String, size: CGFloat = 54) -> CGImage? {35    let attributes: [NSAttributedString.Key: Any] = [36        NSAttributedString.Key(kCTFontAttributeName as String): CTFontCreateWithName(font as CFString, size, nil),37        NSAttributedString.Key(kCTForegroundColorAttributeName as String): CGColor(gray: 0.05, alpha: 1),38    ]39    let line = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: attributes))40    let bounds = CTLineGetBoundsWithOptions(line, .useOpticalBounds)41    let width = Int(bounds.width + 120), height = Int(bounds.height + 120)42    guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(),43                                  bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { return nil }44    context.setFillColor(CGColor(gray: 1, alpha: 1))45    context.fill(CGRect(x: 0, y: 0, width: width, height: height))46    context.textPosition = CGPoint(x: 60 - bounds.minX, y: 60 - bounds.minY)47    CTLineDraw(line, context)48    return context.makeImage()49}5051@Suite(.serialized) struct RecognitionBenchmark {52    static let expressions = [53        "25 × 4", "12.5 + 7.25", "(3 + 4) × 5", "144 ÷ 12", "2^10", "100 − 37", "3.14159 × 2", "(8 − 3) × (2 + 6)",54        "sin(30) + cos(60)", "sqrt(16) + 9", "1000 × 1.05", "7 × 8 − 6 ÷ 2", "45 + 55 + 100", "0.5 × 0.25", "ln(10)",55        "log(1000)", "(1 + 2) ÷ 3", "9 × 9 × 9", "15% × 200", "2 × (3 + (4 − 1))", "600 ÷ 4 − 25", "1.5 × 10^6",56        "81 ÷ 9 + 1", "tan(45)", "3 × 4 + 5 × 6", "50 − 12.75", "(10 + 20) × 30", "999 + 1", "6.02 × 10^23", "4 × (2.5 + 1.5)",57    ]5859    /// Exact-match rate after normalisation: the top candidate must evaluate to the same value as the source expression.60    @Test func printedLinearExpressions() async throws {61        var correct = 0, parsed = 0, total = 062        var failures: [String] = []63        for font in ["Helvetica", "Times New Roman", "Menlo"] {64            for expression in RecognitionBenchmark.expressions {65                guard let image = render(expression, font: font) else { continue }66                total += 167                let expected = try Evaluator().evaluate(try Parser.parse(MathTextNormalizer.normalize(expression)))68                guard let lines = try? await MathRecognizer.recognize(image), let top = lines.first?.first else { failures.append("\(font): \(expression) → nothing"); continue }69                if top.parses { parsed += 1 }70                if top.parses, let e = try? Parser.parse(top.normalized), let v = try? Evaluator().evaluate(e), v == expected { correct += 1 }71                else { failures.append("\(font): \(expression) → \(top.raw) ⇒ \(top.normalized)") }72            }73        }74        let rate = Double(correct) / Double(max(total, 1))75        print("MATH-CAMERA-BENCHMARK total=\(total) parsed=\(parsed) correct=\(correct) rate=\(String(format: "%.3f", rate))")76        for f in failures { print("MATH-CAMERA-MISS \(f)") }77        // The feature ships as a reviewed-entry aid, not as an automatic solver; this floor guards against regressions.78        #expect(rate >= 0.80)79    }80}81
Files main
Menu

Theme