public interface SynonymEngine {
String[] getSynonyms(String key);
}
public class SynonymEngineImpl implements SynonymEngine {
private static HashMap<String,String[]> map = new HashMap<String ,String[]>();
static {
map.put("quick",new String[]{"fast","speedy"});
map.put("jumps",new String[]{"leaps","hops"});
map.put("over",new String[]{"above"});
map.put("lazy",new String[]{"apathetic","sluggish"});
map.put("dog",new String[]{"canine","pooch"});
}
@Override
public String[] getSynonyms(String key) {
// TODO Auto-generated method stub
return map.get(key);
}
}
public class SynonymFilter extends TokenFilter {
private SynonymEngine engine;
private CharTermAttribute ct;
private PositionIncrementAttribute pt;
private Stack<String> stack;
private AttributeSource.State current;
protected SynonymFilter(TokenStream input,SynonymEngine engine) {
super(input);
this.engine = engine;
ct = this.addAttribute(CharTermAttribute.class);
pt = this.addAttribute(PositionIncrementAttribute.class);
stack = new Stack<String>();
}
@Override
public boolean incrementToken() throws IOException {
if(stack.size()>0) {
this.restoreState(current);
String p = stack.pop();
ct.setEmpty();
ct.append(p);
pt.setPositionIncrement(0);
return true;
}
System.out.println("++++++"+ct);
if(!input.incrementToken()) return false;
System.out.println("------"+ct);
if(addSynonym(ct.toString())) {
current = this.captureState();
}
return true;
}
private boolean addSynonym(String name) {
String[] sa = engine.getSynonyms(name);
if(sa != null && sa.length>0) {
for(String s:sa) {
stack.push(s);
}
return true;
} else {
return false;
}
}
}
public class SynonymAnalyzer extends Analyzer {
private SynonymEngine engine;
public SynonymAnalyzer(SynonymEngine engine) {
this.engine = engine;
}
@Override
public TokenStream tokenStream(String s, Reader reader) {
// TODO Auto-generated method stub
return new SynonymFilter(new StopFilter(Version.LUCENE_35,
new LowerCaseFilter(Version.LUCENE_35,
new StandardFilter(Version.LUCENE_35,
new StandardTokenizer(Version.LUCENE_35,reader)))
,StopAnalyzer.ENGLISH_STOP_WORDS_SET),engine);
}
}
public class TestSynonym {
private RAMDirectory directory;
@Test
public void init() {
directory = new RAMDirectory();
SynonymEngine engine = new SynonymEngineImpl();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35,new SynonymAnalyzer(engine));
String content = "The quick brown fox jumps over the lazy dog";
try {
IndexWriter writer = new IndexWriter(directory,config);
Document doc = new Document();
doc.add(new Field("content",content,Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
writer.close();
IndexReader reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(new TermQuery(new Term("content","pooch")),10);
for(ScoreDoc sd:docs.scoreDocs) {
Document d = searcher.doc(sd.doc);
System.out.println(d.get("content"));
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司
創(chuàng)新互聯(lián)公司十年經(jīng)驗(yàn)成就非凡,專業(yè)從事
成都網(wǎng)站建設(shè)、成都做網(wǎng)站,成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)頁制作,
軟文發(fā)布平臺,
一元廣告等。十年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來電!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:lucene同義詞的索引-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article4/idgoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、電子商務(wù)、定制開發(fā)、服務(wù)器托管、搜索引擎優(yōu)化
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源:
創(chuàng)新互聯(lián)