2017-02-11 15 views
-2

오픈 소스 자바 자동화 라이브러리 (예 : org.apache.lucene.util.automaton 또는 dk.brics.automaton)를 사용하여 프리픽스 일치를위한 자동 완성 기능을 어떻게 만들 수 있습니까?접두사 일치를위한 오토 마톤

예 : "luc"또는 "luce"가 주어 졌을 때 일치하지만 "lucy"또는 "lucid dream"가 주어질 때 일치하지 않는 문자열 [ "lucene", "lucid"] 세트로 만든 자동 완성 ".

+0

이 어떻게 [트라이] (https를 정확히이다. org/wiki/Trie)가 작동합니다. 비슷한 아이디어를 사용하여 오토 마톤을 구성 할 수 있습니다. "end of input"문자의 사용은'$'와 같이 유용 할 수 있습니다. – Obicere

+0

Java에서 찾은 구현 (예 : PatriciaTrie)이 실제로지도이고 접두사와 연결된 값을 반환하지만 저는 익숙합니다. 난 그냥 접두사의 존재를 확인하고 싶습니다. – tukushan

답변

0

접두어 매칭은 수용하기위한 모든 상태를 설정하여 예를 org.apache.lucene.util.automaton 사용 가능하다 : //en.wikipedia :

String[] strings = new String[]{"lucene", "lucid dream"}; 
    final List<BytesRef> terms = new ArrayList<>(); 
    for(String s : strings) { 
     terms.add(new BytesRef(s)); 
    } 
    Collections.sort(terms); 
    final Automaton a = DaciukMihovAutomatonBuilder.build(terms); 

    for (int i = 0; i < a.getNumStates(); i++) { 
     a.setAccept(i, true); 
    }