1use mecab::Tagger;
2
3pub struct Mecab {
4 arg: String,
5}
6
7pub struct MecabResponse {
8 pub values: Vec<String>,
9}
10
11impl Mecab {
12 pub fn new(arg: &str) -> Mecab {
13 Mecab {
14 arg: arg.to_string(),
15 }
16 }
17 pub fn get(&self) -> &str {
18 self.arg.as_str()
19 }
20
21 pub fn get_worlds(&self, input: &str) -> Vec<String> {
22 let tagger = Tagger::new(self.arg.clone());
23
24 let result = tagger.parse_str(input);
25 let rows: Vec<&str> = result.split("\n").collect();
26
27 let mut node = Vec::new();
28 for row in rows {
29 if row.len() == 0 {
30 continue;
31 }
32 let line: Vec<&str> = row.trim().split("\t").collect();
33 let word = line[0];
34 if line.len() >= 2 {
35 let w: Vec<&str> = line[1].split(",").collect();
36 if w.len() >= 2 && w[1] == "数" {
37 continue;
38 }
39 if w.len() >= 3 && w[2] == "地域" {
40 continue;
41 }
42 }
43 if word == "EOS" {
44 continue;
45 }
46 if word.len() >= 2 {
47 node.push(line[0].to_string());
48 }
49 }
50 return node;
51 }
52
53 pub fn parse(&self, input: &str) -> MecabResponse {
54 let tagger = Tagger::new(self.arg.clone());
55
56 let result = tagger.parse_str(input);
57 let rows: Vec<&str> = result.split("\n").collect();
58
59 let mut response = MecabResponse { values: Vec::new() };
60 for row in rows {
61 let row: Vec<&str> = row.trim().split("\t").collect();
62 if row[0] == "EOS" {
63 break;
64 }
65 response.values.push(row[0].to_string());
66 }
67 response
68 }
69}
70
71pub fn echo() {
72 println!("uiuifree-mecab")
73}
74
75#[cfg(test)]
76mod tests {
77
78 use crate::Mecab;
79
80 #[test]
81 fn it_works() {
82 let mecab = Mecab::new("-d /usr/lib/x86_64-linux-gnu/mecab/dic/mecab-ipadic-neologd -u /home/uiuifree/PycharmProjects/seo-site-crow/storage/output_dic.dic");
83
84 println!(
85 "{:?}",
86 mecab.get_worlds("ケアマネージャー(介護支援専門員)")
87 );
88 }
108}