Skip to content

Commit

Permalink
make bots a bit smarter, handle natural language light
Browse files Browse the repository at this point in the history
  • Loading branch information
iamironrabbit committed Jun 28, 2018
1 parent a58d101 commit ab83b82
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 51 deletions.
78 changes: 37 additions & 41 deletions src/main/java/im/zom/ractive/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import im.zom.ractive.bots.SearchBot;
import im.zom.ractive.models.Buddy;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.PresenceEventListener;
Expand All @@ -16,6 +16,8 @@
import org.jivesoftware.smack.roster.RosterListener;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.chatstates.ChatState;
import org.jivesoftware.smackx.chatstates.ChatStateManager;
import org.jivesoftware.smackx.omemo.OmemoConfiguration;
import org.jivesoftware.smackx.omemo.OmemoFingerprint;
import org.jivesoftware.smackx.omemo.OmemoManager;
Expand Down Expand Up @@ -52,7 +54,7 @@
/**
* Created by shifar on 7/3/16.
*/
public class Main implements Runnable, IncomingChatMessageListener {
public class Main implements Runnable {

private static Map<BareJid, Buddy> buddyList;

Expand Down Expand Up @@ -178,7 +180,6 @@ public void presenceUnsubscribed(BareJid bareJid, Presence presence) {
System.out.println("Logged in as " + user);

mChatManager = ChatManager.getInstanceFor(mConnection);
mChatManager.addIncomingListener(this);

//Setting presence
final Presence presence = new Presence(Presence.Type.available);
Expand Down Expand Up @@ -216,7 +217,7 @@ public void onOmemoMessageReceived(String decryptedBody, Message encryptedMessag
Chat chat = chatList.get(from.asEntityBareJidIfPossible());
Message newMessage = encryptedMessage.clone();
newMessage.setBody(decryptedBody);
newIncomingMessage(from.asEntityBareJidIfPossible(),newMessage,chat);
handleMessage(from.asEntityBareJidIfPossible(),newMessage,chat);

}

Expand Down Expand Up @@ -277,29 +278,29 @@ public void buildSession (EntityBareJid source)
{
if (!buddyList.containsKey(source)) {

//Building a bot randomly
BasicBot bot = null;
if (mBotType.equalsIgnoreCase("rive"))
{
bot = new RiveBot(source.asBareJid().getLocalpartOrNull().toString(),mBotParam);
}
else if (mBotType.equalsIgnoreCase("search"))
{
String lang = "en";
String[] loginParts = mBotParam.split(":");
bot = new SearchBot(loginParts[0],loginParts[1],lang);
}

final Buddy newBuddy = new Buddy(source.asBareJid(), bot);
//Adding new buddy to the list
buddyList.put(source, newBuddy);
//Building a bot randomly
BasicBot bot = null;
if (mBotType.equalsIgnoreCase("rive")) {
bot = new RiveBot(source.asBareJid().getLocalpartOrNull().toString(), mBotParam);
} else if (mBotType.equalsIgnoreCase("search")) {
String lang = "en";
String[] loginParts = mBotParam.split(":");
bot = new SearchBot(loginParts[0], loginParts[1], lang);
}

System.out.println("Listening to " + source);
System.out.println("Buddylist size: " + buddyList.size());
final Buddy newBuddy = new Buddy(source.asBareJid(), bot);
//Adding new buddy to the list
buddyList.put(source, newBuddy);

//Setting listeners to the source
Chat chat = mChatManager.chatWith(source);
chatList.put(source, chat);
//Setting listeners to the source
Chat chat = mChatManager.createChat(source);
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
trustAllIdentities(message.getFrom().asBareJid());
}
});
chatList.put(source, chat);

try {
Presence presenced = new Presence(Presence.Type.subscribed);
Expand All @@ -310,20 +311,14 @@ else if (mBotType.equalsIgnoreCase("search"))
presence.setTo(source);
mConnection.sendStanza(presence);

String welcome = bot.getWelcomeMessage();
if (welcome != null)
sendMessage(source.asBareJid(),welcome);

} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (Exception e) {
e.printStackTrace();
}


mChatManager.addIncomingListener(Main.this);

trustAllIdentities(source.asBareJid());


}
}

}

Expand Down Expand Up @@ -370,13 +365,13 @@ public void sendMessage (Jid jid, String message) throws XMPPException, SmackExc

//send
if (encrypted != null) {
ChatManager.getInstanceFor(mConnection).chatWith(jid.asEntityBareJidIfPossible()).send(encrypted);
//ChatManager.getInstanceFor(mConnection).chatWith(jid.asEntityBareJidIfPossible()).send(encrypted);
chatList.get(jid.asEntityBareJidIfPossible()).sendMessage(encrypted);
}

}

