Add Swift Script

This commit is contained in:
Simon Rieger 2023-12-07 03:42:45 +01:00
parent 132e612542
commit 0e5bf7cf87
3 changed files with 95 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

14
Package.swift Normal file
View file

@ -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"),
]
)

73
Sources/main.swift Normal file
View file

@ -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)
}
}