標籤:lucene 自訂 filter 同義字
前一篇文章 lucene分詞流程講解了分詞的一些流程,我們也對分詞流程有了一個初步的瞭解,知道一個分詞器由多個Tokenizer和TokenFilter組成,這篇文章講解的就是我們利用這兩個特性實現自己的一個簡單的同義字分詞器,不妥之處請大家指出
(一)分析
如何?同義字呢?比如重慶可以叫做山城,我們搜尋山城的時候也應該要搜尋要包含重慶這個單詞的文章。那麼我們就必須要瞭解lucene只怎麼處理我們的文檔了,上一篇說了在一篇文檔當中lucene給一段話賦予了3個類。這3個類記錄了每一個詞語的位移量,位置增量等等。lucene通過位置增量來判斷位置,那麼我們現在是否有自己的想法了呢。我們只要在相應的位置加上自己的同義字就ok了
(二)實現
那麼首先我們要實現一個自己的analazer,覆蓋tokenSTREAM方法(因為我們要自訂一個filter,stream流要改變)
package org.xiezhaodong.lucene;import java.io.Reader;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.WhitespaceTokenizer;import org.apache.lucene.util.Version;public class MySameAnalyzer extends Analyzer {@Overridepublic TokenStream tokenStream(String arg0, Reader arg1) {return new MySameFilter(new WhitespaceTokenizer(Version.LUCENE_35, arg1));//調用自己實現的filter,分詞器我們用空格分詞器就可以了}}
看我們的filter,主要看注釋,關鍵都在注釋裡面
package org.xiezhaodong.lucene;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Stack;import org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;import org.apache.lucene.util.AttributeSource;public class MySameFilter extends TokenFilter {private CharTermAttribute cta=null;//要得到詞彙就要得到這個,上一篇已經講到private Stack<String> wordStack=null;//存放同義字的stackprivate AttributeSource.State current=null;private PositionIncrementAttribute pia;//位置增量設定protected MySameFilter(TokenStream input) {super(input);wordStack=new Stack<String>();pia=this.addAttribute(PositionIncrementAttribute.class);//從tokenStream中得到這兩個關鍵類cta=this.addAttribute(CharTermAttribute.class);}@Overridepublic boolean incrementToken() throws IOException {while(wordStack.size()>0){String word=wordStack.pop();restoreState(current);//還原到上一個狀態cta.setEmpty();cta.append(word);pia.setPositionIncrement(0);//增量為0和原單詞相對相同return true;}if(!this.input.incrementToken())return false;if(isHaveSameWords(cta.toString())){//有同義字current=captureState();//捕獲目前狀態,上一節說了,可以捕獲當前流的狀態}return true;}//判斷該詞語是否有同義字,這裡我們假設這些同義字,我亂寫的public boolean isHaveSameWords(String word){Map<String, String[]> map=new HashMap<String, String[]>();map.put("how", new String[]{"what","which"});map.put("thank", new String[]{"like","love"});String[] sws=map.get(word);if(sws!=null){//存在同義字for(String s:sws){wordStack.push(s);}return true;}return false;}}
測試載入器類
public class AnalyzerUtils {public static void displayAllTokenInfo(String str, Analyzer a) {try {TokenStream tokenStream = a.tokenStream("content",new StringReader(str));PositionIncrementAttribute positionIncrementAttribute = tokenStream.addAttribute(PositionIncrementAttribute.class);OffsetAttribute oa = tokenStream.addAttribute(OffsetAttribute.class);CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);TypeAttribute ta = tokenStream.addAttribute(TypeAttribute.class);while(tokenStream.incrementToken()){//System.out.print(positionIncrementAttribute.getPositionIncrement());System.out.print(cta+"{"+oa.startOffset()+"-"+oa.endOffset()+"}");}System.out.println();} catch (Exception e) {e.printStackTrace();}}}
@Testpublic void test04(){//Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_35);//Analyzer analyzer2=new StopAnalyzer(Version.LUCENE_35);//Analyzer analyzer3=new SimpleAnalyzer(Version.LUCENE_35);//Analyzer analyzer4=new WhitespaceAnalyzer(Version.LUCENE_35);//Analyzer analyzer=new MyStopAnalyzer(new String[]{"I","you"});Analyzer analyzer=new MySameAnalyzer();String txt="how are you thank you";//AnalyzerUtils.displayToken(txt, analyzer);AnalyzerUtils.displayAllTokenInfo(txt, analyzer);}
輸出
how{0-3}which{0-3}what{0-3}are{4-7}you{8-11}thank{12-17}love{12-17}like{12-17}you{18-21}有沒有發現,同義字已經加進去了,而且他們的位移量和位置增量都是一樣的,然後我們搜尋what的時候就可以把這篇文檔搜尋出來了。具體就不示範了,下面有附件,自行下載試一試,當然除了實現同義字,我們只要知道了怎麼自訂自己的filter可tokenizer我們可以做很多的事情。
轉載請註明http://blog.csdn.net/a837199685/article/
附件連結http://pan.baidu.com/s/1o6n2O9k
lucene如何寫自己的同義字分詞器