eliza dev

fukurou

the supreme coder
ADMIN
Python:
import re
import random

class Eliza:
    reflections = {
        "am": "are"
    }

    class PhraseMatcher:
        def __init__(self, matcher, responses):
            self.matcher = re.compile(matcher)
            self.responses = responses

        def matches(self, str):
            return self.matcher.match(str) is not None

        def respond(self, str):
            m = self.matcher.match(str)
            p = self.random_phrase()
            for i in range(len(m.groups())):
                s = self.reflect(m.group(i + 1))
                p = p.replace("{" + str(i) + "}", s)
            return p

        @staticmethod
        def reflect(s):
            words = s.split(" ")
            for i in range(len(words)):
                if words[i] in Eliza.reflections:
                    words[i] = Eliza.reflections[words[i]]
            return " ".join(words)

        def random_phrase(self):
            return self.responses[abs(random.randint(0, len(self.responses) - 1))]

        def __str__(self):
            return self.matcher.pattern + ":" + str(self.responses)

    babble = [
        PhraseMatcher("quit", ["Thank you for talking with me.", "Good-bye.",
                               "Thank you, that will be $150.  Have a good day!"])
    ]

    def respond(self, msg):
        for pm in self.babble:
            if pm.matches(msg):
                return pm.respond(msg.lower())
        return ""
 

fukurou

the supreme coder
ADMIN
stem:
Python:
import re
import random


class Eliza:
    reflections = {
        "am": "are",
        "was": "were",
        "i": "you",
        "i'd": "you would",
        "i've": "you have",
        "my": "your",
        "are": "am",
        "you've": "I have",
        "you'll": "I will",
        "your": "my",
        "yours": "mine",
        "you": "i",
        "me": "you"
    }

    class PhraseMatcher:
        def __init__(self, matcher, responses):
            self.matcher = re.compile(matcher)
            self.responses = responses

        def matches(self, str):
            return self.matcher.match(str) is not None

        def respond(self, str):
            m = self.matcher.match(str)
            p = self.random_phrase()
            for i in range(len(m.groups())):
                s = self.reflect(m.group(i + 1))
                p = p.replace("{" + f'{i}' + "}", s)
            return p

        @staticmethod
        def reflect(s):
            words = s.split(" ")
            for i in range(len(words)):
                if words[i] in Eliza.reflections:
                    words[i] = Eliza.reflections[words[i]]
            return " ".join(words)

        def random_phrase(self):
            return self.responses[abs(random.randint(0, len(self.responses) - 1))]

        def __str__(self):
            return self.matcher.pattern + ":" + str(self.responses)

    babble = [
        PhraseMatcher("i need (.*)", ["Why do you need {0}?",
                                      "Would it really help you to get {0}?",
                                      "Are you sure you need {0}?"])
    ]

    def respond(self, msg):
        for pm in self.babble:
            if pm.matches(msg):
                return pm.respond(msg.lower())
        return ""
 

fukurou

the supreme coder
ADMIN
Python:
import re
import random


class Eliza:
    reflections = {
        "am": "are",
        "was": "were",
        "i": "you",
        "i'd": "you would",
        "i've": "you have",
        "my": "your",
        "are": "am",
        "you've": "I have",
        "you'll": "I will",
        "your": "my",
        "yours": "mine",
        "you": "i",
        "me": "you"
    }

    class PhraseMatcher:
        def __init__(self, matcher, responses):
            self.matcher = re.compile(matcher)
            self.responses = responses
            self.context: str = ""  # last speech context (subject or pattern)
            # example: i need (.*)
            self.param: str = ""  # last param extracted
            # example : water (for input: i need water)
            self.infoRequest: str = ""  # request more info on input
            # example: Why do you need {0}

        def matches(self, str):
            return self.matcher.match(str) is not None

        def respond(self, str):
            m = self.matcher.match(str)
            self.context = self.matcher.pattern  # context
            p = self.random_phrase()
            for i in range(len(m.groups())):
                s = self.reflect(m.group(i + 1))
                self.param = s  # param
                self.infoRequest = p  # more info request
                p = p.replace("{" + f'{i}' + "}", s)
            return p

        @staticmethod
        def reflect(s):
            words = s.split(" ")
            for i in range(len(words)):
                if words[i] in Eliza.reflections:
                    words[i] = Eliza.reflections[words[i]]
            return " ".join(words)

        def random_phrase(self):
            return self.responses[abs(random.randint(0, len(self.responses) - 1))]

        def __str__(self):
            return self.matcher.pattern + ":" + str(self.responses)

    babble = [
        PhraseMatcher("i need (.*)", ["Why do you need {0}?",
                                      "Would it really help you to get {0}?",
                                      "Are you sure you need {0}?"])
    ]

    def respond(self, msg):
        for pm in self.babble:
            if pm.matches(msg):
                return pm.respond(msg.lower())
        return ""
 

owly

闇の伝説
Staff member
戦闘 コーダー
shit in the ass:
Swift:
import Foundation

class Eliza {
    static let reflections = [
        "am": "are",
        "was": "were",
        "i": "you",
        "i'd": "you would",
        "i've": "you have",
        "my": "your",
        "are": "am",
        "you've": "I have",
        "you'll": "I will",
        "your": "my",
        "yours": "mine",
        "you": "i",
        "me": "you"
    ]

    class PhraseMatcher {
        let matcher: NSRegularExpression
        let responses: [String]
        var context: String = ""  // last speech context (subject or pattern)
        var param: String = ""  // last param extracted
        var infoRequest: String = ""  // request more info on input

        init(matcher: String, responses: [String]) {
            self.matcher = try! NSRegularExpression(pattern: matcher, options: [])
            self.responses = responses
        }

        func matches(_ str: String) -> Bool {
            let range = NSRange(location: 0, length: str.utf16.count)
            return matcher.firstMatch(in: str, options: [], range: range) != nil
        }

        func respond(_ str: String) -> String {
            let range = NSRange(location: 0, length: str.utf16.count)
            guard let m = matcher.firstMatch(in: str, options: [], range: range) else { return "" }
            context = matcher.pattern  // context
            var p = randomPhrase()
            for i in 0..<m.numberOfRanges {
                var s = reflect((str as NSString).substring(with: m.range(at: i)))
                param = s  // param
                infoRequest = p  // more info request
                p = p.replacingOccurrences(of: "{\(i)}", with: s)
            }
            return p
        }

        func reflect(_ s: String) -> String {
            var words = s.split(separator: " ")
            for i in 0..<words.count {
                if let reflection = Eliza.reflections[String(words[i])] {
                    words[i] = Substring(reflection)
                }
            }
            return words.joined(separator: " ")
        }

        func randomPhrase() -> String {
            return responses[Int.random(in: 0..<responses.count)]
        }

        var description: String {
            return "\(matcher.pattern): \(responses)"
        }
    }

    var babble = [
        PhraseMatcher(matcher: "i need (.*)", responses: ["Why do you need {0}?",
                                                          "Would it really help you to get {0}?",
                                                          "Are you sure you need {0}?"])
    ]

    func respond(_ msg: String) -> String {
        for pm in babble {
            if pm.matches(msg) {
                return pm.respond(msg.lowercased())
            }
        }
        return ""
    }
}
 
Top