正在显示
4 个修改的文件
包含
116 行增加
和
12 行删除
| @@ -8,6 +8,7 @@ import com.crossoverjie.cim.client.service.RouteRequest; | @@ -8,6 +8,7 @@ import com.crossoverjie.cim.client.service.RouteRequest; | ||
| 8 | import com.crossoverjie.cim.client.vo.req.GroupReqVO; | 8 | import com.crossoverjie.cim.client.vo.req.GroupReqVO; |
| 9 | import com.crossoverjie.cim.client.vo.req.P2PReqVO; | 9 | import com.crossoverjie.cim.client.vo.req.P2PReqVO; |
| 10 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; | 10 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; |
| 11 | +import com.crossoverjie.cim.common.data.construct.TrieTree; | ||
| 11 | import com.crossoverjie.cim.common.enums.SystemCommandEnumType; | 12 | import com.crossoverjie.cim.common.enums.SystemCommandEnumType; |
| 12 | import com.crossoverjie.cim.common.util.StringUtil; | 13 | import com.crossoverjie.cim.common.util.StringUtil; |
| 13 | import org.slf4j.Logger; | 14 | import org.slf4j.Logger; |
| @@ -146,6 +147,9 @@ public class MsgHandler implements MsgHandle { | @@ -146,6 +147,9 @@ public class MsgHandler implements MsgHandle { | ||
| 146 | //关闭 AI 模式 | 147 | //关闭 AI 模式 |
| 147 | aiModel = false ; | 148 | aiModel = false ; |
| 148 | System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m"); | 149 | System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m"); |
| 150 | + }else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")){ | ||
| 151 | + //模糊匹配 | ||
| 152 | + prefixSearch(msg); | ||
| 149 | }else { | 153 | }else { |
| 150 | printAllCommand(allStatusCode); | 154 | printAllCommand(allStatusCode); |
| 151 | } | 155 | } |
| @@ -159,6 +163,33 @@ public class MsgHandler implements MsgHandle { | @@ -159,6 +163,33 @@ public class MsgHandler implements MsgHandle { | ||
| 159 | 163 | ||
| 160 | } | 164 | } |
| 161 | 165 | ||
| 166 | + | ||
| 167 | + /** | ||
| 168 | + * 模糊匹配 | ||
| 169 | + * @param msg | ||
| 170 | + */ | ||
| 171 | + private void prefixSearch(String msg) { | ||
| 172 | + try { | ||
| 173 | + List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers(); | ||
| 174 | + TrieTree trieTree = new TrieTree() ; | ||
| 175 | + for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) { | ||
| 176 | + trieTree.insert(onlineUser.getUserName()); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + String[] split = msg.split(" "); | ||
| 180 | + String key = split[1]; | ||
| 181 | + List<String> list = trieTree.prefixSearch(key); | ||
| 182 | + | ||
| 183 | + for (String res : list) { | ||
| 184 | + res = res.replace(key, "\033[31;4m" + key + "\033[0m"); | ||
| 185 | + System.out.println(res); | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + } catch (Exception e) { | ||
| 189 | + LOGGER.error("Exception" ,e); | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | + | ||
| 162 | /** | 193 | /** |
| 163 | * 查询聊天记录 | 194 | * 查询聊天记录 |
| 164 | * @param msg | 195 | * @param msg |
| @@ -14,6 +14,9 @@ import java.util.List; | @@ -14,6 +14,9 @@ import java.util.List; | ||
| 14 | */ | 14 | */ |
| 15 | public class TrieTree { | 15 | public class TrieTree { |
| 16 | 16 | ||
| 17 | + /** | ||
| 18 | + * 大小写都可保存 | ||
| 19 | + */ | ||
| 17 | private static final int CHILDREN_LENGTH = 26 * 2; | 20 | private static final int CHILDREN_LENGTH = 26 * 2; |
| 18 | private static final int MAX_CHAR_LENGTH = 16; | 21 | private static final int MAX_CHAR_LENGTH = 16; |
| 19 | 22 | ||
| @@ -43,7 +46,7 @@ public class TrieTree { | @@ -43,7 +46,7 @@ public class TrieTree { | ||
| 43 | char[] chars = data.toCharArray(); | 46 | char[] chars = data.toCharArray(); |
| 44 | for (int i = 0; i < chars.length; i++) { | 47 | for (int i = 0; i < chars.length; i++) { |
| 45 | char aChar = chars[i]; | 48 | char aChar = chars[i]; |
| 46 | - int index ; | 49 | + int index; |
| 47 | if (Character.isUpperCase(aChar)) { | 50 | if (Character.isUpperCase(aChar)) { |
| 48 | index = aChar - UPPERCASE_STAR; | 51 | index = aChar - UPPERCASE_STAR; |
| 49 | } else { | 52 | } else { |
| @@ -105,7 +108,7 @@ public class TrieTree { | @@ -105,7 +108,7 @@ public class TrieTree { | ||
| 105 | if (child.isEnd && key == null) { | 108 | if (child.isEnd && key == null) { |
| 106 | value.add(result); | 109 | value.add(result); |
| 107 | } | 110 | } |
| 108 | - if (key != null) { | 111 | + if (StringUtil.isNotEmpty(key)) { |
| 109 | char ca = key.charAt(0); | 112 | char ca = key.charAt(0); |
| 110 | 113 | ||
| 111 | int index; | 114 | int index; |
| @@ -121,17 +124,19 @@ public class TrieTree { | @@ -121,17 +124,19 @@ public class TrieTree { | ||
| 121 | } | 124 | } |
| 122 | } else { | 125 | } else { |
| 123 | for (int i = 0; i < CHILDREN_LENGTH; i++) { | 126 | for (int i = 0; i < CHILDREN_LENGTH; i++) { |
| 124 | - if (child.children[i] != null) { | ||
| 125 | - int j; | ||
| 126 | - if (Character.isUpperCase(child.children[i].data)) { | ||
| 127 | - j = UPPERCASE_STAR + i; | ||
| 128 | - } else { | ||
| 129 | - j = LOWERCASE_STAR + i; | ||
| 130 | - } | 127 | + if (child.children[i] == null) { |
| 128 | + continue; | ||
| 129 | + } | ||
| 131 | 130 | ||
| 132 | - char temp = (char) j; | ||
| 133 | - query(child.children[i], value, null, result + temp); | 131 | + int j; |
| 132 | + if (Character.isUpperCase(child.children[i].data)) { | ||
| 133 | + j = UPPERCASE_STAR + i; | ||
| 134 | + } else { | ||
| 135 | + j = LOWERCASE_STAR + i; | ||
| 134 | } | 136 | } |
| 137 | + | ||
| 138 | + char temp = (char) j; | ||
| 139 | + query(child.children[i], value, null, result + temp); | ||
| 135 | } | 140 | } |
| 136 | } | 141 | } |
| 137 | 142 |
| @@ -19,7 +19,8 @@ public enum SystemCommandEnumType { | @@ -19,7 +19,8 @@ public enum SystemCommandEnumType { | ||
| 19 | QUIT(":q! ","退出程序"), | 19 | QUIT(":q! ","退出程序"), |
| 20 | QUERY(":q ","【:q 关键字】查询聊天记录"), | 20 | QUERY(":q ","【:q 关键字】查询聊天记录"), |
| 21 | AI(":ai ","开启 AI 模式"), | 21 | AI(":ai ","开启 AI 模式"), |
| 22 | - QAI(":qai ","关闭 AI 模式") | 22 | + QAI(":qai ","关闭 AI 模式"), |
| 23 | + PREFIX(":pu ","模糊匹配用户") | ||
| 23 | 24 | ||
| 24 | ; | 25 | ; |
| 25 | 26 |
| @@ -129,4 +129,71 @@ public class TrieTreeTest { | @@ -129,4 +129,71 @@ public class TrieTreeTest { | ||
| 129 | } | 129 | } |
| 130 | Assert.assertTrue(result.equals("CDa,CDfff,")); | 130 | Assert.assertTrue(result.equals("CDa,CDfff,")); |
| 131 | } | 131 | } |
| 132 | + | ||
| 133 | + @Test | ||
| 134 | + public void prefixSearch7() throws Exception { | ||
| 135 | + TrieTree trieTree = new TrieTree(); | ||
| 136 | + trieTree.insert("Cde"); | ||
| 137 | + trieTree.insert("CDa"); | ||
| 138 | + trieTree.insert("ABe"); | ||
| 139 | + trieTree.insert("CDfff"); | ||
| 140 | + trieTree.insert("Cdfff"); | ||
| 141 | + | ||
| 142 | + List<String> ab = trieTree.prefixSearch(""); | ||
| 143 | + String result = ""; | ||
| 144 | + for (String s : ab) { | ||
| 145 | + result += s + "," ; | ||
| 146 | + System.out.println(s); | ||
| 147 | + } | ||
| 148 | + Assert.assertTrue(result.equals("")); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + @Test | ||
| 152 | + public void prefixSearch8() throws Exception { | ||
| 153 | + TrieTree trieTree = new TrieTree(); | ||
| 154 | + | ||
| 155 | + List<String> ab = trieTree.prefixSearch(""); | ||
| 156 | + String result = ""; | ||
| 157 | + for (String s : ab) { | ||
| 158 | + result += s + "," ; | ||
| 159 | + System.out.println(s); | ||
| 160 | + } | ||
| 161 | + Assert.assertTrue(result.equals("")); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + @Test | ||
| 166 | + public void prefixSearch9() throws Exception { | ||
| 167 | + TrieTree trieTree = new TrieTree(); | ||
| 168 | + trieTree.insert("Cde"); | ||
| 169 | + trieTree.insert("CDa"); | ||
| 170 | + trieTree.insert("ABe"); | ||
| 171 | + trieTree.insert("CDfff"); | ||
| 172 | + trieTree.insert("Cdfff"); | ||
| 173 | + | ||
| 174 | + List<String> ab = trieTree.prefixSearch("CDFD"); | ||
| 175 | + String result = ""; | ||
| 176 | + for (String s : ab) { | ||
| 177 | + result += s + "," ; | ||
| 178 | + System.out.println(s); | ||
| 179 | + } | ||
| 180 | + Assert.assertTrue(result.equals("")); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + | ||
| 184 | + @Test | ||
| 185 | + public void prefixSearch10() throws Exception { | ||
| 186 | + TrieTree trieTree = new TrieTree(); | ||
| 187 | + trieTree.insert("crossoverJie"); | ||
| 188 | + trieTree.insert("zhangsan"); | ||
| 189 | + | ||
| 190 | + List<String> ab = trieTree.prefixSearch("c"); | ||
| 191 | + String result = ""; | ||
| 192 | + for (String s : ab) { | ||
| 193 | + result += s + "," ; | ||
| 194 | + System.out.println(s); | ||
| 195 | + } | ||
| 196 | + Assert.assertTrue(result.equals("crossoverJie,")); | ||
| 197 | + } | ||
| 198 | + | ||
| 132 | } | 199 | } |
-
请 注册 或 登录 后发表评论