123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package eliza
- import (
- "bytes"
- "fmt"
- "html/template"
- "math/rand"
- "regexp"
- "strings"
- "time"
- )
- type InteractiveBot interface {
- ReplyTo(statement string) (string, *Personality)
- }
- type Chatbot struct {
- Personality *Personality
- Context *ChatbotContext
- }
- type ChatbotContext struct {
- EngineVersion string
- Session SessionData
- }
- type ChatbotInteraction struct {
- Time string `json:"time,omitempty" yaml:"time,omitempty"`
- Question string `json:"question,omitempty" yaml:"question,omitempty"`
- Answer string `json:"answer,omitempty" yaml:"answer,omitempty"`
- }
- type SessionData struct {
- SessionID string `json:"sessionID" yaml:"sessionID"`
- StartTime string `json:"startTime,omitempty" yaml:"startTime,omitempty"`
- User string `json:"user,omitempty" yaml:"user,omitempty"`
- Bot string `json:"bot,omitempty" yaml:"bot,omitempty"`
- BotVersion string `json:"botVersion,omitempty" yaml:"botVersion,omitempty"`
- Conversation []ChatbotInteraction `json:"conversation,omitempty" yaml:"conversation,omitempty"`
- }
- func NewBotPersonality(personality *Personality, context *ChatbotContext) *Chatbot {
- return &Chatbot{personality, context}
- }
- func (p *Chatbot) Greetings() string {
- return p.randChoice(p.Personality.Introductions)
- }
- func (p *Chatbot) GoodbyeResponse() string {
- return p.randChoice(p.Personality.Goodbyes)
- }
- func (p *Chatbot) ReplyTo(statement string) string {
-
- statement = p.preprocess(statement)
-
- if p.IsQuitStatement(statement) {
- return p.GoodbyeResponse()
- }
-
-
- for _, similarQuestionResponse := range p.Personality.Psychobabble {
- for _, question := range similarQuestionResponse.SimilarQuestions {
- re := regexp.MustCompile(question)
- matches := re.FindStringSubmatch(statement)
-
- if len(matches) > 0 {
-
-
-
- var fragment string
- if len(matches) > 1 {
- fragment = p.reflect(matches[1])
- }
-
-
- response := p.randChoice(similarQuestionResponse.Responses)
- if strings.Contains(response, "%s") {
- response = fmt.Sprintf(response, fragment)
- }
-
- return p.replacePlaceHolders(response)
- }
- }
- }
-
- return p.replacePlaceHolders(p.randChoice(p.Personality.DefaultResponses))
- }
- func (p *Chatbot) IsQuitStatement(statement string) bool {
- statement = p.preprocess(statement)
- for _, quitResponse := range p.Personality.QuitResponses {
- if statement == quitResponse {
- return true
- }
- }
- return false
- }
- func (p *Chatbot) preprocess(statement string) string {
- statement = strings.TrimRight(statement, "\n.!")
- statement = strings.ToLower(statement)
- return statement
- }
- 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, " ")
- }
- func (p *Chatbot) randChoice(list []string) string {
-
- if len(list) == 0 {
- return ""
- }
- rand.Seed(time.Now().UnixNano())
- randIndex := rand.Intn(len(list))
- return list[randIndex]
- }
- func (p *Chatbot) replacePlaceHolders(answer string) string {
- var tBuffer bytes.Buffer
- t := template.Must(template.New("answer").Parse(answer))
- t.Execute(&tBuffer, p.Context)
- return tBuffer.String()
- }
|