simple/
simple.rs

1extern crate mecab;
2
3use mecab::Tagger;
4
5fn main() {
6    let input = "太郎は次郎が持っている本を花子に渡した。";
7    println!("INPUT: {}", input);
8
9    let mut tagger = Tagger::new("");
10
11    // gets tagged result as String
12    let mut result = tagger.parse_str(input);
13    println!("RESULT: {}", result);
14
15    // gets N best results as String
16    result = tagger.parse_nbest(3, input);
17    println!("NBEST:\n{}", result);
18
19    // gets N best in sequence
20    tagger.parse_nbest_init(input);
21    for i in 0..3 {
22        if let Some(res) = tagger.next() {
23            println!("{}:\n{}", i, res);
24        }
25    }
26
27    // gets Node object
28    for node in tagger.parse_to_node(input).iter_next() {
29        match node.stat as i32 {
30            mecab::MECAB_BOS_NODE => {
31                print!("{} BOS ", node.id);
32            }
33            mecab::MECAB_EOS_NODE => {
34                print!("{} EOS ", node.id);
35            }
36            _ => {
37                print!("{} {} ", node.id, &(node.surface)[..(node.length as usize)]);
38            }
39        }
40
41        println!("{} {} {} {} {} {} {} {} {} {} {} {} {}",
42                 node.feature,
43                 input.len() as isize - node.surface.len() as isize,
44                 input.len() as isize - node.surface.len() as isize + node.length as isize,
45                 node.rcattr,
46                 node.lcattr,
47                 node.posid,
48                 node.char_type,
49                 node.stat,
50                 node.isbest,
51                 node.alpha,
52                 node.beta,
53                 node.prob,
54                 node.cost);
55    }
56
57    // dictionary info
58    for dict in tagger.dictionary_info().iter() {
59        println!("\nfilename: {}", dict.filename);
60        println!("charset: {}", dict.charset);
61        println!("size: {}", dict.size);
62        println!("type: {}", dict.dict_type);
63        println!("lsize: {}", dict.lsize);
64        println!("rsize: {}", dict.rsize);
65        println!("version: {}", dict.version);
66    }
67}