啥都不说,直接上代码:
测试用例:
"数据库大富。科技阿道夫,打广告.s;;.大概电饭锅你好,小漫讯飞,.。,,,,,讯飞讯s飞讯飞"
命中词:你好小漫讯
// 关键词的字数3-5个
static Map<Integer, String> wakeKey = new HashMap<Integer, String>();
static {
wakeKey.put(1, "小飞小飞");
wakeKey.put(2, "你好小讯");
wakeKey.put(3, "讯飞讯飞");
wakeKey.put(4, "连接无线");
wakeKey.put(5, "打广告");
}
/**
* 匹配算法
*
* @param key
* @return
*/
public static String wakepu(String key) {
// 取出标点符号,注意中文和英文,进一步处理所有的标点符号
key = key.replaceAll("[\\pP‘’“”]", "");
// 分组
String substring = "";
for (int j = 3; j <= 5; j++) {// 分为三组
for (int i = 0; i < key.length(); i++) {
int beginIndex = i * 1;
int endIndex = beginIndex + j;
if (endIndex <= key.length()) {
substring = key.substring(beginIndex, endIndex);
} else {
substring = key.substring(beginIndex);
break;
}
System.out.println(substring);
if (wakeKey.containsValue(substring)) {
return substring;
}
}
}
return "failed";
}