1 | ;;; |
---|
2 | ;;; mecab.scm |
---|
3 | ;;; |
---|
4 | ;;; 2009.3.13 by naoya_t |
---|
5 | ;;; |
---|
6 | |
---|
7 | (define-module mecab |
---|
8 | (export <mecab> <mecab-node> <mecab-dictionary-info> |
---|
9 | mecab? mecab-node? mecab-dictionary-info? |
---|
10 | mecab-destroyed? |
---|
11 | write-object |
---|
12 | |
---|
13 | mecab-tagger ; message passing |
---|
14 | <mecab-tagger> mecab-make-tagger ; class |
---|
15 | |
---|
16 | mecab-do |
---|
17 | mecab-new mecab-new2 |
---|
18 | mecab-version |
---|
19 | mecab-strerror |
---|
20 | mecab-destroy |
---|
21 | |
---|
22 | mecab-get-partial mecab-set-partial |
---|
23 | mecab-get-theta mecab-set-theta |
---|
24 | mecab-get-lattice-level mecab-set-lattice-level |
---|
25 | mecab-get-all-morphs mecab-set-all-morphs |
---|
26 | |
---|
27 | mecab-sparse-tostr mecab-sparse-tostr2 ;; mecab-sparse-tostr3 |
---|
28 | mecab-sparse-tonode mecab-sparse-tonode2 |
---|
29 | mecab-nbest-sparse-tostr mecab-nbest-sparse-tostr2 ;; mecab-nbest-sparse-tostr3 |
---|
30 | mecab-nbest-init mecab-nbest-init2 |
---|
31 | mecab-nbest-next-tostr ;; mecab-nbest-next-tostr2 |
---|
32 | mecab-nbest-next-tonode |
---|
33 | mecab-format-node |
---|
34 | mecab-dictionary-info |
---|
35 | mecab-dict-index mecab-dict-gen |
---|
36 | mecab-cost-train mecab-system-eval mecab-test-gen |
---|
37 | |
---|
38 | mecab-node-prev mecab-node-next mecab-node-enext mecab-node-bnext |
---|
39 | mecab-node-surface mecab-node-feature |
---|
40 | mecab-node-length mecab-node-rlength |
---|
41 | mecab-node-id mecab-node-rc-attr mecab-node-lc-attr |
---|
42 | mecab-node-posid mecab-node-char-type |
---|
43 | mecab-node-stat mecab-node-best? |
---|
44 | mecab-node-alpha mecab-node-beta mecab-node-prob |
---|
45 | mecab-node-wcost mecab-node-cost |
---|
46 | |
---|
47 | mecab-dictionary-info-filename mecab-dictionary-info-charset |
---|
48 | mecab-dictionary-info-size mecab-dictionary-info-type |
---|
49 | mecab-dictionary-info-lsize mecab-dictionary-info-rsize |
---|
50 | mecab-dictionary-info-version mecab-dictionary-info-next |
---|
51 | ) |
---|
52 | ) |
---|
53 | |
---|
54 | (select-module mecab) |
---|
55 | |
---|
56 | ;; Loads extension |
---|
57 | (dynamic-load "mecab") |
---|
58 | |
---|
59 | ;; |
---|
60 | ;; Put your Scheme definitions here |
---|
61 | ;; |
---|
62 | (define-macro (mecab? obj) `(is-a? ,obj <mecab>)) |
---|
63 | (define-macro (mecab-node? obj) `(is-a? ,obj <mecab-node>)) |
---|
64 | (define-macro (mecab-dictionary-info? obj) `(is-a? ,obj <mecab-dictionary-info>)) |
---|
65 | |
---|
66 | (define-method write-object ((m <mecab>) out) |
---|
67 | (format out "#<mecab>")); (mecab-version))) |
---|
68 | (define-method write-object ((m <mecab-node>) out) |
---|
69 | (format out "#<mecab-node>")) |
---|
70 | (define-method write-object ((m <mecab-dictionary-info>) out) |
---|
71 | (format out "#<mecab-dictionary-info>")) |
---|
72 | |
---|
73 | (define-reader-ctor '<mecab> |
---|
74 | (lambda args (apply mecab-new args))) |
---|
75 | |
---|
76 | (define (mecab-tagger paramstr) |
---|
77 | (let1 mecabobj (mecab-new paramstr) |
---|
78 | ;; str を解析して文字列として結果を得ます. len は str の長さ(省略可能) |
---|
79 | (define (parse-to-string str) |
---|
80 | (mecab-sparse-tostr mecabobj str)) |
---|
81 | |
---|
82 | ;; str を解析して MeCab::Node 型の形態素を返します. |
---|
83 | ;; この形態素は文頭を示すもので, next を順に辿ることで全形態素にアクセスできます |
---|
84 | (define (parse-to-node str) |
---|
85 | (mecab-sparse-tonode mecabobj str)) |
---|
86 | |
---|
87 | ;; parse の Nbest 版です. N に nbest の個数を指定します. |
---|
88 | ;; この機能を使う場合は, 起動時オプションとして -l 1 を指定する必要があります |
---|
89 | (define (parse-nbest n str) |
---|
90 | (mecab-nbest-sparse-tostr mecabobj n str)) |
---|
91 | ;; 解析結果を, 確からしいものから順番に取得する場合にこの関数で初期化を行います. |
---|
92 | (define (parse-nbest-init str) |
---|
93 | (mecab-nbest-init mecabobj str)) |
---|
94 | |
---|
95 | ;; parse-nbest-init の後, この関数を順次呼ぶことで, |
---|
96 | ;; 確からしい解析結果を, 順番に取得できます. (string) |
---|
97 | (define (next) |
---|
98 | (mecab-nbest-next-tostr mecabobj)) |
---|
99 | ;; next() と同じですが, MeCab::Node を返します. |
---|
100 | (define (next-node) |
---|
101 | (mecab-nbest-next-tonode mecabobj)) |
---|
102 | ;; |
---|
103 | (define (format-node node) |
---|
104 | (mecab-format-node mecabobj node)) |
---|
105 | |
---|
106 | (lambda (m) |
---|
107 | (case m |
---|
108 | [(parse parse-to-string) parse-to-string] |
---|
109 | [(parse-to-node) parse-to-node] |
---|
110 | [(parse-nbest) parse-nbest] |
---|
111 | [(parse-nbest-init) parse-nbest-init] |
---|
112 | [(next) next] |
---|
113 | [(next-node) next-node] |
---|
114 | [(format-node) format-node] |
---|
115 | )))) |
---|
116 | |
---|
117 | ;;; class |
---|
118 | (define-class <mecab-tagger> () (mecab #f)) |
---|
119 | (define (mecab-make-tagger paramstr) |
---|
120 | (make <mecab-tagger> :mecab (mecab-new2 paramstr))) |
---|
121 | (define (tagger-mecab tagger) (slot-ref tagger 'mecab)) |
---|
122 | (define-method parse ((tagger <mecab-tagger>) (str <string>)) |
---|
123 | (mecab-sparse-tostr (tagger-mecab tagger) str)) |
---|
124 | (define-method parse ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
---|
125 | (mecab-sparse-tostr2 (tagger-mecab tagger) str len)) |
---|
126 | (define-method parse-to-string ((tagger <mecab-tagger>) (str <string>)) |
---|
127 | (mecab-sparse-tostr (tagger-mecab tagger) str)) |
---|
128 | (define-method parse-to-string ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
---|
129 | (mecab-sparse-tostr (tagger-mecab tagger) str len)) |
---|
130 | (define-method parse-to-node ((tagger <mecab-tagger>) (str <string>)) |
---|
131 | (mecab-sparse-tonode (tagger-mecab tagger) str)) |
---|
132 | (define-method parse-to-node ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
---|
133 | (mecab-sparse-tonode2 (tagger-mecab tagger) str len)) |
---|
134 | (define-method parse-nbest ((tagger <mecab-tagger>) (n <integer>) (str <string>)) |
---|
135 | (mecab-nbest-sparse-tostr (tagger-mecab tagger) str)) |
---|
136 | (define-method parse-nbest ((tagger <mecab-tagger>) (n <integer>) (str <string>) (len <integer>)) |
---|
137 | (mecab-nbest-sparse-tostr (tagger-mecab tagger) str len)) |
---|
138 | (define-method parse-nbest-init ((tagger <mecab-tagger>) (str <string>)) |
---|
139 | (mecab-nbest-init (tagger-mecab tagger) str)) |
---|
140 | (define-method parse-nbest-init ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
---|
141 | (mecab-nbest-init (tagger-mecab tagger) str len)) |
---|
142 | (define-method next ((tagger <mecab-tagger>)) |
---|
143 | (mecab-nbest-next-tostr (tagger-mecab tagger))) |
---|
144 | (define-method next-node ((tagger <mecab-tagger>)) |
---|
145 | (mecab-nbest-next-tonode (tagger-mecab tagger))) |
---|
146 | (define-method format-node ((tagger <mecab-tagger>) (node <mecab-node>)) |
---|
147 | (mecab-format-node (tagger-mecab tagger) node)) |
---|
148 | |
---|
149 | ;; Epilogue |
---|
150 | (provide "mecab") |
---|