pub struct Segment { /* private fields */ }Expand description
Chinese word segmenter.
Implementations§
Source§impl Segment
impl Segment
Sourcepub fn new(options: SegmentOptions) -> Self
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}Sourcepub fn with_default() -> Result<Self>
pub fn with_default() -> Result<Self>
new Segment(); useDefault() — auto_cjk stays false.
Sourcepub fn with_novel_default() -> Result<Self>
pub fn with_novel_default() -> Result<Self>
Test/CLI profile: autoCjk, all_mod, convertSynonym.
Sourcepub fn with_node_novel_default() -> Result<Self>
pub fn with_node_novel_default() -> Result<Self>
word.novel.test.ts: createSegment then wipe SYNONYM and reload with nodeNovelMode.
Sourcepub fn use_default(&mut self) -> Result<&mut Self>
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}pub fn use_default_modules(&mut self) -> &mut Self
pub fn use_tokenizer(&mut self, tok: Box<dyn Tokenizer>) -> &mut Self
pub fn use_optimizer(&mut self, opt: Box<dyn Optimizer>) -> &mut Self
pub fn use_default_dicts(&mut self) -> Result<&mut Self>
pub fn load_dict(&mut self, name: &str) -> Result<&mut Self>
pub fn load_dict_file(&mut self, path: impl AsRef<Path>) -> Result<&mut Self>
pub fn load_synonym_dict( &mut self, name: &str, skip_exists: bool, ) -> Result<&mut Self>
pub fn clear_synonym_dict(&mut self) -> &mut Self
Sourcepub fn use_default_synonym_dict(
&mut self,
node_novel_mode: bool,
) -> Result<&mut Self>
pub fn use_default_synonym_dict( &mut self, node_novel_mode: bool, ) -> Result<&mut Self>
JS useDefaultSynonymDict.
pub fn load_stopword_dict(&mut self, name: &str) -> Result<&mut Self>
pub fn load_blacklist_dict(&mut self, name: &str) -> Result<&mut Self>
pub fn load_blacklist_optimizer_dict(&mut self, name: &str) -> Result<&mut Self>
pub fn load_blacklist_synonym_dict(&mut self, name: &str) -> Result<&mut Self>
pub fn do_blacklist(&mut self) -> &mut Self
Sourcepub fn add_blacklist(&mut self, word: &str) -> &mut Self
pub fn add_blacklist(&mut self, word: &str) -> &mut Self
Remove a word from TABLE (JS addBlacklist).
Sourcepub fn add_word(
&mut self,
spec: &str,
p: Option<u32>,
f: Option<f64>,
) -> Result<&mut Self>
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}pub fn add_synonym(&mut self, canonical: &str, variants: &[&str]) -> &mut Self
Sourcepub fn do_segment(&self, text: &str, options: DoSegmentOptions) -> Vec<Word>
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}Sourcepub fn do_segment_simple(
&self,
text: &str,
options: DoSegmentOptions,
) -> Vec<String>
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§
Auto Trait Implementations§
impl !RefUnwindSafe for Segment
impl !UnwindSafe for Segment
impl Freeze for Segment
impl Send for Segment
impl Sync for Segment
impl Unpin for Segment
impl UnsafeUnpin for Segment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more