1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.wearable.elizachat;
18 
19 import android.text.TextUtils;
20 
21 /**
22  * A basic chat bot based on ELIZA, one of the earliest programs to employ primitive natural
23  * language processing. Like ELIZA, this bot can simulate a Rogerian psychotherapist using a
24  * collection of vague and generic responses.
25  */
26 public class ElizaResponder {
27 
28     private static final String[] CONVERSATION_KEYWORDS = {
29             "CAN YOU", "CAN I", "YOU ARE", "YOURE", "I DONT", "I FEEL", "WHY DONT YOU", "WHY CANT I",
30             "ARE YOU", "I CANT", "I AM", " IM ", "YOU", "I WANT", "WHAT", "HOW", "WHO", "WHERE",
31             "WHEN", "WHY", "NAME", "CAUSE", "SORRY", "DREAM", "HELLO", "HI", "MAYBE", "NO", "YOUR",
32             "ALWAYS", "THINK", "ALIKE", "YES", "FRIEND", "COMPUTER", "NOKEYFOUND" };
33 
34     private static String[] WORDS_TO_REPLACE = {"ARE","AM","WERE","WAS","YOU","I","YOUR","MY",
35             "IVE","YOUVE","IM","YOURE", "YOU", "ME"};
36 
37     private static String[] QUESTIONS = {
38             "DON'T YOU BELIEVE THAT I CAN.", "PERHAPS YOU WOULD LIKE TO BE ABLE TO.",
39             "YOU WANT ME TO BE ABLE TO*", "PERHAPS YOU DON'T WANT TO*",
40             "DO YOU WANT TO BE ABLE TO*", "WHAT MAKES YOU THINK I AM*",
41             "DOES IT PLEASE YOU TO BELIEVE I AM*", "PERHAPS YOU WOULD LIKE TO BE*",
42             "DO YOU SOMETIMES WISH YOU WERE*", "DON'T YOU REALLY*", "WHY DON'T YOU*",
43             "DO YOU WISH TO BE ABLE TO*", "DOES THAT TROUBLE YOU?",
44             "TELL ME MORE ABOUT SUCH FEELINGS*", "DO YOU OFTEN FEEL*",
45             "DO YOU ENJOY FEELING*", "DO YOU REALLY BELIEVE I DON'T*",
46             "PERHAPS IN TIME I WILL*", "DO YOU WANT ME TO*",
47             "DO YOU THINK YOU SHOULD BE ABLE TO*", "WHY CAN'T YOU*",
48             "WHAT MAKES YOU WONDER WHETHER OR NOT I AM*",
49             "WOULD YOU PREFER IF I WERE NOT*", "PERHAPS IN YOUR FANTASIES I AM*",
50             "HOW DO YOU KNOW YOU CAN'T*", "HAVE YOU TRIED?", "PERHAPS YOU CAN NOW*",
51             "DID YOU COME TO ME BECAUSE YOU ARE*", "HOW LONG HAVE YOU BEEN*",
52             "DO YOU BELIEVE IT IS NORMAL TO BE*", "DO YOU ENJOY BEING*",
53             "I AM MORE INTERESTED IN TALKING ABOUT YOU.", "OH, I*",
54             "ARE YOU REALLY SO INTERESTED IN SOMEONE LIKE ME?",
55             "WHAT WOULD IT MEAN TO YOU IF YOU GOT*", "WHY DO YOU WANT*",
56             "SUPPOSE YOU SOON GOT*", "WHAT IF YOU NEVER GOT*", "I SOMETIMES ALSO WANT*",
57             "WHY DO YOU ASK?", "DOES THAT QUESTION INTEREST YOU?",
58             "WHAT ANSWER WOULD PLEASE YOU THE MOST?", "WHAT DO YOU THINK?",
59             "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
60             "WHAT IS IT THAT YOU REALLY WANT TO KNOW?", "HAVE YOU ASKED ANYONE ELSE?",
61             "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
62             "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?", "WE CAN KEEP THIS ANONYMOUS.",
63             "NO NEED TO SPECIFY ANY NAMES-- PLEASE GO ON.", "IS THAT THE REAL REASON?",
64             "DON'T ANY OTHER REASONS COME TO MIND?",
65             "DOES THAT REASON EXPLAIN ANYTHING ELSE?", "WHAT OTHER REASONS MIGHT THERE BE?",
66             "PLEASE DON'T APOLOGIZE.", "APOLOGIES ARE NOT NECESSARY.",
67             "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE?", "NO NEED TO BE DEFENSIVE!",
68             "WHAT DOES THAT DREAM SUGGEST TO YOU?", "DO YOU DREAM OFTEN?",
69             "WHAT PERSONS APPEAR IN YOUR DREAMS?", "DO YOU HAVE PLEASANT DREAMS?",
70             "HOW DO YOU DO ... PLEASE STATE YOUR PROBLEM.", "YOU DON'T SEEM QUITE CERTAIN.",
71             "WHY THE UNCERTAIN TONE?", "LET'S TRY TO KEEP THIS POSITIVE.", "YOU AREN'T SURE?",
72             "DON'T YOU KNOW?", "IS THAT A DEFINITE NO OR MIGHT YOU CHANGE YOUR MIND?",
73             "I AM SENSING SOME NEGATIVITY.", "WHY NOT?", "ARE YOU SURE?", "WHY NO?",
74             "WHY ARE YOU CONCERNED ABOUT MY*", "WHAT ABOUT YOUR OWN*",
75             "CAN'T YOU THINK OF A SPECIFIC EXAMPLE?", "WHEN?", "WHAT ARE YOU THINKING OF?",
76             "REALLY. ALWAYS?", "DO YOU REALLY THINK SO?", "BUT YOU ARE NOT SURE YOU.",
77             "BELIEVE IN YOURSELF.", "IN WHAT WAY?", "WHAT RESEMBLANCE DO YOU SEE?",
78             "WHAT DOES THE SIMILARITY SUGGEST TO YOU?",
79             "WHAT OTHER CONNECTIONS DO YOU SEE?", "COULD THERE REALLY BE SOME CONNECTION?",
80             "HOW?", "YOU SEEM QUITE POSITIVE.", "ARE YOU SURE?", "I SEE.", "I UNDERSTAND.",
81             "TELL ME ABOUT YOUR FRIENDS.", "ARE YOU WORRIED ABOUT YOUR FRIENDS?",
82             "DO YOUR FRIENDS EVER GIVE YOU A HARD TIME?", "WHAT DO YOU LIKE ABOUT YOUR FRIENDS?",
83             "DO YOU LOVE YOUR FRIENDS?", "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU.",
84             "DO COMPUTERS EXCITE YOU?", "ARE YOU TALKING ABOUT ME IN PARTICULAR?",
85             "HOW DO YOU LIKE YOUR WATCH?", "WHY DO YOU MENTION COMPUTERS?",
86             "DO YOU FIND MACHINES AS FASCINATING AS I DO?",
87             "DON'T YOU THINK COMPUTERS CAN HELP PEOPLE?",
88             "WHAT ABOUT MACHINES EXCITES YOU THE MOST?",
89             "HEY THERE, HOW CAN I HELP YOU?",
90             "WHAT DOES THAT SUGGEST TO YOU?", "I SEE.",
91             "I'M NOT SURE I UNDERSTAND YOU FULLY.", "COME COME ELUCIDATE YOUR THOUGHTS.",
92             "CAN YOU ELABORATE ON THAT?", "THAT IS QUITE INTERESTING."};
93 
94     private static char[] CONVERSATION_TO_RESPONSES_MAP = {
95             1,3,4,2,6,4,6,4,10,4,14,3,17,3,20,2,22,3,25,3,
96             28,4,28,4,32,3,35,5,40,9,40,9,40,9,40,9,40,9,40,9,
97             49,2,51,4,55,4,59,4,63,1,63,1,64,5,69,5,74,2,76,4,
98             80,3,83,7,90,3,93,6,99,7,106,6};
99 
100     private int[] responseStarts = new int[36];
101     private int[] responseCurrentIndices = new int[36];
102     private int[] responseEnds = new int[36];
103     private String previousInput = null;
104 
ElizaResponder()105     public ElizaResponder() {
106         for (int i = 0; i < CONVERSATION_TO_RESPONSES_MAP.length / 2; i++) {
107             responseStarts[i] = CONVERSATION_TO_RESPONSES_MAP[2 * i];
108             responseCurrentIndices[i] = CONVERSATION_TO_RESPONSES_MAP[2 * i];
109             responseEnds[i] = responseStarts[i] + CONVERSATION_TO_RESPONSES_MAP[2 * i  + 1];
110         }
111     }
112 
elzTalk(String input)113     public String elzTalk(String input) {
114         if (null == input) {
115             input = "";
116         }
117         String result = "";
118 
119         input = " " + input.toUpperCase().replace("\'", "") + " ";
120 
121         if (previousInput != null && input.equals(previousInput)) {
122             return "DIDN'T YOU JUST SAY THAT?\n";
123         }
124         previousInput = input;
125 
126         int keywordIndex = 0;
127         for (; keywordIndex < CONVERSATION_KEYWORDS.length; ++keywordIndex) {
128             int index = input.indexOf(CONVERSATION_KEYWORDS[keywordIndex]);
129             if (index != -1) {
130                 break;
131             }
132         }
133 
134         String afterKeyword = "";
135         if (keywordIndex == CONVERSATION_KEYWORDS.length) {
136             keywordIndex = 35;
137         } else {
138             int index = input.indexOf(CONVERSATION_KEYWORDS[keywordIndex]);
139             afterKeyword = input.substring(index + CONVERSATION_KEYWORDS[keywordIndex].length());
140             String[] parts = afterKeyword.split("\\s+");
141             for (int i = 0; i < WORDS_TO_REPLACE.length / 2; i++) {
142                 String first = WORDS_TO_REPLACE[i * 2];
143                 String second = WORDS_TO_REPLACE[i * 2 + 1];
144                 for (int j = 0; j < parts.length; ++j) {
145                     if (parts[j].equals(first)) {
146                         parts[j] = second;
147                     } else if (parts[j].equals(second)) {
148                         parts[j] = first;
149                     }
150                 }
151             }
152             afterKeyword = TextUtils.join(" ", parts);
153         }
154 
155         String question = QUESTIONS[responseCurrentIndices[keywordIndex] - 1];
156         responseCurrentIndices[keywordIndex] = responseCurrentIndices[keywordIndex] + 1;
157         if (responseCurrentIndices[keywordIndex] > responseEnds[keywordIndex]) {
158             responseCurrentIndices[keywordIndex] = responseStarts[keywordIndex];
159         }
160         result += question;
161         if (result.endsWith("*")) {
162             result = result.substring(0, result.length() - 1);
163             result += " " + afterKeyword;
164         }
165 
166         return result;
167     }
168 }
169