Guest User

Untitled

a guest
Nov 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "regexp"
  7. "strings"
  8. "time"
  9. )
  10.  
  11. //Function ElizaResponse to take in and return a string
  12. func ElizaResponse(str string) string {
  13.  
  14. // replace := "How do you know you are"
  15.  
  16. /*Regex MatchString function with isolation of the word "father"
  17. *with a boundry ignore case regex command.
  18. */
  19. if matched, _ := regexp.MatchString(`(?i)bfatherb`, str);
  20. //Condition to replace the original string if it has the word "father"
  21. matched {
  22. return "Why don’t you tell me more about your father?"
  23. }
  24. r1 := regexp.MustCompile(`(?i)bI'?s*a?mb`)
  25.  
  26. //Match the words "I am" and capture for replacement
  27. matched := r1.MatchString(str)
  28.  
  29. //condition if "I am" is matched
  30. if matched {
  31.  
  32. capturedString := r1.ReplaceAllString(str, "$1")
  33. boundaries := regexp.MustCompile(`b`)
  34. tokens := boundaries.Split(capturedString, -1)
  35.  
  36. // List the reflections.
  37. reflections := [][]string{
  38. {`I`, `you`},
  39. {`you're`, `I'm`},
  40. {`your`, `my`},
  41. {`me`, `you`},
  42. {`you`, `I`},
  43. {`my`, `your`},
  44. }
  45.  
  46. // Loop through each token, reflecting it if there's a match.
  47. for i, token := range tokens {
  48. for _, reflection := range reflections {
  49. if matched, _ := regexp.MatchString(reflection[0], token); matched {
  50. tokens[i] = reflection[1]
  51. break
  52. }
  53. }
  54. }
  55.  
  56. // Put the tokens back together.
  57. return strings.Join(tokens, ``)
  58.  
  59. }
  60.  
  61. //Get random number from the length of the array of random struct
  62. //an array of strings for the random response
  63. response := []string{"I’m not sure what you’re trying to say. Could you explain it to me?",
  64. "How does that make you feel?",
  65. "Why do you say that?"}
  66. //Return a random index of the array
  67. return response[rand.Intn(len(response))]
  68.  
  69. }
  70.  
  71. func main() {
  72. rand.Seed(time.Now().UTC().UnixNano())
  73.  
  74. fmt.Println("Im supposed to just take what you're saying at face value?")
  75. fmt.Println(ElizaResponse("Im supposed to just take what you're saying at face value?"))
  76.  
  77.  
  78. }
Add Comment
Please, Sign In to add comment