@Override
public void newIncomingMessage(EntityBareJid entityBareJid, Message message, Chat chat) {
public void handleMessage(EntityBareJid entityBareJid, Message message, Chat chat) {

buildSession(message.getFrom().asEntityBareJidIfPossible());

Expand All @@ -391,6 +386,7 @@ public void newIncomingMessage(EntityBareJid entityBareJid, Message message, Cha

if (sourceBuddyMessage != null && sourceBuddyMessage.length() > 0) {

ChatStateManager.getInstance(mConnection).setCurrentState(ChatState.composing,chatList.get(entityBareJid));
handleBot (message.getFrom(), sourceBuddy.getBot(),sourceBuddyMessage);

}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/im/zom/ractive/bots/BasicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public abstract class BasicBot {

public abstract String getWhatYouThink(String whatBotThinks);

public abstract String getWelcomeMessage ();

}
5 changes: 5 additions & 0 deletions src/main/java/im/zom/ractive/bots/RiveBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public ArrayList<String> getWhatBotThinks(String sourceBuddyMessage) {
public String getWhatYouThink(String whatBotThinks) {
return null;
}

@Override
public String getWelcomeMessage() {
return null;
}
}
89 changes: 79 additions & 10 deletions src/main/java/im/zom/ractive/bots/SearchBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SearchBot extends BasicBot {
Expand All @@ -22,6 +24,9 @@ public class SearchBot extends BasicBot {

private final static int MAX_LENGTH = 1000;

private static final Pattern UNWANTED_SYMBOLS =
Pattern.compile("(?:--|[\\[\\]{}()+/\\\\])");

public SearchBot (String login, String pass, String lang)
{
mLang = lang;
Expand Down Expand Up @@ -73,23 +78,55 @@ else if (Character.UnicodeBlock.of(searchTerm.charAt(0))==Character.UnicodeBlock

Article article = wikiBot.getArticle(searchTerm);

if (article.getSimpleArticle().isRedirect())
String pageText = article.getText();

if (article.isRedirect() || pageText.startsWith("#REDIRECT"))
{
String redirect = article.getText().substring(12);
redirect = redirect.substring(0,redirect.length()-2);
article = wikiBot.getArticle(redirect);
if (pageText.length() > 12) {
pageText = pageText.substring(12);
pageText = pageText.substring(0, pageText.indexOf("]"));
article = wikiBot.getArticle(pageText);
pageText = article.getText();
}
}
else if (pageText == null || pageText.length() == 0)
{
searchTerm = longestWord(searchTerm);
searchTerm = searchTerm.replace("?", "");

try {
article = wikiBot.getArticle(searchTerm);
pageText = article.getText();

if (article.getSimpleArticle().isRedirect())
{
if (pageText.length() > 12) {
pageText = pageText.substring(12);
pageText = pageText.substring(0, pageText.length() - 2);
article = wikiBot.getArticle(pageText);
pageText = article.getText();
}
}
}

String plainStr = wikiModel.render(new PlainTextConverter(), article.getText());

if (plainStr.length() > MAX_LENGTH)
plainStr = plainStr.substring(0,MAX_LENGTH) + "...";
try {

plainStr = plainStr.replaceAll("(\\w+):([^\\n]+)","").replace("\n","");
String plainStr = wikiModel.render(new PlainTextConverter(), pageText);

resp.add(plainStr);
if (plainStr != null && plainStr.length() > 0) {
plainStr = plainStr.replaceAll("(\\w+):([^\\n]+)", " ").replace("\n", " ").trim();
if (plainStr.length() > MAX_LENGTH)
plainStr = plainStr.substring(0, MAX_LENGTH) + "...";

resp.add(plainStr);
}
else
{
if (mLang.equalsIgnoreCase("bo"))
resp.add("དགོངས་དག་ ང་ཚོས་ཚིག་དེའི་སྐོར་ཅི་ཡང་རྙེད་མ་སོང་།");
else
resp.add("Sorry, we could not find the work you were looking for.");
}

} catch (IOException e) {
e.printStackTrace();
Expand All @@ -103,4 +140,36 @@ else if (Character.UnicodeBlock.of(searchTerm.charAt(0))==Character.UnicodeBlock
public String getWhatYouThink(String whatBotThinks) {
return null;
}

@Override
public String getWelcomeMessage() {

String welcome = "Tashi Delek, I am Topgyal. Type in the word or subject you want me to search. For example, type \"rainforest\" to search for a wiki article on rainforest.\n\n";

welcome +=
"བཀྲ་ཤིས་བདེ་ལེགས། ཁྱེད་རང་གི་འཚོལ་བཤེར་བྱེད་འདོད་པའི་མིང་ཚིག་གམ་ཡང་ན།\n\n"+
"ཐ་སྙད་དེ་གཏགས་རོགས། དཔེར་ན། ནགས་ཚལ་ཞེས་གཏགས་ཏེ།\n\n" +
"ཝེ་ཁེ་རིག་མཛོད་ནང་དུ་ནགས་ཚལ་སྐོར་གྱི་རྩོམ་ཡིག་འཚོལ།";
return welcome;
}

private String longestWord (String sentence)
{
String [] word = sentence.split(" ");
String maxlethWord = "";

for(int i = 0; i < word.length; i++){
for (int j = 1; j < word.length ; j++) {
if(word[i].length() >= word[j].length()){
if (null == maxlethWord) {
maxlethWord = word[i];
} else if (maxlethWord.length() <= word[i].length()) {
maxlethWord = word[i];
}
}
}
}

return maxlethWord;
}
}

0 comments on commit ab83b82

Please sign in to comment.