com.google.code.chatterbotapi.Cleverbot Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chatter-bot-api Show documentation
Show all versions of chatter-bot-api Show documentation
A JAVA (also Mono/.NET, Python and PHP) chatter bot API
that supports Cleverbot, JabberWacky and Pandorabots.
package com.google.code.chatterbotapi;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
/*
chatter-bot-api
Copyright (C) 2011 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
class Cleverbot implements ChatterBot {
private final String baseUrl;
private final String serviceUrl;
private int endIndex;
public Cleverbot(String baseUrl, String serviceUrl, int endIndex) {
this.baseUrl = baseUrl;
this.serviceUrl = serviceUrl;
this.endIndex = endIndex;
}
@Override
public ChatterBotSession createSession(Locale... locales) {
return new Session(locales);
}
private class Session implements ChatterBotSession {
private final Map vars;
private final Map headers;
private final Map cookies;
public Session(Locale... locales) {
vars = new LinkedHashMap();
//vars.put("start", "y");
vars.put("stimulus", "");
vars.put("islearning", "1");
vars.put("icognoid", "wsf");
//vars.put("fno", "0");
//vars.put("sub", "Say");
//vars.put("cleanslate", "false");
headers = new LinkedHashMap();
if (locales.length > 0)
headers.put("Accept-Language", Utils.toAcceptLanguageTags(locales));
cookies = new LinkedHashMap();
try {
Utils.request(baseUrl, headers, cookies, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ChatterBotThought think(ChatterBotThought thought) throws Exception {
vars.put("stimulus", thought.getText());
String formData = Utils.parametersToWWWFormURLEncoded(vars);
String formDataToDigest = formData.substring(9, endIndex);
String formDataDigest = Utils.md5(formDataToDigest);
vars.put("icognocheck", formDataDigest);
String response = Utils.request(serviceUrl, headers, cookies, vars);
String[] responseValues = response.split("\r");
//vars.put("", Utils.stringAtIndex(responseValues, 0)); ??
vars.put("sessionid", Utils.stringAtIndex(responseValues, 1));
vars.put("logurl", Utils.stringAtIndex(responseValues, 2));
vars.put("vText8", Utils.stringAtIndex(responseValues, 3));
vars.put("vText7", Utils.stringAtIndex(responseValues, 4));
vars.put("vText6", Utils.stringAtIndex(responseValues, 5));
vars.put("vText5", Utils.stringAtIndex(responseValues, 6));
vars.put("vText4", Utils.stringAtIndex(responseValues, 7));
vars.put("vText3", Utils.stringAtIndex(responseValues, 8));
vars.put("vText2", Utils.stringAtIndex(responseValues, 9));
vars.put("prevref", Utils.stringAtIndex(responseValues, 10));
//vars.put("", Utils.stringAtIndex(responseValues, 11)); ??
// vars.put("emotionalhistory", Utils.stringAtIndex(responseValues, 12));
// vars.put("ttsLocMP3", Utils.stringAtIndex(responseValues, 13));
// vars.put("ttsLocTXT", Utils.stringAtIndex(responseValues, 14));
// vars.put("ttsLocTXT3", Utils.stringAtIndex(responseValues, 15));
// vars.put("ttsText", Utils.stringAtIndex(responseValues, 16));
// vars.put("lineRef", Utils.stringAtIndex(responseValues, 17));
// vars.put("lineURL", Utils.stringAtIndex(responseValues, 18));
// vars.put("linePOST", Utils.stringAtIndex(responseValues, 19));
// vars.put("lineChoices", Utils.stringAtIndex(responseValues, 20));
// vars.put("lineChoicesAbbrev", Utils.stringAtIndex(responseValues, 21));
// vars.put("typingData", Utils.stringAtIndex(responseValues, 22));
// vars.put("divert", Utils.stringAtIndex(responseValues, 23));
ChatterBotThought responseThought = new ChatterBotThought();
responseThought.setText(Utils.stringAtIndex(responseValues, 0));
return responseThought;
}
public String think(String text) throws Exception {
ChatterBotThought thought = new ChatterBotThought();
thought.setText(text);
return think(thought).getText();
}
}
}