Skip to content

Instantly share code, notes, and snippets.

@JUSTINMKAUFMAN
Last active February 21, 2020 15:26
Show Gist options
  • Save JUSTINMKAUFMAN/3067e19a7a50c134d3c356d724272b35 to your computer and use it in GitHub Desktop.
Save JUSTINMKAUFMAN/3067e19a7a50c134d3c356d724272b35 to your computer and use it in GitHub Desktop.
State of the art real-time verbal output transformer written in Swift
//
// SpeechTransformer.swift
// SpeechTransformer
//
// Created by Justin Kaufman 2/2/20.
// Copyright © 2020 Justin Kaufman. All rights reserved.
//
import Foundation
// [1] Create an instance of SpeechTransformer
let transformer = SpeechTransformer(
speaker: Speaker(name: "Justin"),
filters: [SWAGFilter()]
)
// [2] Become awesome
transformer.activate()
/// State of the art real-time verbal output transformer
class SpeechTransformer {
private let speaker: Speaker
private let filters: [SpeechFilter]
private var isActive: Bool = false {
didSet { filters.forEach { $0.toggle(isActive) } }
}
init(speaker: Speaker, filters: [SpeechFilter]) {
filters.forEach { $0.install(to: speaker) }
self.filters = filters
}
func activate() { isActive = true }
func deactivate() { isActive = false }
}
struct Speaker {
let name: String
}
protocol SpeechFilter {
var description: String { get }
func output(for phrase: String) -> String
func toggle(_ isOn: Bool)
func install(to speaker: Speaker)
}
/// Scientific wild-ass guess (SWAG) transformer plugin
struct SWAGFilter: SpeechFilter {
let description: String = "Improves the terrible estimate about to be uttered"
private static let redFlags: [String] = ["hours", "minutes", "seconds", "today"]
private var isOn: Bool = false
func output(for phrase: String) -> String {
/// Is this thing on?
guard isOn else { return phrase }
/// Speaker compatibility validation check
guard SWAGFilter.redFlags.filter({ phrase.contains($0) }).isEmpty else {
fatalError("Speaker requires more a powerful filter; try `NEWBFilter`?")
}
return phrase
.replacingOccurrences(of: "easy", with: "time consuming")
.replacingOccurrences(of: "soon", with: "in time")
.replacingOccurrences(of: " years", with: "+ years")
.replacingOccurrences(of: " months", with: "+ months to a year")
.replacingOccurrences(of: " weeks", with: "+ months")
.replacingOccurrences(of: " days", with: "+ weeks")
}
func toggle(_ isOn: Bool) { self.isOn = isOn }
func install(to speaker: Speaker) { // TODO: Add brain injection code here }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment