|
@@ -33,9 +33,11 @@ type ChatbotContext struct {
|
|
|
|
|
|
|
|
|
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"`
|
|
|
+ Time string `json:"time,omitempty" yaml:"time,omitempty"`
|
|
|
+ Question string `json:"question,omitempty" yaml:"question,omitempty"`
|
|
|
+ PatternGroup string `json:"patternGroup,omitempty" yaml:"patternGroup,omitempty"`
|
|
|
+ Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
|
|
|
+ Answer string `json:"answer,omitempty" yaml:"answer,omitempty"`
|
|
|
}
|
|
|
|
|
|
|
|
@@ -54,37 +56,34 @@ func NewBotPersonality(personality *Personality, context *ChatbotContext) *Chatb
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-func (p *Chatbot) Greetings() string {
|
|
|
- return p.randChoice(p.Personality.Introductions)
|
|
|
+func NewChatbotInteraction(question, patterngroup, answer string) ChatbotInteraction {
|
|
|
+ return ChatbotInteraction{Time: time.Now().UTC().String(), Question: question, PatternGroup: patterngroup, Answer: answer}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-func (p *Chatbot) GoodbyeResponse() string {
|
|
|
- return p.randChoice(p.Personality.Goodbyes)
|
|
|
+func NewChatbotInteractionWithPattern(question, patterngroup, pattern, answer string) ChatbotInteraction {
|
|
|
+ return ChatbotInteraction{Time: time.Now().UTC().String(), Question: question, PatternGroup: patterngroup, Pattern: pattern, Answer: answer}
|
|
|
}
|
|
|
|
|
|
|
|
|
-func (p *Chatbot) ReplyTo(statement string) string {
|
|
|
+func (p *Chatbot) ReplyTo(statement string) ChatbotInteraction {
|
|
|
|
|
|
statement = p.preprocess(statement)
|
|
|
|
|
|
|
|
|
if p.IsQuitStatement(statement) {
|
|
|
- return p.GoodbyeResponse()
|
|
|
+ return NewChatbotInteraction(statement, "QuitResponses", p.GoodbyeResponse())
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, similarQuestionResponse := range p.Personality.Psychobabble {
|
|
|
- for _, question := range similarQuestionResponse.SimilarQuestions {
|
|
|
- re := regexp.MustCompile(question)
|
|
|
+ for _, questionPattern := range similarQuestionResponse.SimilarQuestions {
|
|
|
+ re := regexp.MustCompile(questionPattern)
|
|
|
matches := re.FindStringSubmatch(statement)
|
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
var fragment string
|
|
|
if len(matches) > 1 {
|
|
@@ -98,13 +97,23 @@ func (p *Chatbot) ReplyTo(statement string) string {
|
|
|
response = fmt.Sprintf(response, fragment)
|
|
|
}
|
|
|
|
|
|
- return p.replacePlaceHolders(response)
|
|
|
+ return NewChatbotInteractionWithPattern(statement, "Psychobabble", questionPattern, p.replacePlaceHolders(response))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- return p.replacePlaceHolders(p.randChoice(p.Personality.DefaultResponses))
|
|
|
+ return NewChatbotInteraction(statement, "DefaultResponses", p.replacePlaceHolders(p.randChoice(p.Personality.DefaultResponses)))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func (p *Chatbot) Greetings() string {
|
|
|
+ return p.randChoice(p.Personality.Introductions)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func (p *Chatbot) GoodbyeResponse() string {
|
|
|
+ return p.randChoice(p.Personality.Goodbyes)
|
|
|
}
|
|
|
|
|
|
|
|
@@ -152,7 +161,16 @@ func (p *Chatbot) randChoice(list []string) string {
|
|
|
*/
|
|
|
func (p *Chatbot) replacePlaceHolders(answer string) string {
|
|
|
var tBuffer bytes.Buffer
|
|
|
- t := template.Must(template.New("answer").Parse(answer))
|
|
|
+ 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()
|