pub struct Jieba { /* private fields */ }Expand description
Jieba segmentation
Implementations§
Source§impl Jieba
impl Jieba
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new instance with embed dict
Requires default-dict feature to be enabled.
Sourcepub fn with_dict<R: BufRead>(dict: &mut R) -> Result<Self, Error>
pub fn with_dict<R: BufRead>(dict: &mut R) -> Result<Self, Error>
Create a new instance with dict
Sourcepub fn load_default_dict(&mut self)
pub fn load_default_dict(&mut self)
Loads the default dictionary into the instance.
This method reads the default dictionary from a predefined byte slice (DEFAULT_DICT)
and loads it into the current instance using the load_dict method.
§Arguments
&mut self- Mutable reference to the current instance.
Requires default-dict feature to be enabled.
§Examples
use jieba_rs::Jieba;
let mut instance = Jieba::empty();
instance.load_default_dict(); // Loads the default dictionary into the instance
assert!(instance.has_word("我们"), "The word '我们' should be in the dictionary after loading the default dictionary");Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all data
This method performs the following actions:
- Clears the
recordslist, removing all entries. - Resets
cedarto a new instance ofCedar. - Sets
totalto 0, resetting the count.
§Arguments
&mut self- Mutable reference to the current instance.
§Examples
use jieba_rs::Jieba;
let mut instance = Jieba::new();
assert!(instance.has_word("我们"), "The word '我们' should be in the dictionary after loading the default dictionary");
instance.clear(); // clear all dict data
assert!(!instance.has_word("我们"), "The word '我们' should not be in the dictionary after clearing the dictionary");Sourcepub fn add_word(
&mut self,
word: &str,
freq: Option<usize>,
tag: Option<&str>,
) -> usize
pub fn add_word( &mut self, word: &str, freq: Option<usize>, tag: Option<&str>, ) -> usize
Add word to dict, return freq
freq: if None, will be given by suggest_freq
tag: if None, will be given ""
Sourcepub fn load_dict<R: BufRead>(&mut self, dict: &mut R) -> Result<(), Error>
pub fn load_dict<R: BufRead>(&mut self, dict: &mut R) -> Result<(), Error>
Loads a dictionary by adding entries to the existing dictionary rather than resetting it.
This function reads from a BufRead source, parsing each line as a dictionary entry. Each entry
is expected to contain a word, its frequency, and optionally a tag.
§Type Parameters
R: A type that implements theBufReadtrait, used for reading lines from the dictionary.
§Arguments
dict- A mutable reference to aBufReadsource containing the dictionary entries.
§Returns
Result<(), Error>- ReturnsOk(())if the dictionary is successfully loaded; otherwise, returns an error describing what went wrong.
§Errors
This function will return an error if:
- There is an issue reading from the provided
BufReadsource. - A line in the dictionary file contains invalid frequency data (not a valid integer).
Sourcepub fn suggest_freq(&self, segment: &str) -> usize
pub fn suggest_freq(&self, segment: &str) -> usize
Suggest word frequency to force the characters in a word to be joined or split.