// // main.swift // Eliza // Swift adaptation of elizabot.js from https://www.masswerk.at/elizabot/ // by N. Landsteiner 2005; // // Created by Guy Lapalme on 2024-08-07. // import Foundation // replace last component of the path by "elizadata.json" var eliza = Eliza(path: #file.replacing(/(.*\/)(.*)/,with:{"\($0.1)elizadata.json"})) // clean input func unify(_ input:String) -> String { var text = input.lowercased() text.replace(/[@#\$%\^&\*\(\)_\+=~`\{\[\}\]\|:;<>\/\\\t]/, with: " "); text.replace(/\s+-+\s+/,with:"."); text.replace(/\s*[,\.\?!;]+\s*/,with: "."); text.replace(/\s*\bbut\b\s*/,with: "."); text.replace(/\s{2,}/,with: " "); return text } func makeQuestion(_ text:String) -> String { var question = "" for part in unify(text).split(separator: "."){ // separate input in parts let part = eliza.pres.replacing(in: String(part)) // preprocess the part if !part.isEmpty, let keyword = getKeyword(part) { question += reply(String(part),keyword)!+" " } } if question.isEmpty { return reply("x",eliza.keywords.first(where: {"xnone".contains($0.keywordRE)})!)! } else { return applyPostTrans(question) // apply post translation } } // search list of keywords (sorted in decreasing order of priority) // for the first one that matches func getKeyword(_ text:String)-> Keyword? { return eliza.keywords.first(where: {text.contains($0.keywordRE)}) } func reply(_ part:String,_ keyword:Keyword)->String? { for matcher in keyword.matchers { if let rpl = matcher(String(part)) { if let m = rpl.wholeMatch(of: /goto (\w+)/){ return reply(part,getKeyword(String(m.output.1))!) } else { return rpl } } } return nil } func applyPostTrans(_ text:String)->String { var res = text for (regex,str) in eliza.postTrans { if let m = res.firstMatch(of: regex) { if str.contains("$1"){res.replace(/\$1/, with: m.output[1].substring!)} if str.contains("$2"){res.replace(/\$2/, with: m.output[2].substring!)} } } return res } let landauerScript = [ "Well, my boyfriend made me come here.", "I dont like machines.", "Parlez-moi en francais!", "Men are all alike.", "They're always bugging us about something or other.", "He says I'm depressed much of the time.", "It's true. I am unhappy.", "I need some help, that much seems certain.", "Perhaps I could learn to get along with my mother.", "My mother takes care of me.", "My father.", "You are like my father in some ways.", "You are not very aggressive but I think you don't want me to notice that.", "You don't argue with me.", "You are afraid of me.", "My father is afraid of everybody.", "Bullies." ]; func runScript(lines:[String]) { print(eliza.initials.randomElement()!) for line in lines { print("User: \(line)") print("Eliza: \(makeQuestion(line))") } print(eliza.finals.randomElement()!) } // run Eliza interactively func chat () { print(eliza.initials.randomElement()!) print("> ",terminator: "") while let userInput = readLine() { if eliza.quits.contains(userInput){ print(eliza.finals.randomElement()!) break } print(makeQuestion(userInput)) print("> ",terminator: "") } }