Skip to content

Commit

Permalink
feat: add Jieba::has_word method
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu authored and messense committed Jan 21, 2025
1 parent 83e1d69 commit fb16d62
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@ impl Jieba {
freq
}

/// Checks if a word exists in the dictionary.
///
/// # Arguments
///
/// * `word` - The word to check.
///
/// # Returns
///
/// * `bool` - Whether the word exists in the dictionary.
pub fn has_word(&self, word: &str) -> bool {
self.cedar.exact_match_search(word).is_some()
}

/// Load dictionary
pub fn load_dict<R: BufRead>(&mut self, dict: &mut R) -> Result<(), Error> {
let mut buf = String::new();
Expand Down Expand Up @@ -797,6 +810,14 @@ mod tests {
let _ = Jieba::new();
}

#[test]
fn test_has_word() {
let jieba = Jieba::new();
assert!(jieba.has_word("中国"));
assert!(jieba.has_word("开源"));
assert!(!jieba.has_word("不存在的词"));
}

#[test]
fn test_split_matches() {
let re_han = &*RE_HAN_DEFAULT;
Expand Down

0 comments on commit fb16d62

Please sign in to comment.