multithreaded/
multithreaded.rs

1extern crate mecab;
2
3use mecab::Model;
4use std::sync::Arc;
5
6fn main() {
7    let input = "太郎は次郎が持っている本を花子に渡した。";
8
9    // create model object
10    let model = Arc::new(Model::new(""));
11
12    let handle = std::thread::spawn(move || {
13        // create tagger based on the model
14        let tagger = model.create_tagger();
15
16        // create lattice object per thread
17        let mut lattice = model.create_lattice();
18
19        // get tagged result as string
20        lattice.set_sentence(input);
21
22        // parse lattice
23        tagger.parse(&lattice);
24        println!("{}", lattice.to_string());
25
26        // iterate over node objects
27        for node in lattice.bos_node().iter_next() {
28            match node.stat as i32 {
29                mecab::MECAB_BOS_NODE => {
30                    print!("{} BOS ", node.id);
31                }
32                mecab::MECAB_EOS_NODE => {
33                    print!("{} EOS ", node.id);
34                }
35                _ => {
36                    print!("{} {} ", node.id, &(node.surface)[..(node.length as usize)]);
37                }
38            }
39
40            println!("{} {} {} {} {} {} {} {} {} {} {} {} {}",
41                     node.feature,
42                     input.len() as isize - node.surface.len() as isize,
43                     input.len() as isize - node.surface.len() as isize + node.length as isize,
44                     node.rcattr,
45                     node.lcattr,
46                     node.posid,
47                     node.char_type,
48                     node.stat,
49                     node.isbest,
50                     node.alpha,
51                     node.beta,
52                     node.prob,
53                     node.cost);
54        }
55
56        // iterate over begin and end nodes
57        let len = lattice.size();
58        for i in 0..len + 1 {
59            let b = lattice.begin_nodes(i);
60            let e = lattice.end_nodes(i);
61
62            if let Some(nodes) = b {
63                for node in nodes.iter_bnext() {
64                    println!("B[{}] {}\t{}", i, node.surface, node.feature);
65                }
66            }
67
68            if let Some(nodes) = e {
69                for node in nodes.iter_enext() {
70                    println!("E[{}] {}\t{}", i, node.surface, node.feature);
71                }
72            }
73        }
74
75        // get N best results
76        lattice.set_request_type(mecab::MECAB_NBEST);
77        lattice.set_sentence(input);
78        tagger.parse(&lattice);
79
80        for i in 0..10 {
81            println!("NBEST: {}", i);
82            println!("{}", lattice.to_string());
83
84            if !lattice.next() {
85                break;
86            }
87        }
88
89        // marginal probabilities
90        lattice.remove_request_type(mecab::MECAB_NBEST);
91        lattice.set_request_type(mecab::MECAB_MARGINAL_PROB);
92        lattice.set_sentence(input);
93        tagger.parse(&lattice);
94
95        println!("{}", lattice.theta());
96
97        for node in lattice.bos_node().iter_next() {
98            println!("{}\t{}\t{}",
99                     &(node.surface)[..(node.length as usize)],
100                     node.feature,
101                     node.prob);
102        }
103
104        // dictionary info
105        for dict in model.dictionary_info().iter() {
106            println!("\nfilename: {}", dict.filename);
107            println!("charset: {}", dict.charset);
108            println!("size: {}", dict.size);
109            println!("type: {}", dict.dict_type);
110            println!("lsize: {}", dict.lsize);
111            println!("rsize: {}", dict.rsize);
112            println!("version: {}", dict.version);
113        }
114    });
115
116    handle.join().unwrap();
117}