사전
Gukhanmun은 하나 이상의 HanjaDictionary 구현에서 한자 독음을 찾습니다.
gukhanmun 크레이트는 FST와 CDB 백엔드, 그리고 내장 《표준국어대사전》과 함께
배포됩니다.
내장 사전 사용
stdict 피처가 켜져 있으면(기본) 내장 사전이 자동으로 포함됩니다. 이를 끄려면:
let converter = Builder::with_preset(Preset::KoKr)
.no_bundled_stdict()
.build()?;
no_bundled_stdict를 호출한 뒤 다시 명시적으로 켜려면:
builder.bundled_stdict();
파일에서 사전 불러오기
fst나 cdb 피처가 필요합니다.
use gukhanmun::FstDictionary; // 또는 CdbDictionary
let dict = FstDictionary::open("custom.gukfst")?;
let converter = Builder::with_preset(Preset::KoKr)
.push_dictionary(dict)
.build()?;
push_dictionary로 추가한 사전은 내장 사전보다 먼저 참조됩니다. 연쇄 전체에서
처음 일치한 것이 채택됩니다.
zero-copy 정적 사전
include_bytes!로 사전을 바이너리에 직접 내장하여 파일 입출력 없이 불러옵니다:
use gukhanmun::FstDictionary;
static MY_DICT: &[u8] = include_bytes!("../data/custom.gukfst");
let dict = FstDictionary::from_static_bytes(MY_DICT)?;
from_static_bytes는 데이터를 복사하지 않습니다; 정적 슬라이스에 기댄 zero-copy
뷰를 만듭니다.
소유된 바이트에서 불러오기
바이트가 런타임 출처(네트워크, 데이터베이스 등)에서 올 때는 Arc<[u8]>로
감쌉니다:
use std::sync::Arc;
use gukhanmun::CdbDictionary;
let bytes: Vec<u8> = std::fs::read("custom.gukcdb")?;
let dict = CdbDictionary::from_bytes(Arc::from(bytes.as_slice()))?;
여러 사전 연결
ChainDictionary는 여러 사전을 명시적 우선순위로 결합하게 해 줍니다. 연쇄에서
일치를 가진 첫 사전이 채택됩니다:
use gukhanmun::{ChainDictionary, FstDictionary, CdbDictionary};
let domain_dict = FstDictionary::open("legal.gukfst")?;
let names_dict = CdbDictionary::open("names.gukcdb")?;
let chain = ChainDictionary::new(vec![
Box::new(domain_dict),
Box::new(names_dict),
]);
let converter = Builder::with_preset(Preset::KoKr)
.no_bundled_stdict()
.push_boxed_dictionary(Box::new(chain))
.build()?;
대안으로, push_dictionary를 여러 번 호출합니다; 사전은 push된 순서대로, 내장
사전보다 먼저 탐색됩니다.
사용자 정의 사전 구축
위에서 불러온 .gukfst와 .gukcdb 파일은 컴파일된 산출물이며,
gukhanmun-mkdict 도구로 순수 텍스트 표에서 빌드됩니다. 사전 출처를 작성하고
컴파일하는 방법은 CLI 안내서의
〈사용자 정의 사전 구축〉을
참조하십시오.