91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

lucene 同義詞的索引

發布時間:2020-03-04 10:41:38 來源:網絡 閱讀:562 作者:mingyongyao_cto 欄目:開發技術
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();
        }
    }
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

含山县| 赣州市| 分宜县| 封丘县| 永修县| 南开区| 武功县| 洞口县| 曲阜市| 高青县| 竹溪县| 周至县| 岳西县| 横山县| 手游| 荣成市| 揭阳市| 南涧| 曲阳县| 句容市| 西林县| 云阳县| 凯里市| 宁强县| 华亭县| 乌苏市| 长宁县| 双牌县| 黔江区| 绥化市| 行唐县| 望谟县| 塔河县| 兴海县| 临泉县| 乐东| 万载县| 资讯| 富民县| 庆安县| 惠东县|