Skip to main content

Crate mecab_sys

Crate mecab_sys 

Source
Expand description

mecab-sys is the FFI wrapper for MeCab. While ffi provides the FFI bindings, the top-level items are the safe wrapper for MeCab.

§Examples

use mecab_sys::Model;

let model = Model::from_cli_arg(c"-d /path/to/your/dict -r /path/to/dictrc").unwrap();

let tagger = model.new_tagger().unwrap();
let mut lattice = model.new_lattice().unwrap();

let mut lattice = lattice.set_sentence("すもももももももものうち");
tagger.parse(&mut lattice).unwrap();

for node in lattice.bos_node() {
    let surface = node.surface();
    let feat = node.feature();

    println!("{surface}: {feat}");
}

§Cursor API

lattice.bos_node() returns a NodeCursor, which can be used either as an iterator and as a cursor of Nodes.

use mecab_sys::Model;

let model = Model::from_cli_arg(c"-d /path/to/your/dict -r /path/to/dictrc").unwrap();

let tagger = model.new_tagger().unwrap();
let mut lattice = model.new_lattice().unwrap();

let mut lattice = lattice.set_sentence("すもももももももものうち");
tagger.parse(&mut lattice).unwrap();

let mut cursor = lattice.bos_node();
assert!(cursor.curr().is_some_and(|node| node.kind().is_bos()));

cursor.move_next();
cursor.move_next();
if let Some(node) = cursor.curr() {
    let surface = node.surface();
    let feat = node.feature();

    println!("{surface}: {feat}");
}

cursor.move_prev();
if let Some(node) = cursor.curr() {
    let surface = node.surface();
    let feat = node.feature();

    println!("{surface}: {feat}");
}

After you call move_next() on the EoS node, call move_prev() on the BoS node, or consume as an iterator, then the cursor shifted to the “dead” state, never being back to the alive state.

let mut cursor = lattice.bos_node();
while cursor.curr().is_some_and(|node| !node.kind().is_eos()) {
    cursor.move_next();
}
assert!(cursor.curr().is_some_and(|node| node.kind().is_eos()));

// Call `move_next()` on the EoS node
cursor.move_next();
assert!(cursor.curr().is_none());

// Never back to the original node
cursor.move_prev();
assert!(cursor.curr().is_none());

let mut cursor = lattice.bos_node();
assert!(cursor.curr().is_some_and(|node| node.kind().is_bos()));

// Call `move_prev()` on the BoS node
cursor.move_prev();
assert!(cursor.curr().is_none());

// Never back to the original node
cursor.move_next();
assert!(cursor.curr().is_none());

let mut cursor = lattice.bos_node();
// Consume the iterator, reaching at the EoS node
for _ in &mut cursor {}
assert!(cursor.curr().is_none());

// Never back to alive nodes
cursor.move_prev();
assert!(cursor.curr().is_none());

Modules§

ffi
The raw FFI layer for MeCab, generated from mecab.h.

Structs§

Error
Represents an error returned by MeCab.
Lattice
A MeCab lattice representing a search space for morphological analysis. It wraps C mecab_lattice_t.
LatticeGuard
A guard for a lattice that holds a reference to the sentence string.
LcAttr
Left attribute ID.
Model
A thread-safe, shared MeCab model. It wraps C mecab_model_t.
Node
A single node (morpheme) in the MeCab lattice. It wraps C mecab_node_t.
NodeCursor
A cursor for iterating over nodes in a lattice.
RcAttr
Right attribute ID.
RequestType
Request type flags for MeCab analysis.
Tagger
A MeCab tagger for analyzing text. It wraps C mecab_t.

Enums§

BoundaryConstraintType
Boundary constraint type for MeCab analysis.
NodeKind
Status of a MeCab node.