作者 crossoverJie

:sparkles: Introducing new features.用户名模糊匹配查询

... ... @@ -8,6 +8,7 @@ import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
import com.crossoverjie.cim.common.data.construct.TrieTree;
import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
... ... @@ -146,6 +147,9 @@ public class MsgHandler implements MsgHandle {
//关闭 AI 模式
aiModel = false ;
System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
}else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")){
//模糊匹配
prefixSearch(msg);
}else {
printAllCommand(allStatusCode);
}
... ... @@ -159,6 +163,33 @@ public class MsgHandler implements MsgHandle {
}
/**
* 模糊匹配
* @param msg
*/
private void prefixSearch(String msg) {
try {
List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
TrieTree trieTree = new TrieTree() ;
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
trieTree.insert(onlineUser.getUserName());
}
String[] split = msg.split(" ");
String key = split[1];
List<String> list = trieTree.prefixSearch(key);
for (String res : list) {
res = res.replace(key, "\033[31;4m" + key + "\033[0m");
System.out.println(res);
}
} catch (Exception e) {
LOGGER.error("Exception" ,e);
}
}
/**
* 查询聊天记录
* @param msg
... ...
... ... @@ -14,6 +14,9 @@ import java.util.List;
*/
public class TrieTree {
/**
* 大小写都可保存
*/
private static final int CHILDREN_LENGTH = 26 * 2;
private static final int MAX_CHAR_LENGTH = 16;
... ... @@ -43,7 +46,7 @@ public class TrieTree {
char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
int index ;
int index;
if (Character.isUpperCase(aChar)) {
index = aChar - UPPERCASE_STAR;
} else {
... ... @@ -105,7 +108,7 @@ public class TrieTree {
if (child.isEnd && key == null) {
value.add(result);
}
if (key != null) {
if (StringUtil.isNotEmpty(key)) {
char ca = key.charAt(0);
int index;
... ... @@ -121,17 +124,19 @@ public class TrieTree {
}
} else {
for (int i = 0; i < CHILDREN_LENGTH; i++) {
if (child.children[i] != null) {
int j;
if (Character.isUpperCase(child.children[i].data)) {
j = UPPERCASE_STAR + i;
} else {
j = LOWERCASE_STAR + i;
}
if (child.children[i] == null) {
continue;
}
char temp = (char) j;
query(child.children[i], value, null, result + temp);
int j;
if (Character.isUpperCase(child.children[i].data)) {
j = UPPERCASE_STAR + i;
} else {
j = LOWERCASE_STAR + i;
}
char temp = (char) j;
query(child.children[i], value, null, result + temp);
}
}
... ...
... ... @@ -19,7 +19,8 @@ public enum SystemCommandEnumType {
QUIT(":q! ","退出程序"),
QUERY(":q ","【:q 关键字】查询聊天记录"),
AI(":ai ","开启 AI 模式"),
QAI(":qai ","关闭 AI 模式")
QAI(":qai ","关闭 AI 模式"),
PREFIX(":pu ","模糊匹配用户")
;
... ...
... ... @@ -129,4 +129,71 @@ public class TrieTreeTest {
}
Assert.assertTrue(result.equals("CDa,CDfff,"));
}
@Test
public void prefixSearch7() throws Exception {
TrieTree trieTree = new TrieTree();
trieTree.insert("Cde");
trieTree.insert("CDa");
trieTree.insert("ABe");
trieTree.insert("CDfff");
trieTree.insert("Cdfff");
List<String> ab = trieTree.prefixSearch("");
String result = "";
for (String s : ab) {
result += s + "," ;
System.out.println(s);
}
Assert.assertTrue(result.equals(""));
}
@Test
public void prefixSearch8() throws Exception {
TrieTree trieTree = new TrieTree();
List<String> ab = trieTree.prefixSearch("");
String result = "";
for (String s : ab) {
result += s + "," ;
System.out.println(s);
}
Assert.assertTrue(result.equals(""));
}
@Test
public void prefixSearch9() throws Exception {
TrieTree trieTree = new TrieTree();
trieTree.insert("Cde");
trieTree.insert("CDa");
trieTree.insert("ABe");
trieTree.insert("CDfff");
trieTree.insert("Cdfff");
List<String> ab = trieTree.prefixSearch("CDFD");
String result = "";
for (String s : ab) {
result += s + "," ;
System.out.println(s);
}
Assert.assertTrue(result.equals(""));
}
@Test
public void prefixSearch10() throws Exception {
TrieTree trieTree = new TrieTree();
trieTree.insert("crossoverJie");
trieTree.insert("zhangsan");
List<String> ab = trieTree.prefixSearch("c");
String result = "";
for (String s : ab) {
result += s + "," ;
System.out.println(s);
}
Assert.assertTrue(result.equals("crossoverJie,"));
}
}
\ No newline at end of file
... ...