Skip to main content

Segment

Struct Segment 

Source
pub struct Segment { /* private fields */ }
Expand description

Chinese word segmenter.

Implementations§

Source§

impl Segment

Source

pub fn new(options: SegmentOptions) -> Self

Examples found in repository?
examples/basic.rs (lines 4-7)
3fn main() -> Result<(), novel_segment::Error> {
4    let mut seg = Segment::new(novel_segment::SegmentOptions {
5        auto_cjk: true,
6        ..Default::default()
7    });
8    seg.use_default()?;
9    let text = "这是一个基于Rust的中文分词模块。";
10    let words = seg.do_segment(text, DoSegmentOptions::default());
11    println!("{text}");
12    println!(
13        "{}",
14        words
15            .iter()
16            .map(|w| format!("{}:{:x}", w.w, w.p.unwrap_or(0)))
17            .collect::<Vec<_>>()
18            .join(" / ")
19    );
20
21    let simple = seg.do_segment_simple(text, DoSegmentOptions::default());
22    println!("{}", simple.join(" / "));
23
24    seg.add_word("專有名詞", Some(novel_segment::POSTAG::D_N), Some(100.0))?;
25    Ok(())
26}
Source

pub fn with_default() -> Result<Self>

new Segment(); useDefault()auto_cjk stays false.

Source

pub fn with_novel_default() -> Result<Self>

Test/CLI profile: autoCjk, all_mod, convertSynonym.

Source

pub fn with_node_novel_default() -> Result<Self>

word.novel.test.ts: createSegment then wipe SYNONYM and reload with nodeNovelMode.

Source

pub fn use_default(&mut self) -> Result<&mut Self>

Examples found in repository?
examples/basic.rs (line 8)
3fn main() -> Result<(), novel_segment::Error> {
4    let mut seg = Segment::new(novel_segment::SegmentOptions {
5        auto_cjk: true,
6        ..Default::default()
7    });
8    seg.use_default()?;
9    let text = "这是一个基于Rust的中文分词模块。";
10    let words = seg.do_segment(text, DoSegmentOptions::default());
11    println!("{text}");
12    println!(
13        "{}",
14        words
15            .iter()
16            .map(|w| format!("{}:{:x}", w.w, w.p.unwrap_or(0)))
17            .collect::<Vec<_>>()
18            .join(" / ")
19    );
20
21    let simple = seg.do_segment_simple(text, DoSegmentOptions::default());
22    println!("{}", simple.join(" / "));
23
24    seg.add_word("專有名詞", Some(novel_segment::POSTAG::D_N), Some(100.0))?;
25    Ok(())
26}
Source

pub fn use_default_modules(&mut self) -> &mut Self

Source

pub fn use_tokenizer(&mut self, tok: Box<dyn Tokenizer>) -> &mut Self

Source

pub fn use_optimizer(&mut self, opt: Box<dyn Optimizer>) -> &mut Self

Source

pub fn use_default_dicts(&mut self) -> Result<&mut Self>

Source

pub fn load_dict(&mut self, name: &str) -> Result<&mut Self>

Source

pub fn load_dict_file(&mut self, path: impl AsRef<Path>) -> Result<&mut Self>

Source

pub fn load_synonym_dict( &mut self, name: &str, skip_exists: bool, ) -> Result<&mut Self>

Source

pub fn clear_synonym_dict(&mut self) -> &mut Self

Source

pub fn use_default_synonym_dict( &mut self, node_novel_mode: bool, ) -> Result<&mut Self>

JS useDefaultSynonymDict.

Source

pub fn load_stopword_dict(&mut self, name: &str) -> Result<&mut Self>

Source

pub fn load_blacklist_dict(&mut self, name: &str) -> Result<&mut Self>

Source

pub fn load_blacklist_optimizer_dict(&mut self, name: &str) -> Result<&mut Self>

Source

pub fn load_blacklist_synonym_dict(&mut self, name: &str) -> Result<&mut Self>

Source

pub fn do_blacklist(&mut self) -> &mut Self

Source

pub fn add_blacklist(&mut self, word: &str) -> &mut Self

Remove a word from TABLE (JS addBlacklist).

Source

pub fn add_word( &mut self, spec: &str, p: Option<u32>, f: Option<f64>, ) -> Result<&mut Self>

Add a dictionary entry (詞|詞性|詞權值 or just the word).

Examples found in repository?
examples/basic.rs (line 24)
3fn main() -> Result<(), novel_segment::Error> {
4    let mut seg = Segment::new(novel_segment::SegmentOptions {
5        auto_cjk: true,
6        ..Default::default()
7    });
8    seg.use_default()?;
9    let text = "这是一个基于Rust的中文分词模块。";
10    let words = seg.do_segment(text, DoSegmentOptions::default());
11    println!("{text}");
12    println!(
13        "{}",
14        words
15            .iter()
16            .map(|w| format!("{}:{:x}", w.w, w.p.unwrap_or(0)))
17            .collect::<Vec<_>>()
18            .join(" / ")
19    );
20
21    let simple = seg.do_segment_simple(text, DoSegmentOptions::default());
22    println!("{}", simple.join(" / "));
23
24    seg.add_word("專有名詞", Some(novel_segment::POSTAG::D_N), Some(100.0))?;
25    Ok(())
26}
Source

pub fn add_synonym(&mut self, canonical: &str, variants: &[&str]) -> &mut Self

Source

pub fn do_segment(&self, text: &str, options: DoSegmentOptions) -> Vec<Word>

Examples found in repository?
examples/basic.rs (line 10)
3fn main() -> Result<(), novel_segment::Error> {
4    let mut seg = Segment::new(novel_segment::SegmentOptions {
5        auto_cjk: true,
6        ..Default::default()
7    });
8    seg.use_default()?;
9    let text = "这是一个基于Rust的中文分词模块。";
10    let words = seg.do_segment(text, DoSegmentOptions::default());
11    println!("{text}");
12    println!(
13        "{}",
14        words
15            .iter()
16            .map(|w| format!("{}:{:x}", w.w, w.p.unwrap_or(0)))
17            .collect::<Vec<_>>()
18            .join(" / ")
19    );
20
21    let simple = seg.do_segment_simple(text, DoSegmentOptions::default());
22    println!("{}", simple.join(" / "));
23
24    seg.add_word("專有名詞", Some(novel_segment::POSTAG::D_N), Some(100.0))?;
25    Ok(())
26}
Source

pub fn do_segment_simple( &self, text: &str, options: DoSegmentOptions, ) -> Vec<String>

Examples found in repository?
examples/basic.rs (line 21)
3fn main() -> Result<(), novel_segment::Error> {
4    let mut seg = Segment::new(novel_segment::SegmentOptions {
5        auto_cjk: true,
6        ..Default::default()
7    });
8    seg.use_default()?;
9    let text = "这是一个基于Rust的中文分词模块。";
10    let words = seg.do_segment(text, DoSegmentOptions::default());
11    println!("{text}");
12    println!(
13        "{}",
14        words
15            .iter()
16            .map(|w| format!("{}:{:x}", w.w, w.p.unwrap_or(0)))
17            .collect::<Vec<_>>()
18            .join(" / ")
19    );
20
21    let simple = seg.do_segment_simple(text, DoSegmentOptions::default());
22    println!("{}", simple.join(" / "));
23
24    seg.add_word("專有名詞", Some(novel_segment::POSTAG::D_N), Some(100.0))?;
25    Ok(())
26}

Trait Implementations§

Source§

impl Default for Segment

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.