From ff4bfa9a5017a9293e7451c7225724539204ef48 Mon Sep 17 00:00:00 2001 From: fwcd Date: Fri, 5 Jan 2024 22:12:50 +0000 Subject: [PATCH] Use LCS distance as metric for finding the best match --- Package.resolved | 4 ++-- Package.swift | 2 +- Sources/D2Commands/cau/exam/ExamCommand.swift | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Package.resolved b/Package.resolved index 5cf3d6ce..6eb8e9f4 100644 --- a/Package.resolved +++ b/Package.resolved @@ -301,8 +301,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/fwcd/swift-utils.git", "state" : { - "revision" : "31ed21dabedba93b3892adc0ec617787c9bf515e", - "version" : "2.0.1" + "revision" : "3090d5171bc3bba4d8cdb495483c46cd7fad422c", + "version" : "2.1.0" } }, { diff --git a/Package.swift b/Package.swift index 861f1e7d..69426b6c 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.3.2"), .package(url: "https://github.com/fwcd/swift-qrcode-generator.git", from: "1.0.0"), .package(url: "https://github.com/fwcd/swift-prolog.git", from: "0.1.0"), - .package(url: "https://github.com/fwcd/swift-utils.git", from: "2.0.1"), + .package(url: "https://github.com/fwcd/swift-utils.git", from: "2.1.0"), .package(url: "https://github.com/fwcd/swift-graphics.git", from: "2.1.0"), .package(url: "https://github.com/fwcd/swift-gif.git", from: "2.1.0"), .package(url: "https://github.com/fwcd/swift-mensa.git", from: "0.1.10"), diff --git a/Sources/D2Commands/cau/exam/ExamCommand.swift b/Sources/D2Commands/cau/exam/ExamCommand.swift index b3ce5e56..e2609f04 100644 --- a/Sources/D2Commands/cau/exam/ExamCommand.swift +++ b/Sources/D2Commands/cau/exam/ExamCommand.swift @@ -34,7 +34,11 @@ public class ExamCommand: StringCommand { exams.first { $0.module?.code == query } ?? exams.first { $0.module?.name == query } ?? exams.min(by: ascendingComparator { - ($0.module?.name ?? $0.module?.code)?.levenshteinDistance(to: query) ?? .max + [$0.module?.name, $0.module?.code] + .compactMap { $0 } + .map { $0.lcsDistance(to: query, caseSensitive: false) } + .min() + ?? .max }) }