啥都不說巧鸭,直接上代碼:
測試用例:
"數(shù)據(jù)庫大富÷槟恚科技阿道夫纲仍,打廣告.s;;.大概電飯鍋你好,小漫訊飛贸毕,.郑叠。,明棍,乡革,,,訊飛訊s飛訊飛"
命中詞:你好小漫訊
// 關(guān)鍵詞的字?jǐn)?shù)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) {
// 取出標(biāo)點符號,注意中文和英文,進(jìn)一步處理所有的標(biāo)點符號
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";
}