Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr SMS Gateway9// File : ios/MacLustrSMS/Core/WritingTools.swift10// Description : Outils d'écriture locaux et déterministes (aucune IA,11// aucun réseau) : rendre compatible GSM, raccourcir pour12// tenir en un segment.13// Module : ios14// Repository : https://git.spboucher.ai/maclustr-sms15// Project Website : https://sms.spboucher.ai16// Created : 2026-12-0117// Last Modified : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: admin@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation9293struct WritingSuggestion: Equatable, Sendable {94 let text: String95 let steps: [String] // ce qui a été changé, en clair96 let before: SegmentInfo97 let after: SegmentInfo98 var improves: Bool { after.segments < before.segments || (after.encoding == .gsm7 && before.encoding == .ucs2) || after.units < before.units }99}100101enum WritingTools {102 /// Abréviations d'usage courant, sans ambiguïté dans un SMS. Appliquées dans cet ordre, seulement tant que le message ne tient pas.103 static let abbreviations: [(String, String)] = [104 ("rendez-vous", "RDV"), ("Rendez-vous", "RDV"), ("s'il vous plaît", "SVP"), ("S'il vous plaît", "SVP"), ("s'il vous plait", "SVP"),105 ("minutes", "min"), ("téléphone", "tél."), ("numéro", "no"), ("Numéro", "No"), ("confirmation", "conf."), ("avant-midi", "AM"), ("après-midi", "PM"),106 ("heures", "h"), ("Merci de votre compréhension.", "Merci."), ("Au plaisir de vous voir!", "Au plaisir!"),107 ]108109 /// Espaces en double, espaces en fin de ligne, lignes vides multiples.110 static func tidy(_ text: String) -> String {111 var s = text.replacing(/[ \t]{2,}/, with: " ").replacing(/[ \t]+\n/, with: "\n").replacing(/\n{3,}/, with: "\n\n")112 s = s.trimmingCharacters(in: .whitespacesAndNewlines)113 return s114 }115 /// Remplace tout ce qui a un équivalent dans l'alphabet GSM (guillemets, tirets longs, apostrophes courbes, espaces insécables, accents absents).116 static func gsmCompatible(_ text: String) -> WritingSuggestion {117 let before = Segments.info(text)118 let out = Segments.simplified(text)119 var steps: [String] = []120 let fixed = Segments.offenders(text).filter { $0.replacement != nil }121 if !fixed.isEmpty { steps.append("Caractères remplacés : " + fixed.map { "\(display($0.character)) → \($0.replacement == " " ? "espace" : $0.replacement ?? "")" }.joined(separator: ", ")) }122 let left = Segments.offenders(out)123 if !left.isEmpty { steps.append("Sans équivalent, laissés tels quels : " + left.map { display($0.character) }.joined(separator: " ")) }124 return WritingSuggestion(text: out, steps: steps, before: before, after: Segments.info(out))125 }126 private static func display(_ c: Character) -> String { c == "\u{00A0}" || c == "\u{202F}" ? "espace insécable" : c == " " ? "espace" : String(c) }127128 /// Raccourcit par étapes sûres jusqu'à tenir en un segment : nettoyage, caractères typographiques, puis abréviations une à une.129 /// S'arrête dès que le message tient ; ne coupe jamais de mots et ne supprime aucune information.130 static func shortenToOneSegment(_ text: String) -> WritingSuggestion {131 let before = Segments.info(text)132 var s = text, steps: [String] = []133 func fits() -> Bool { Segments.info(s).segments == 1 }134 let t = tidy(s)135 if t != s { s = t; steps.append("Espaces superflus retirés") }136 if !fits() || Segments.info(s).encoding == .ucs2 {137 let g = Segments.simplified(s)138 if g != s, Segments.info(g).segments <= Segments.info(s).segments { s = g; steps.append("Caractères typographiques remplacés (alphabet GSM)") }139 }140 for (long, short) in abbreviations where !fits() {141 if s.contains(long) { s = s.replacingOccurrences(of: long, with: short); steps.append("« \(long) » → « \(short) »") }142 }143 if !fits() {144 let p = s.replacing(/\x20([!?:;])/, with: { String($0.1) })145 if p != s { s = p; steps.append("Espace retirée avant ! ? : ;") }146 }147 return WritingSuggestion(text: s, steps: steps, before: before, after: Segments.info(s))148 }149}150