Skip to main content

mecab_ko_dict/
lib.rs

1//! # mecab-ko-dict
2//!
3//! 한국어 형태소 사전 관리 라이브러리
4//!
5//! ## 주요 기능
6//!
7//! - 바이너리 사전 포맷 (v3.0)
8//! - FST 기반 형태소 검색
9//! - 연접 비용 매트릭스
10//! - 사전 빌더/컴파일러
11//!
12//! ## 예제
13//!
14//! ```rust,ignore
15//! use mecab_ko_dict::dictionary::SystemDictionary;
16//!
17//! let dict = SystemDictionary::load("path/to/dict").unwrap();
18//! let entries = dict.lookup("안녕");
19//! ```
20
21#![warn(missing_docs)]
22#![deny(unsafe_code)]
23#![cfg_attr(feature = "simd", feature(portable_simd))]
24
25pub mod dictionary;
26pub mod file_watcher;
27pub mod hot_reload;
28pub mod lazy_entries;
29pub mod loader;
30pub mod matrix;
31pub mod trie;
32pub mod user_dict;
33
34pub use dictionary::{DictEntry, DictionaryLoader, SystemDictionary};
35pub use error::{DictError, Result};
36pub use file_watcher::{FileEvent, FileWatcher, WatchConfig};
37pub use hot_reload::{
38    DeltaUpdate, DeltaUpdateBuilder, EntryChange, HotReloadDictionary, Version, VersionInfo,
39};
40pub use lazy_entries::LazyEntries;
41pub use loader::{LazyDictionary, LoaderConfig, MmapDictionary};
42pub use matrix::{ConnectionMatrix, DenseMatrix, Matrix, MatrixLoader, MmapMatrix, SparseMatrix};
43pub use trie::{DictionarySearcher, EntryIndex, PrefixMatch, Trie, TrieBuilder};
44pub use user_dict::{UserDictionary, UserDictionaryBuilder, UserEntry};
45
46/// 사전 엔트리
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct Entry {
49    /// 표면형
50    pub surface: String,
51    /// 좌문맥 ID
52    pub left_id: u16,
53    /// 우문맥 ID
54    pub right_id: u16,
55    /// 비용
56    pub cost: i16,
57    /// 품사 정보
58    pub feature: String,
59}
60
61/// 사전 인터페이스
62pub trait Dictionary {
63    /// 형태소 검색
64    fn lookup(&self, surface: &str) -> Vec<Entry>;
65
66    /// 연접 비용 조회
67    fn get_connection_cost(&self, left_id: u16, right_id: u16) -> i16;
68}
69
70/// 에러 모듈
71pub mod error {
72    use thiserror::Error;
73
74    /// 사전 에러 타입
75    #[derive(Error, Debug)]
76    pub enum DictError {
77        /// IO 에러
78        #[error("IO error: {0}")]
79        Io(#[from] std::io::Error),
80
81        /// 포맷 에러
82        #[error("Invalid dictionary format: {0}")]
83        Format(String),
84
85        /// 버전 불일치
86        #[error("Version mismatch: expected {expected}, found {found}")]
87        Version {
88            /// 예상 버전
89            expected: u32,
90            /// 실제 버전
91            found: u32,
92        },
93    }
94
95    /// Result 타입 별칭
96    pub type Result<T> = std::result::Result<T, DictError>;
97}
98
99/// 사전 포맷 모듈 (스텁)
100///
101/// 바이너리 사전 포맷 정의
102pub mod format {
103
104    /// 사전 헤더
105    pub struct Header {
106        /// 매직 넘버
107        pub magic: [u8; 4],
108        /// 버전
109        pub version: u32,
110        /// 엔트리 수
111        pub entry_count: u32,
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_entry_creation() {
121        let entry = Entry {
122            surface: "안녕".to_string(),
123            left_id: 1,
124            right_id: 1,
125            cost: 100,
126            feature: "NNG,*,T,안녕,*,*,*,*".to_string(),
127        };
128
129        assert_eq!(entry.surface, "안녕");
130    }
131}