// Author: Matthew Shiel // Code adapted from https://github.com/kennysong/goeliza/ package eliza import ( "bytes" "fmt" "html/template" "math/rand" "regexp" "strings" "time" ) // NewBotPersonality is a utility method to create a bot instance from a personality and its context func NewBotPersonality(personality *Personality, context *ChatbotContext) *Chatbot { return &Chatbot{personality, context} } func NewChatbotInteraction(question, patterngroup, rawanswer, answer string) ChatbotInteraction { return ChatbotInteraction{Time: time.Now().UTC().String(), Question: question, PatternGroup: patterngroup, RawAnswer: rawanswer, Answer: answer} } func NewChatbotInteractionWithPattern(question, patterngroup, pattern, rawanswer, answer string) ChatbotInteraction { return ChatbotInteraction{Time: time.Now().UTC().String(), Question: question, PatternGroup: patterngroup, Pattern: pattern, RawAnswer: rawanswer, Answer: answer} } // ReplyTo will construct a reply for a given statement using ELIZA's rules. func (p *Chatbot) ReplyTo(statement string) ChatbotInteraction { // First, preprocess the statement for more effective matching statement = p.preprocess(statement) var rawResponse string // Then, we check if this is a quit statement if p.IsQuitStatement(statement) { rawResponse = p.GoodbyeResponse() return NewChatbotInteraction(statement, "QuitResponses", rawResponse, rawResponse) } // Next, we try to match the statement to a statement that ELIZA can // recognize, and construct a pre-determined, appropriate response. for _, similarQuestionResponse := range p.Personality.Psychobabble { for _, questionPattern := range similarQuestionResponse.SimilarQuestions { re := regexp.MustCompile(questionPattern) matches := re.FindStringSubmatch(statement) // If the statement matched any recognizable statements. if len(matches) > 0 { // If we matched a regex group in parentheses, get the first match. The matched regex group will match a "fragment" that will form // part of the response, for added realism. var fragment string if len(matches) > 1 { fragment = p.reflect(matches[1]) } // Choose a random appropriate response, and format it with the // fragment, if needed. response := p.randChoice(similarQuestionResponse.Responses) if strings.Contains(response, "%s") { response = fmt.Sprintf(response, fragment) } // fmt.Printf("For Statement \"%s\" got a hit with pattern \"%s\" Responded With \"%s\"\n", statement, pattern, response) return NewChatbotInteractionWithPattern(statement, "Psychobabble", questionPattern, response, p.replacePlaceHolders(response)) } } } // If no patterns were matched, return a default response. rawResponse = p.randChoice(p.Personality.DefaultResponses) return NewChatbotInteraction(statement, "DefaultResponses", rawResponse, p.replacePlaceHolders(rawResponse)) } // Greetings will return a random introductory sentence for ELIZA. func (p *Chatbot) Greetings() string { return p.randChoice(p.Personality.Introductions) } // GoodbyeResponse will return a random goodbye sentence for ELIZA. func (p *Chatbot) GoodbyeResponse() string { return p.randChoice(p.Personality.Goodbyes) } // IsQuitStatement returns if the statement is a quit statement func (p *Chatbot) IsQuitStatement(statement string) bool { statement = p.preprocess(statement) for _, quitResponse := range p.Personality.QuitResponses { if statement == quitResponse { return true } } return false } // preprocess will do some normalization on a statement for better regex matching func (p *Chatbot) preprocess(statement string) string { statement = strings.TrimRight(statement, "\n.!") statement = strings.ToLower(statement) return statement } // reflect flips a few words in an input fragment (such as "I" -> "you"). func (p *Chatbot) reflect(fragment string) string { words := strings.Split(fragment, " ") for i, word := range words { if reflectedWord, ok := p.Personality.ReflectedWords[word]; ok { words[i] = reflectedWord } } return strings.Join(words, " ") } // randChoice returns a random element in an (string) array. func (p *Chatbot) randChoice(list []string) string { // Added for truly random generation of numbers with seeds if len(list) == 0 { return "" } rand.Seed(time.Now().UnixNano()) maxAttempts := 2 * len(list) // try looking for an unspoken response at random which has not been used recently // 1st hash previous responses previousResponseHash := make(map[uint32]bool) for _, r := range p.Context.Session.Conversation { previousResponseHash[hash(r.RawAnswer)] = true } var candidate string for i := 1; i <= maxAttempts; i++ { randIndex := rand.Intn(len(list)) candidate = list[randIndex] h := hash(candidate) _, known := previousResponseHash[h] // if not known - use it // fmt.Println(randIndex, known, candidate) if !known { return candidate } } // if here then use default return candidate } /** * Replaces conversation templates (names etc) in reply with */ func (p *Chatbot) replacePlaceHolders(answer string) string { var tBuffer bytes.Buffer funcsMap := template.FuncMap{ "dayOfWeek": dayOfWeek, "fullDate": date, "year": year, } rawTemplate := template.New("answer") tmpl := rawTemplate.Funcs(funcsMap) t, _ := tmpl.Parse(answer) t.Execute(&tBuffer, p.Context) return tBuffer.String() }