👨‍💻 dev swift chat modules porting

development

owly

闇の伝説
Staff member
戦闘 コーダー
project progress at 100%
 
0% 100%


+ChatBot
+PercentDripper
+AXNPC
+AXTimeContextResponder
+UniqueItemsPriorityQue*Q peak()
+depracate perchance, perchance test

@fukurou
 
Last edited by a moderator:

fukurou

the supreme coder
ADMIN
Swift:
import Foundation

class PercentDripper {
    private let dr = DrawRnd()
    private var limis = 35
    
    func setLimis(_ limis: Int) {
        self.limis = limis
    }
    
    func drip() -> Bool {
        return dr.getSimpleRNDNum(100) < limis
    }
    
    func dripPlus(_ plus: Int) -> Bool {
        return dr.getSimpleRNDNum(100) < limis + plus
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
class PercentDripper {
    private let dr:DrawRnd = DrawRnd()
    private var limis:Int = 35
    
    func setLimis(_ limis: Int) {
        self.limis = limis
    }
    
    func drip() -> Bool {
        return dr.getSimpleRNDNum(bound: 100) < limis
    }
    
    func dripPlus(_ plus: Int) -> Bool {
        return dr.getSimpleRNDNum(bound: 100) < limis + plus
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
import Foundation

class AXNPC {
    var responder = Responder()
    var dripper = PercentDripper()
    
    func respond() -> String {
        if dripper.drip() {
            return responder.getAResponse()
        }
        return ""
    }
    
    func respondPlus(plus: Int) -> String {
        if dripper.dripPlus(plus: plus) {
            return responder.getAResponse()
        }
        return ""
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
class AXNPC {
    public var responder:Responder = Responder()
    public var dripper:PercentDripper = PercentDripper()
    
    func respond() -> String {
        if dripper.drip() {
            return responder.getAResponse()
        }
        return ""
    }
    
    func respondPlus(plus: Int) -> String {
        if dripper.dripPlus(plus) {
            return responder.getAResponse()
        }
        return ""
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
class AXTimeContextResponder {
    private var pl:PlayGround = PlayGround()
    var morning:Responder = Responder()
    var afternoon:Responder = Responder()
    var evening:Responder = Responder()
    var night:Responder = Responder()
    fileprivate var responders = [String: Responder]()
    
    init() {
        responders["morning"] = morning
        responders["afternoon"] = afternoon
        responders["evening"] = evening
        responders["night"] = night
    }
    
    func respond() -> String {
        return responders[pl.partOfDay()]?.getAResponse() ?? ""
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
class ChatBot {
    var sentences = RefreshQ()
    var wordToList = [String: RefreshQ]()
    private var regexUtil = RegexUtil()
    var allParamRef = [String: String]()
    var paramLim = 5
    var loggedParams = RefreshQ()
    private var conjuration = "is a"
    
    init(logParamLim: Int) {
        loggedParams.setLimit(logParamLim)
    }
    
    func setConjuration(_ conjuration: String) {
        self.conjuration = conjuration
    }
    
    func setSentencesLim(_ lim: Int) {
        sentences.setLimit(lim)
    }
    
    func setParamLim(_ paramLim: Int) {
        self.paramLim = paramLim
    }
    
    func getWordToList() -> [String: RefreshQ] {
        return wordToList
    }
    
    func talk() -> String {
        var result = sentences.getRNDElement()
        return clearRecursion(result)
    }
    
    private func clearRecursion(_ result: String) -> String {
        var params = [String]()
        params = regexUtil.extractAllRegexes("(\\w+)(?= #)", result)
        for strI in params {
            guard let temp = wordToList[strI] else { continue }
            let s1 = temp.getRNDElement()
            result = result.replacingOccurrences(of: strI + " #", with: s1)
        }
        if !result.contains("#") {
            return result
        } else {
            return clearRecursion(result)
        }
    }
    
    func addParam(_ category: String, _ value: String) {
        if !(wordToList.keys.contains(category)) {
            let temp = RefreshQ()
            temp.setLimit(paramLim)
            wordToList[category] = temp
        }
        wordToList[category]?.add(value)
        allParamRef[value] = category
    }
    
    func addParam(_ kv: AXKeyValuePair) {
        if !(wordToList.keys.contains(kv.getKey())) {
            let temp = RefreshQ()
            temp.setLimit(paramLim)
            wordToList[kv.getKey()] = temp
        }
        wordToList[kv.getKey()]?.add(kv.getValue())
        allParamRef[kv.getValue()] = kv.getKey()
    }
    
    func addSentence(_ sentence: String) {
        sentences.add(sentence)
    }
    
    func learn(_ s1: String) {
        var s1 = " " + s1
        for key in wordToList.keys {
            s1 = s1.replacingOccurrences(of: " " + key, with: " \(key) #")
        }
        sentences.add(s1.trimmingCharacters(in: .whitespaces))
    }
    
    func learnV2(_ s1: String) -> Bool {
        let OGStr = s1
        var s1 = " " + s1
        for key in allParamRef.keys {
            s1 = s1.replacingOccurrences(of: " " + key, with: " \(allParamRef[key]!) #")
        }
        s1 = s1.trimmingCharacters(in: .whitespaces)
        if OGStr != s1 {
            sentences.add(s1)
            return true
        }
        return false
    }
    
    func learnParam(_ s1: String) {
        guard s1.contains(conjuration) else { return }
        let category = regexUtil.afterWord(conjuration, s1)
        guard wordToList.keys.contains(category) else { return }
        let param = s1.replacingOccurrences(of: conjuration + " " + category, with: "").trimmingCharacters(in: .whitespaces)
        wordToList[category]?.add(param)
        allParamRef[param] = category
        loggedParams.add(s1)
    }
    
    func getALoggedParam() -> String {
        return loggedParams.getRNDElement()
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
class ChatBot {
    var sentences = RefreshQ()
    var wordToList = [String: RefreshQ]()
    private var regexUtil = RegexUtil()
    var allParamRef = [String: String]()
    var paramLim = 5
    var loggedParams = RefreshQ()
    private var conjuration = "is a"
    
    init(logParamLim: Int) {
        loggedParams.setLimit(lim: logParamLim)
    }
    
    func setConjuration(_ conjuration: String) {
        self.conjuration = conjuration
    }
    
    func setSentencesLim(_ lim: Int) {
        sentences.setLimit(lim: lim)
    }
    
    func setParamLim(_ paramLim: Int) {
        self.paramLim = paramLim
    }
    
    func getWordToList() -> [String: RefreshQ] {
        return wordToList
    }
    
    func talk() -> String {
        let result = sentences.getRndItem()
        return clearRecursion(result)
    }
    
    private func clearRecursion(_ result: String) -> String {
        var tempResult:String = result
        var params = [String]()
        params = regexUtil.extractAllRegexResults(regex: "(\\w+)(?= #)", text: result)
        for strI in params {
            guard let temp = wordToList[strI] else { continue }
            let s1 = temp.getRndItem()
            tempResult = tempResult.replacingOccurrences(of: strI + " #", with: s1)
        }
        if !tempResult.contains("#") {
            return tempResult
        } else {
            return clearRecursion(tempResult)
        }
    }
    
    func addParam(_ category: String, _ value: String) {
        if !(wordToList.keys.contains(category)) {
            let temp = RefreshQ()
            temp.setLimit(lim: paramLim)
            wordToList[category] = temp
        }
        wordToList[category]?.input(in1: value)
        allParamRef[value] = category
    }
    
    func addParam(_ kv: AXKeyValuePair) {
        if !(wordToList.keys.contains(kv.getKey())) {
            let temp = RefreshQ()
            temp.setLimit(lim: paramLim)
            wordToList[kv.getKey()] = temp
        }
        wordToList[kv.getKey()]?.input(in1: kv.getValue())
        allParamRef[kv.getValue()] = kv.getKey()
    }
    
    func addSentence(_ sentence: String) {
        sentences.input(in1: sentence)
    }
    
    func learn(_ s1: String) {
        var s1 = " " + s1
        for key in wordToList.keys {
            s1 = s1.replacingOccurrences(of: " " + key, with: " \(key) #")
        }
        sentences.input(in1: s1.trimmingCharacters(in: .whitespaces))
    }
    @discardableResult
    func learnV2(_ s1: String) -> Bool {
        let OGStr = s1
        var s1 = " " + s1
        for key in allParamRef.keys {
            s1 = s1.replacingOccurrences(of: " " + key, with: " \(allParamRef[key]!) #")
        }
        s1 = s1.trimmingCharacters(in: .whitespaces)
        if OGStr != s1 {
            sentences.input(in1: s1)
            return true
        }
        return false
    }
    
    func learnParam(_ s1: String) {
        guard s1.contains(conjuration) else { return }
        let category = regexUtil.afterWord(word: conjuration, str2Check: s1)
        guard wordToList.keys.contains(category) else { return }
        let param = s1.replacingOccurrences(of: conjuration + " " + category, with: "").trimmingCharacters(in: .whitespaces)
        wordToList[category]?.input(in1: param)
        allParamRef[param] = category
        loggedParams.input(in1: s1)
    }
    
    func getALoggedParam() -> String {
        return loggedParams.getRndItem()
    }
}
 
Top