From ab83b8296df133fa6e1512e6d3d592cd5ab4dffb Mon Sep 17 00:00:00 2001 From: ironrabbit Date: Thu, 28 Jun 2018 15:31:28 -0400 Subject: [PATCH] make bots a bit smarter, handle natural language light --- src/main/java/im/zom/ractive/Main.java | 78 ++++++++-------- .../java/im/zom/ractive/bots/BasicBot.java | 2 + .../java/im/zom/ractive/bots/RiveBot.java | 5 ++ .../java/im/zom/ractive/bots/SearchBot.java | 89 ++++++++++++++++--- 4 files changed, 123 insertions(+), 51 deletions(-) diff --git a/src/main/java/im/zom/ractive/Main.java b/src/main/java/im/zom/ractive/Main.java index 76dc07f..496e889 100644 --- a/src/main/java/im/zom/ractive/Main.java +++ b/src/main/java/im/zom/ractive/Main.java @@ -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; @@ -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; @@ -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 buddyList; @@ -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); @@ -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); } @@ -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); @@ -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()); - - - } + } } @@ -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()); @@ -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); } diff --git a/src/main/java/im/zom/ractive/bots/BasicBot.java b/src/main/java/im/zom/ractive/bots/BasicBot.java index 539528b..525dd4b 100644 --- a/src/main/java/im/zom/ractive/bots/BasicBot.java +++ b/src/main/java/im/zom/ractive/bots/BasicBot.java @@ -13,4 +13,6 @@ public abstract class BasicBot { public abstract String getWhatYouThink(String whatBotThinks); + public abstract String getWelcomeMessage (); + } diff --git a/src/main/java/im/zom/ractive/bots/RiveBot.java b/src/main/java/im/zom/ractive/bots/RiveBot.java index 83fcc09..611349a 100644 --- a/src/main/java/im/zom/ractive/bots/RiveBot.java +++ b/src/main/java/im/zom/ractive/bots/RiveBot.java @@ -50,4 +50,9 @@ public ArrayList getWhatBotThinks(String sourceBuddyMessage) { public String getWhatYouThink(String whatBotThinks) { return null; } + + @Override + public String getWelcomeMessage() { + return null; + } } diff --git a/src/main/java/im/zom/ractive/bots/SearchBot.java b/src/main/java/im/zom/ractive/bots/SearchBot.java index f10c350..eadc000 100644 --- a/src/main/java/im/zom/ractive/bots/SearchBot.java +++ b/src/main/java/im/zom/ractive/bots/SearchBot.java @@ -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 { @@ -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; @@ -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(); @@ -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; + } }