SPB Git

Repository Public

Support.swift 12.1 KB 213 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Keyboard9// File             : Shared/Tests/PredictionCoreTests/Support.swift10// Description      : Test support: lexicon providers, shared loaded engine,11//                    typo corpora (fictional text only).12// Module           : PredictionCoreTests13// Repository       : https://git.spboucher.ai/sp-keyboard14// Created          : 2026-12-0115// Last Modified    : 2026-12-0116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : None specified (no license file in this repository)82// Maintainer       : Simon-Pierre Boucher83// Technical Contact: dev@spboucher.ai84//85// =============================================================================86// END OF SP BOUCHER SOURCE HEADER87// =============================================================================8889import Foundation90import XCTest91@testable import PredictionCore9293/// The real lexicons shipped with the app, read from Resources/Lexicons in the repository94/// (SPK_LEXICONS overrides the location when the tests run from a copy of the package).95let repositoryLexicons: URL = {96    if let override = ProcessInfo.processInfo.environment["SPK_LEXICONS"] {97        return URL(fileURLWithPath: override, isDirectory: true)98    }99    return URL(fileURLWithPath: #filePath)100        .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()101        .appendingPathComponent("Resources/Lexicons", isDirectory: true)102}()103104struct MemoryLexiconProvider: LexiconProviding {105    var files: [String: Data] = [:]106107    init(_ files: [String: String]) {108        self.files = files.mapValues { Data($0.utf8) }109    }110111    init(raw: [String: Data]) {112        self.files = raw113    }114115    func data(for language: LexiconLanguage, resource: LexiconResource) throws -> Data {116        guard let data = files["\(language.rawValue).\(resource.fileExtension)"] else {117            throw LexiconError.missingResource(language, resource)118        }119        return data120    }121}122123func makeEngine(_ languages: [LexiconLanguage] = LexiconLanguage.allCases, cacheLimit: Int = 256) async throws -> PredictionEngine {124    let engine = PredictionEngine(provider: DirectoryLexiconProvider(directory: repositoryLexicons), cacheLimit: cacheLimit)125    for language in languages { try await engine.load(language) }126    return engine127}128129struct Typo {130    let typed: String131    let expected: String132    let kind: String133    init(_ typed: String, _ expected: String, _ kind: String) {134        self.typed = typed135        self.expected = expected136        self.kind = kind137    }138}139140/// Typing errors a thumb actually makes: t = transposition, n = neighbour key, m = missing letter, x = extra letter,141/// a = missing accent, p = missing apostrophe. Written by hand; none of these come from real conversations.142enum Corpus {143    static let french: [Typo] = [144        Typo("bonjuor", "bonjour", "t"), Typo("mecri", "merci", "t"), Typo("demian", "demain", "t"),145        Typo("mainetnant", "maintenant", "t"), Typo("smeaine", "semaine", "t"), Typo("quelqeu", "quelque", "t"),146        Typo("toujuors", "toujours", "t"), Typo("beacuoup", "beaucoup", "t"), Typo("rpobleme", "problème", "t"),147        Typo("bonjoir", "bonjour", "n"), Typo("merxi", "merci", "n"), Typo("demaim", "demain", "n"),148        Typo("travaul", "travail", "n"), Typo("maisom", "maison", "n"), Typo("aujourd'hyi", "aujourd'hui", "n"),149        Typo("vrainent", "vraiment", "n"), Typo("ensenble", "ensemble", "n"), Typo("rendez-vouz", "rendez-vous", "n"),150        Typo("courruel", "courriel", "n"), Typo("depanneyr", "dépanneur", "n"),151        Typo("bonjur", "bonjour", "m"), Typo("mantenant", "maintenant", "m"), Typo("beaucup", "beaucoup", "m"),152        Typo("toujurs", "toujours", "m"), Typo("vraimnt", "vraiment", "m"), Typo("probablemnt", "probablement", "m"),153        Typo("adrese", "adresse", "m"), Typo("apeler", "appeler", "m"), Typo("magasner", "magasiner", "m"),154        Typo("bonjourr", "bonjour", "x"), Typo("mercii", "merci", "x"), Typo("demainn", "demain", "x"),155        Typo("semainre", "semaine", "x"), Typo("travvail", "travail", "x"), Typo("exactementt", "exactement", "x"),156        Typo("ecole", "école", "a"), Typo("deja", "déjà", "a"), Typo("tres", "très", "a"), Typo("apres", "après", "a"),157        Typo("fenetre", "fenêtre", "a"), Typo("etudiant", "étudiant", "a"), Typo("probleme", "problème", "a"),158        Typo("evenement", "événement", "a"), Typo("reponse", "réponse", "a"), Typo("francais", "français", "a"),159        Typo("garcon", "garçon", "a"), Typo("numero", "numéro", "a"), Typo("depanneur", "dépanneur", "a"),160        Typo("cegep", "cégep", "a"), Typo("desole", "désolé", "a"), Typo("bientot", "bientôt", "a"),161        Typo("peut-etre", "peut-être", "a"), Typo("j'etais", "j'étais", "a"), Typo("l'ecole", "l'école", "a"),162        Typo("qu'ils", "qu'ils", "ok"), Typo("j'arive", "j'arrive", "m"), Typo("d'acord", "d'accord", "m"),163    ]164165    static let english: [Typo] = [166        Typo("teh", "the", "t"), Typo("adn", "and", "t"), Typo("freind", "friend", "t"), Typo("recieve", "receive", "t"),167        Typo("tommorow", "tomorrow", "t"), Typo("thier", "their", "t"), Typo("wiht", "with", "t"), Typo("waht", "what", "t"),168        Typo("becuase", "because", "t"), Typo("peopel", "people", "t"),169        Typo("thabks", "thanks", "n"), Typo("tomorriw", "tomorrow", "n"), Typo("meetimg", "meeting", "n"),170        Typo("pleasr", "please", "n"), Typo("morming", "morning", "n"), Typo("problen", "problem", "n"),171        Typo("availabke", "available", "n"), Typo("weekemd", "weekend", "n"), Typo("soirry", "sorry", "x"),172        Typo("helo", "hello", "m"), Typo("tomorow", "tomorrow", "m"), Typo("definitly", "definitely", "m"),173        Typo("probaly", "probably", "m"), Typo("beautful", "beautiful", "m"), Typo("diferent", "different", "m"),174        Typo("adress", "address", "m"), Typo("realy", "really", "m"), Typo("intresting", "interesting", "m"),175        Typo("helllo", "hello", "x"), Typo("thankss", "thanks", "x"), Typo("meetting", "meeting", "x"),176        Typo("untill", "until", "x"), Typo("writting", "writing", "x"), Typo("pleaase", "please", "x"),177        Typo("dont", "don't", "p"), Typo("im", "I'm", "p"), Typo("doesnt", "doesn't", "p"), Typo("didnt", "didn't", "p"),178        Typo("isnt", "isn't", "p"), Typo("youre", "you're", "p"), Typo("thats", "that's", "p"), Typo("ive", "I've", "p"),179        Typo("couldnt", "couldn't", "p"), Typo("wouldnt", "wouldn't", "p"), Typo("i", "I", "p"),180        Typo("cant", "can't", "p"), Typo("wont", "won't", "p"), Typo("theyre", "they're", "p"),181    ]182183    static let spanish: [Typo] = [184        Typo("garcias", "gracias", "t"), Typo("bueons", "buenos", "t"), Typo("mañaan", "mañana", "t"),185        Typo("timepo", "tiempo", "t"), Typo("proelbma", "problema", "t"), Typo("trabaoj", "trabajo", "t"),186        Typo("graciad", "gracias", "n"), Typo("buenis", "buenos", "n"), Typo("tienpo", "tiempo", "n"),187        Typo("trabaho", "trabajo", "n"), Typo("problena", "problema", "n"), Typo("sienpre", "siempre", "n"),188        Typo("tambuen", "también", "n"), Typo("ahira", "ahora", "n"),189        Typo("gracas", "gracias", "m"), Typo("tiepo", "tiempo", "m"), Typo("probema", "problema", "m"),190        Typo("siepre", "siempre", "m"), Typo("perfeto", "perfecto", "m"), Typo("neceito", "necesito", "m"),191        Typo("graciass", "gracias", "x"), Typo("holaa", "hola", "x"), Typo("trabajjo", "trabajo", "x"),192        Typo("problemma", "problema", "x"), Typo("siemppre", "siempre", "x"),193        Typo("nino", "niño", "a"), Typo("manana", "mañana", "a"), Typo("tambien", "también", "a"),194        Typo("despues", "después", "a"), Typo("adios", "adiós", "a"), Typo("telefono", "teléfono", "a"),195        Typo("rapido", "rápido", "a"), Typo("facil", "fácil", "a"), Typo("dificil", "difícil", "a"),196        Typo("corazon", "corazón", "a"), Typo("cancion", "canción", "a"), Typo("espanol", "español", "a"),197        Typo("senor", "señor", "a"), Typo("pequeno", "pequeño", "a"), Typo("informacion", "información", "a"),198    ]199200    /// Correct words that must never be auto-corrected (homophones, short function words, everyday vocabulary).201    static let validFrench = ["a", "à", "ou", "où", "est", "et", "la", "là", "sa", "ça", "ce", "se", "son", "sont", "on", "ont",202                              "mais", "mes", "du", "dû", "sur", "sûr", "cote", "côte", "côté", "courriel", "dépanneur",203                              "magasiner", "tuque", "poutine", "cégep", "pourriel", "bonjour", "merci", "fin", "semaine",204                              "j'ai", "l'école", "qu'il", "c'est", "d'accord", "aujourd'hui", "peut-être", "allô"]205    static let validEnglish = ["their", "there", "they're", "your", "you're", "its", "it's", "to", "too", "two", "then", "than",206                               "were", "we're", "where", "hello", "thanks", "meeting", "I", "I'm", "don't", "weekend",207                               "tomorrow", "ok", "gonna", "the", "of"]208    static let validSpanish = ["el", "él", "si", "sí", "tu", "tú", "mi", "mí", "que", "qué", "como", "cómo", "esta", "está",209                               "hola", "gracias", "mañana", "niño", "año", "también", "trabajo", "de", "se", "sé",210                               // real words that only look like a missing accent211                               "ano", "numero", "musica", "hablo", "papa"]212}213
Files main
Menu

Theme