Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "regexp"
- "strings"
- "time"
- )
- //Function ElizaResponse to take in and return a string
- func ElizaResponse(str string) string {
- // replace := "How do you know you are"
- /*Regex MatchString function with isolation of the word "father"
- *with a boundry ignore case regex command.
- */
- if matched, _ := regexp.MatchString(`(?i)bfatherb`, str);
- //Condition to replace the original string if it has the word "father"
- matched {
- return "Why don’t you tell me more about your father?"
- }
- r1 := regexp.MustCompile(`(?i)bI'?s*a?mb`)
- //Match the words "I am" and capture for replacement
- matched := r1.MatchString(str)
- //condition if "I am" is matched
- if matched {
- capturedString := r1.ReplaceAllString(str, "$1")
- boundaries := regexp.MustCompile(`b`)
- tokens := boundaries.Split(capturedString, -1)
- // List the reflections.
- reflections := [][]string{
- {`I`, `you`},
- {`you're`, `I'm`},
- {`your`, `my`},
- {`me`, `you`},
- {`you`, `I`},
- {`my`, `your`},
- }
- // Loop through each token, reflecting it if there's a match.
- for i, token := range tokens {
- for _, reflection := range reflections {
- if matched, _ := regexp.MatchString(reflection[0], token); matched {
- tokens[i] = reflection[1]
- break
- }
- }
- }
- // Put the tokens back together.
- return strings.Join(tokens, ``)
- }
- //Get random number from the length of the array of random struct
- //an array of strings for the random response
- response := []string{"I’m not sure what you’re trying to say. Could you explain it to me?",
- "How does that make you feel?",
- "Why do you say that?"}
- //Return a random index of the array
- return response[rand.Intn(len(response))]
- }
- func main() {
- rand.Seed(time.Now().UTC().UnixNano())
- fmt.Println("Im supposed to just take what you're saying at face value?")
- fmt.Println(ElizaResponse("Im supposed to just take what you're saying at face value?"))
- }
Add Comment
Please, Sign In to add comment