package AXJava;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ElizaDeducer {
public static final Map<String, String> reflections;
public static final List<PhraseMatcher> babble2;
static {
HashMap<String, String> ref = new HashMap<String, String>();
ref.put("am", "are");
ref.put("was", "were");
ref.put("i", "you");
ref.put("i'd", "you would");
ref.put("i've", "you have");
ref.put("my", "your");
ref.put("are", "am");
ref.put("you've", "Ihave");
ref.put("you'll", "I will");
ref.put("your", "my");
ref.put("yours", "mine");
ref.put("you", "i");
ref.put("me", "you");
reflections = Collections.unmodifiableMap(ref);
ArrayList<PhraseMatcher> babbleTmp = new ArrayList<PhraseMatcher>();
ArrayList<AXKeyValuePair> kvs = new ArrayList<>();
kvs.add(new AXKeyValuePair("what is a {0}","{0} is a {1}"));
kvs.add(new AXKeyValuePair("explain {0}","{0} is a {1}"));
babbleTmp.add(new PhraseMatcher("(.*) is (.*)", kvs));
babble2 = Collections.unmodifiableList(babbleTmp);
}
public ElizaDeducer() {
}
public ArrayList<AXKeyValuePair> respond(String msg) {
for (PhraseMatcher pm : babble2) {
if (pm.matches(msg)) {
return pm.respond(msg.toLowerCase());
}
}
return new ArrayList<AXKeyValuePair>();
}
public static class PhraseMatcher {
public static final Random RND = new Random();
public final Pattern matcher;
public final List<AXKeyValuePair> responses;
public PhraseMatcher(String matcher, List<AXKeyValuePair> responses) {
this.matcher = Pattern.compile(matcher);
this.responses = responses;
}
public boolean matches(String str) {
Matcher m = matcher.matcher(str);
return m.matches();
}
public ArrayList<AXKeyValuePair> respond(String str) {
Matcher m = matcher.matcher(str);
m.find(); //TODO delete
ArrayList<AXKeyValuePair> result = new ArrayList<AXKeyValuePair>();
int tmp = m.groupCount();
for (AXKeyValuePair kv : this.responses) {
AXKeyValuePair tempKV= new AXKeyValuePair();
tempKV.setKey(kv.getKey());
tempKV.setValue(kv.getValue());
for (int i = 0; i < tmp; i++) {
String s = reflect(m.group(i + 1));
tempKV.setKey(tempKV.getKey().replace("{" + i + "}", s).toLowerCase());
tempKV.setValue(tempKV.getValue().replace("{" + i + "}", s).toLowerCase());
}
result.add(tempKV);
}
return result;
}
public static String reflect(String s) {
String[] sa = s.split(" ");
for (int i = 0; i < sa.length; i++) {
if (reflections.containsKey(sa[i])) {
sa[i] = reflections.get(sa[i]);
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sa.length; i++) {
if (i > 0) {
sb.append(" ");
}
sb.append(sa[i]);
}
return sb.toString();
}
@Override
public String toString() {
return matcher.pattern() + ":" + responses.toString();
}
}
}
package AXJava;
public class AXKeyValuePair {
private String key = "";
private String value = "";
public AXKeyValuePair() {
}
public AXKeyValuePair(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return key + ";" + value;
}
}
package AXJava;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ElizaDeducer {
public static final Map<String, String> reflections;
public static final List<PhraseMatcher> babble2;
static {
HashMap<String, String> ref = new HashMap<String, String>();
ref.put("am", "are");
ref.put("was", "were");
ref.put("i", "you");
ref.put("i'd", "you would");
ref.put("i've", "you have");
ref.put("my", "your");
ref.put("are", "am");
ref.put("you've", "Ihave");
ref.put("you'll", "I will");
ref.put("your", "my");
ref.put("yours", "mine");
ref.put("you", "i");
ref.put("me", "you");
reflections = Collections.unmodifiableMap(ref);
ArrayList<PhraseMatcher> babbleTmp = new ArrayList<PhraseMatcher>();
ArrayList<AXKeyValuePair> kvs = new ArrayList<>();
kvs.add(new AXKeyValuePair("what is a {0}","{0} is a {1}"));
kvs.add(new AXKeyValuePair("explain {0}","{0} is a {1}"));
babbleTmp.add(new PhraseMatcher("(.*) is (.*)", kvs));
babble2 = Collections.unmodifiableList(babbleTmp);
}
public ElizaDeducer() {
}
public ArrayList<AXKeyValuePair> respond(String msg) {
for (PhraseMatcher pm : babble2) {
if (pm.matches(msg)) {
return pm.respond(msg.toLowerCase());
}
}
return new ArrayList<AXKeyValuePair>();
}
public static class PhraseMatcher {
public static final Random RND = new Random();
public final Pattern matcher;
public final List<AXKeyValuePair> responses;
public PhraseMatcher(String matcher, List<AXKeyValuePair> responses) {
this.matcher = Pattern.compile(matcher);
this.responses = responses;
}
public boolean matches(String str) {
Matcher m = matcher.matcher(str);
return m.matches();
}
public ArrayList<AXKeyValuePair> respond(String str) {
Matcher m = matcher.matcher(str);
if (m.find()){}
ArrayList<AXKeyValuePair> result = new ArrayList<AXKeyValuePair>();
int tmp = m.groupCount();
for (AXKeyValuePair kv : this.responses) {
AXKeyValuePair tempKV= new AXKeyValuePair();
tempKV.setKey(kv.getKey());
tempKV.setValue(kv.getValue());
for (int i = 0; i < tmp; i++) {
String s = reflect(m.group(i + 1),i);
tempKV.setKey(tempKV.getKey().replace("{" + i + "}", s).toLowerCase());
tempKV.setValue(tempKV.getValue().replace("{" + i + "}", s).toLowerCase());
}
result.add(tempKV);
}
return result;
}
public static String reflect(String s, int index) {
String[] sa = s.split(" ");
if (reflections.containsKey(sa[index])) {
sa[index] = reflections.get(sa[index]);
}
return sa[index];
}
@Override
public String toString() {
return matcher.pattern() + ":" + responses.toString();
}
}
}
public class ElizaDeducer {
public Map<String, String> reflections;
public List<PhraseMatcher> babble2;
public HashMap<String, String> ref = new HashMap<String, String>();
public ElizaDeducer() {
}
public ArrayList<AXKeyValuePair> respond(String msg) {
for (PhraseMatcher pm : babble2) {
if (pm.matches(msg)) {
return pm.respond(msg.toLowerCase());
}
}
return new ArrayList<AXKeyValuePair>();
}
public class PhraseMatcher {
public final Pattern matcher;
public final List<AXKeyValuePair> responses;
public PhraseMatcher(String matcher, List<AXKeyValuePair> responses) {
this.matcher = Pattern.compile(matcher);
this.responses = responses;
}
public boolean matches(String str) {
Matcher m = matcher.matcher(str);
return m.matches();
}
public ArrayList<AXKeyValuePair> respond(String str) {
Matcher m = matcher.matcher(str);
if (m.find()){}
ArrayList<AXKeyValuePair> result = new ArrayList<AXKeyValuePair>();
int tmp = m.groupCount();
for (AXKeyValuePair kv : this.responses) {
AXKeyValuePair tempKV= new AXKeyValuePair(kv.getKey(),kv.getValue());
for (int i = 0; i < tmp; i++) {
String s = reflect(m.group(i + 1),i);
tempKV.setKey(tempKV.getKey().replace("{" + i + "}", s).toLowerCase());
tempKV.setValue(tempKV.getValue().replace("{" + i + "}", s).toLowerCase());
}
result.add(tempKV);
}
return result;
}
public String reflect(String s, int index) {
String[] sa = s.split(" ");
if (reflections.containsKey(sa[index])) {
sa[index] = reflections.get(sa[index]);
}
return sa[index];
}
}
}