From 0e5bf7cf879bb4e4107fbcb57d9e2de8aa40469d Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Thu, 7 Dec 2023 03:42:45 +0100 Subject: [PATCH] Add Swift Script --- .gitignore | 8 +++++ Package.swift | 14 +++++++++ Sources/main.swift | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100644 Package.swift create mode 100644 Sources/main.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..98fd567 --- /dev/null +++ b/Package.swift @@ -0,0 +1,14 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "swift-playground", + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "swift-playground"), + ] +) diff --git a/Sources/main.swift b/Sources/main.swift new file mode 100644 index 0000000..4262229 --- /dev/null +++ b/Sources/main.swift @@ -0,0 +1,73 @@ +// The Swift Programming Language +// https://docs.swift.org/swift-book + +print("Hello, world!") + +import Foundation + +func percentageMatch(text: String, pattern: String) -> Double? { + guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { + return nil + } + + let range = NSRange(location: 0, length: text.utf16.count) + if let match = regex.firstMatch(in: text, options: [], range: range) { + let matchLength = match.range.length + let textLength = text.utf16.count + let matchPercentage = Double(matchLength) / Double(textLength) * 100.0 + print("\(pattern) Match Percentage: \(matchPercentage)%") + return matchPercentage + } + + return 0.0 +} + +let streetPattern = "[\\-öÖäÄüÜß.a-zA-Z\\s]{8,50}[\\d]{1,3}" +let postcodePattern = "([A-Za-z]{2}[-\\s])?[0-9]{5}[-öÖäÄüÜßA-Za-z\\s]{5,35}" +let namePattern = "[\\-öÖäÄüÜß()&.,A-Za-z\\s]{8,35}" +let beforeNamePattern = "[\\W&&[^\\s()&.,]]|^[tza][oun][\\s.:]|^[s][h][i][p][to][\\s.:]|^[f][oü][r][\\s.:]|herr|frau" +let nextNamePattern = "[\\W&&[^\\s,]]" +let removePattern = "delivery|adress|Empf[aä]nger|Consignee|R[üu][e]?cksendung" +let phonePattern = "(\\+?[0-9]+([\\s-][0-9]+)*)" +// let phonePattern = "[\\d]{4,}[\\s]?[\\d]{3,}|[+][\\d]{2}[\\s]?[\\d]{2}" + +let testStreetText = "Musterstraße 123" +let testStreetMatchPercentage = percentageMatch(text: testStreetText, pattern: streetPattern) + +let testPostcodeText = "DE-12345 Musterstadt" +let testPostcodeMatchPercentage = percentageMatch(text: testPostcodeText, pattern: postcodePattern) + +let testNameText = "Max Mustermann" +let testNameMatchPercentage = percentageMatch(text: testNameText, pattern: namePattern) + +let testPhoneText = "+49 123 456789" +let testPhoneMatchPercentage = percentageMatch(text: testPhoneText, pattern: phonePattern) + +let minimumMatchPercentage = 60.0 + +var mismatchedPatterns: [String] = [] + +if let streetMatchPercentage = testStreetMatchPercentage, streetMatchPercentage < minimumMatchPercentage { + mismatchedPatterns.append("Street Pattern (\(streetMatchPercentage)%)") +} + +if let postcodeMatchPercentage = testPostcodeMatchPercentage, postcodeMatchPercentage < minimumMatchPercentage { + mismatchedPatterns.append("Postcode Pattern (\(postcodeMatchPercentage)%)") +} + +if let nameMatchPercentage = testNameMatchPercentage, nameMatchPercentage < minimumMatchPercentage { + mismatchedPatterns.append("Name Pattern (\(nameMatchPercentage)%)") +} + +if let phoneMatchPercentage = testPhoneMatchPercentage, phoneMatchPercentage < minimumMatchPercentage { + mismatchedPatterns.append("Phone Pattern (\(phoneMatchPercentage)%)") +} + +if mismatchedPatterns.isEmpty { + print("All patterns match at least \(minimumMatchPercentage)%.") +} else { + print("Patterns not meeting the minimum match percentage:") + for pattern in mismatchedPatterns { + print(pattern) + } +} \ No newline at end of file