java字典樹(Trie)實現中文模糊匹配
阿新 • • 發佈:2018-12-16
原理解釋:
java實現:https://blog.csdn.net/yuhk231/article/details/51539840
c實現:https://blog.csdn.net/qq_31175231/article/details/77827324
程式碼模板:缺點,只能檢索出在一個分支中的字首匹配內容
package com.xq.algorithm; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; /* * 無資料結構設計下的蠻力中文鍵樹 */ class TrieNode { public String value; public ArrayList<TrieNode> ptr = null; public TrieNode(String value) { this.value=value; ptr =new ArrayList<TrieNode>(); } } public class TrieTree_1 { private static TrieNode root = null; ArrayList<String> searchResult=new ArrayList<String>(); StringBuffer tempWord=new StringBuffer(); int start=0; public TrieTree_1() { root = new TrieNode(null); } public void insert(String key) { TrieNode p = root; String tempWord; boolean contains; TrieNode tempNode; for (int i = 0; i < key.length(); i++) { tempWord=String.valueOf(key.charAt(i)); contains=false; for(TrieNode tn:p.ptr){ if(tn.value.equals(tempWord)){ p=tn; contains=true; break; } } if(!contains){ tempNode=new TrieNode(tempWord); p.ptr.add(tempNode); p=tempNode; } } } public ArrayList<String> search(String key) { //模糊查詢就是這個方法,打個比方比如key是"ap",那麼ArrayList裡就有{"apple","application"} TrieNode p = root; String temp; boolean contains=false; for (int i = 0; i < key.length(); i++) { temp=String.valueOf(key.charAt(i)); contains=false; for(TrieNode tn:p.ptr){ if(tn.value.equals(temp)){ p=tn; contains=true; break; } } if(contains){ continue; }else{ break; } } if(contains){ if(!(p.ptr.isEmpty())){ //查詢到關鍵字 searchResult.clear(); tempWord.delete(0, tempWord.length()); tempWord.append(key); start=key.length(); traverseTree(p); }else{ //已經查詢到鍵樹的底部 return null; } }else{ //沒有查詢到相應關鍵字 return null; } return searchResult; } private void traverseTree(TrieNode p){ if(!(p.ptr.isEmpty())){ for(TrieNode tn:p.ptr){ tempWord.append(tn.value); start++; traverseTree(tn); start--; tempWord.delete(start,tempWord.length()); } }else{ searchResult.add(tempWord.toString()); } } public static void main(String[] args) { TrieTree_1 chinese = new TrieTree_1(); chinese.insert("中"); chinese.insert("中國人"); chinese.insert("中國"); chinese.insert("中華人民"); chinese.insert("中華人崛起"); chinese.insert("中華上下五千年"); ArrayList<String> list = chinese.search("中華"); for (String string : list) { System.out.println(string); } } }