| 1 | ;;; |
|---|
| 2 | ;;; mecab.stub - MeCab binding |
|---|
| 3 | ;;; |
|---|
| 4 | ;;; Copyright (c) 2004 Kimura Fuyuki, All rights reserved. |
|---|
| 5 | ;;; |
|---|
| 6 | ;;; Redistribution and use in source and binary forms, with or without |
|---|
| 7 | ;;; modification, are permitted provided that the following conditions |
|---|
| 8 | ;;; are met: |
|---|
| 9 | ;;; |
|---|
| 10 | ;;; 1. Redistributions of source code must retain the above copyright |
|---|
| 11 | ;;; notice, this list of conditions and the following disclaimer. |
|---|
| 12 | ;;; |
|---|
| 13 | ;;; 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | ;;; notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | ;;; documentation and/or other materials provided with the distribution. |
|---|
| 16 | ;;; |
|---|
| 17 | ;;; 3. Neither the name of the authors nor the names of its contributors |
|---|
| 18 | ;;; may be used to endorse or promote products derived from this |
|---|
| 19 | ;;; software without specific prior written permission. |
|---|
| 20 | ;;; |
|---|
| 21 | ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 22 | ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 23 | ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 24 | ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 25 | ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 26 | ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|---|
| 27 | ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 28 | ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 29 | ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 30 | ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 31 | ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | ;;; |
|---|
| 33 | ;;; $Id: mecab-lib.scm,v 1.1 2009/03/02 03:52:45 shirok Exp $ |
|---|
| 34 | ;;; |
|---|
| 35 | |
|---|
| 36 | (define-module text.mecab |
|---|
| 37 | (use srfi-1) |
|---|
| 38 | (use gauche.charconv) |
|---|
| 39 | (export <mecab> <mecab-node> <mecab-dictionary-info> |
|---|
| 40 | mecab? mecab-node? mecab-dictionary-info? |
|---|
| 41 | mecab-do mecab-new mecab-new2 |
|---|
| 42 | mecab-version mecab-strerror mecab-destroy mecab-destroyed? |
|---|
| 43 | |
|---|
| 44 | mecab-tagger ; message passing |
|---|
| 45 | <mecab-tagger> mecab-make-tagger ; class |
|---|
| 46 | |
|---|
| 47 | mecab-get-partial mecab-set-partial |
|---|
| 48 | mecab-get-theta mecab-set-theta |
|---|
| 49 | mecab-get-lattice-level mecab-set-lattice-level |
|---|
| 50 | mecab-get-all-morphs mecab-set-all-morphs |
|---|
| 51 | |
|---|
| 52 | mecab-sparse-tostr mecab-sparse-tostr2 ;; mecab-sparse-tostr3 |
|---|
| 53 | mecab-sparse-tonode mecab-sparse-tonode2 |
|---|
| 54 | mecab-nbest-sparse-tostr mecab-nbest-sparse-tostr2 ;; mecab-nbest-sparse-tostr3 |
|---|
| 55 | mecab-nbest-init mecab-nbest-init2 |
|---|
| 56 | mecab-nbest-next-tostr ;; mecab-nbest-next-tostr2 |
|---|
| 57 | mecab-nbest-next-tonode |
|---|
| 58 | mecab-format-node |
|---|
| 59 | mecab-dictionary-info |
|---|
| 60 | mecab-dict-index mecab-dict-gen |
|---|
| 61 | mecab-cost-train mecab-system-eval mecab-test-gen |
|---|
| 62 | |
|---|
| 63 | mecab-node-prev mecab-node-next mecab-node-enext mecab-node-bnext |
|---|
| 64 | mecab-node-surface mecab-node-feature |
|---|
| 65 | mecab-node-length mecab-node-rlength |
|---|
| 66 | mecab-node-id mecab-node-rc-attr mecab-node-lc-attr |
|---|
| 67 | mecab-node-posid mecab-node-char-type |
|---|
| 68 | mecab-node-stat mecab-node-best? |
|---|
| 69 | mecab-node-alpha mecab-node-beta mecab-node-prob |
|---|
| 70 | mecab-node-wcost mecab-node-cost |
|---|
| 71 | |
|---|
| 72 | mecab-dictionary-info-filename mecab-dictionary-info-charset |
|---|
| 73 | mecab-dictionary-info-size mecab-dictionary-info-type |
|---|
| 74 | mecab-dictionary-info-lsize mecab-dictionary-info-rsize |
|---|
| 75 | mecab-dictionary-info-version mecab-dictionary-info-next |
|---|
| 76 | )) |
|---|
| 77 | (select-module text.mecab) |
|---|
| 78 | |
|---|
| 79 | ;; This should be configurable, since mecab can be compiled to use utf-8. |
|---|
| 80 | (define-constant MECAB_ENCODING 'euc-jp) |
|---|
| 81 | ;(define-constant MECAB_ENCODING 'utf-8) |
|---|
| 82 | |
|---|
| 83 | (define (cv-send str) str) |
|---|
| 84 | ; (ces-convert str (gauche-character-encoding) MECAB_ENCODING)) |
|---|
| 85 | |
|---|
| 86 | (define (cv-recv str) str) |
|---|
| 87 | ; (and str (ces-convert str MECAB_ENCODING))) |
|---|
| 88 | |
|---|
| 89 | (define (mecab-do args) |
|---|
| 90 | (unless (every string? args) |
|---|
| 91 | (error "mecab-do: list of strings required, but got:" args)) |
|---|
| 92 | (%mecab-do (map cv-send args))) |
|---|
| 93 | |
|---|
| 94 | (define (mecab-new args) |
|---|
| 95 | (unless (every string? args) |
|---|
| 96 | (error "mecab-new: list of strings required, but got:" args)) |
|---|
| 97 | (%mecab-new (map cv-send args))) |
|---|
| 98 | |
|---|
| 99 | (define (mecab-new2 str) |
|---|
| 100 | (%mecab-new2 (cv-send str))) |
|---|
| 101 | |
|---|
| 102 | (define (mecab-strerror m) |
|---|
| 103 | (cv-recv (if m (%mecab-strerror m) |
|---|
| 104 | (%mecab-strerror-with-null)))) |
|---|
| 105 | |
|---|
| 106 | (define (mecab-get-partial m) |
|---|
| 107 | (%mecab-get-partial m)) |
|---|
| 108 | |
|---|
| 109 | (define (mecab-set-partial m partial) |
|---|
| 110 | (%mecab-set-partial m partial)) |
|---|
| 111 | |
|---|
| 112 | (define (mecab-get-theta m) |
|---|
| 113 | (%mecab-get-theta m)) |
|---|
| 114 | |
|---|
| 115 | (define (mecab-set-theta m theta) |
|---|
| 116 | (%mecab-set-theta m theta)) |
|---|
| 117 | |
|---|
| 118 | (define (mecab-get-lattice-level m) |
|---|
| 119 | (%mecab-get-lattice-level m)) |
|---|
| 120 | |
|---|
| 121 | (define (mecab-set-lattice-level m llevel) |
|---|
| 122 | (%mecab-set-lattice-level m llevel)) |
|---|
| 123 | |
|---|
| 124 | (define (mecab-get-all-morphs m) |
|---|
| 125 | (%mecab-get-all-morphs m)) |
|---|
| 126 | |
|---|
| 127 | (define (mecab-set-all-morphs m morphs) |
|---|
| 128 | (%mecab-set-all-morphs m morphs)) |
|---|
| 129 | |
|---|
| 130 | (define (mecab-sparse-tostr m str) |
|---|
| 131 | (cv-recv (%mecab-sparse-tostr m (cv-send str)))) |
|---|
| 132 | |
|---|
| 133 | (define (mecab-sparse-tostr2 m str len) |
|---|
| 134 | (cv-recv (%mecab-sparse-tostr2 m (cv-send str) len))) |
|---|
| 135 | |
|---|
| 136 | (define (mecab-sparse-tonode m str) |
|---|
| 137 | (%mecab-sparse-tonode m (cv-send str))) |
|---|
| 138 | |
|---|
| 139 | (define (mecab-sparse-tonode2 m str len) |
|---|
| 140 | (%mecab-sparse-tonode2 m (cv-send str) len)) |
|---|
| 141 | |
|---|
| 142 | (define (mecab-nbest-sparse-tostr m n str) |
|---|
| 143 | (cv-recv (%mecab-nbest-sparse-tostr m n (cv-send str)))) |
|---|
| 144 | |
|---|
| 145 | (define (mecab-nbest-sparse-tostr2 m n str len) |
|---|
| 146 | (cv-recv (%mecab-nbest-sparse-tostr2 m n (cv-send str) len))) |
|---|
| 147 | |
|---|
| 148 | (define (mecab-nbest-init m str) |
|---|
| 149 | (%mecab-nbest-init m (cv-send str))) |
|---|
| 150 | |
|---|
| 151 | (define (mecab-nbest-init2 m str len) |
|---|
| 152 | (%mecab-nbest-init2 m (cv-send str) len)) |
|---|
| 153 | |
|---|
| 154 | (define (mecab-nbest-next-tostr m) |
|---|
| 155 | (cv-recv (%mecab-nbest-next-tostr m))) |
|---|
| 156 | |
|---|
| 157 | (define (mecab-nbest-next-tonode m) |
|---|
| 158 | (%mecab-nbest-next-tonode m)) |
|---|
| 159 | |
|---|
| 160 | (define (mecab-format-node m node) |
|---|
| 161 | (%mecab-format-node m node)) |
|---|
| 162 | |
|---|
| 163 | (define (mecab-dictionary-info m) |
|---|
| 164 | (%mecab-dictionary-info m)) |
|---|
| 165 | |
|---|
| 166 | (define (mecab-dict-index args) |
|---|
| 167 | (unless (every string? args) |
|---|
| 168 | (error "mecab-dict-index: list of strings required, but got:" args)) |
|---|
| 169 | (%mecab-dict-index (map cv-send args))) |
|---|
| 170 | |
|---|
| 171 | (define (mecab-dict-gen args) |
|---|
| 172 | (unless (every string? args) |
|---|
| 173 | (error "mecab-dict-gen: list of strings required, but got:" args)) |
|---|
| 174 | (%mecab-dict-gen (map cv-send args))) |
|---|
| 175 | |
|---|
| 176 | (define (mecab-cost-train args) |
|---|
| 177 | (unless (every string? args) |
|---|
| 178 | (error "mecab-cost-train: list of strings required, but got:" args)) |
|---|
| 179 | (%mecab-cost-train (map cv-send args))) |
|---|
| 180 | |
|---|
| 181 | (define (mecab-system-eval args) |
|---|
| 182 | (unless (every string? args) |
|---|
| 183 | (error "mecab-system-eval: list of strings required, but got:" args)) |
|---|
| 184 | (%mecab-system-eval (map cv-send args))) |
|---|
| 185 | |
|---|
| 186 | (define (mecab-test-gen args) |
|---|
| 187 | (unless (every string? args) |
|---|
| 188 | (error "mecab-test-gen: list of strings required, but got:" args)) |
|---|
| 189 | (%mecab-test-gen (map cv-send args))) |
|---|
| 190 | |
|---|
| 191 | ;; |
|---|
| 192 | (define (mecab-node-surface n) |
|---|
| 193 | (cv-recv (%mecab-node-surface n))) |
|---|
| 194 | |
|---|
| 195 | (define (mecab-node-feature n) |
|---|
| 196 | (cv-recv (%mecab-node-feature n))) |
|---|
| 197 | |
|---|
| 198 | (define (mecab-node-stat n) |
|---|
| 199 | (vector-ref #(mecab-nor-node mecab-unk-node mecab-bos-node mecab-eos-node) |
|---|
| 200 | (%mecab-node-stat n))) |
|---|
| 201 | |
|---|
| 202 | (define (mecab-dictionary-info-type dinfo) |
|---|
| 203 | (vector-ref #(mecab-sys-dic mecab-usr-dic mecab-unk-dic) |
|---|
| 204 | (%mecab-dictionary-info-type dinfo))) |
|---|
| 205 | |
|---|
| 206 | (inline-stub |
|---|
| 207 | "#include <mecab.h>" |
|---|
| 208 | |
|---|
| 209 | ;; mecab_t type holder. |
|---|
| 210 | "typedef struct ScmMeCabRec { |
|---|
| 211 | SCM_HEADER; |
|---|
| 212 | mecab_t *m; /* NULL if closed */ |
|---|
| 213 | } ScmMeCab; |
|---|
| 214 | |
|---|
| 215 | typedef struct ScmMeCabNodeRec { |
|---|
| 216 | SCM_HEADER; |
|---|
| 217 | mecab_node_t *node; |
|---|
| 218 | } ScmMeCabNode; |
|---|
| 219 | |
|---|
| 220 | typedef struct ScmMeCabDictionaryInfoRec { |
|---|
| 221 | SCM_HEADER; |
|---|
| 222 | mecab_dictionary_info_t *dic_info; |
|---|
| 223 | } ScmMeCabDictionaryInfo;" |
|---|
| 224 | |
|---|
| 225 | (define-cclass <mecab> :private ScmMeCab* "Scm_MeCabClass" |
|---|
| 226 | () |
|---|
| 227 | ()) |
|---|
| 228 | |
|---|
| 229 | (define-cclass <mecab-node> :private ScmMeCabNode* "Scm_MeCabNodeClass" |
|---|
| 230 | () |
|---|
| 231 | ()) |
|---|
| 232 | |
|---|
| 233 | (define-cclass <mecab-dictionary-info> :private ScmMeCabDictionaryInfo* "Scm_MeCabDictionaryInfoClass" |
|---|
| 234 | () |
|---|
| 235 | ()) |
|---|
| 236 | |
|---|
| 237 | ;; internal utility functions |
|---|
| 238 | (define-cfn mecab-cleanup (m::ScmMeCab*) ::void :static |
|---|
| 239 | (unless (== (-> m m) NULL) |
|---|
| 240 | (mecab-destroy (-> m m)) |
|---|
| 241 | (set! (-> m m) NULL))) |
|---|
| 242 | |
|---|
| 243 | (define-cfn mecab-finalize (obj data::void*) ::void :static |
|---|
| 244 | (mecab-cleanup (SCM_MECAB obj))) |
|---|
| 245 | |
|---|
| 246 | (define-cfn make-mecab (m::mecab_t*) :static |
|---|
| 247 | (when (== m NULL) (mecab-strerror NULL)) |
|---|
| 248 | (let* ([obj::ScmMeCab* (SCM_NEW ScmMeCab)]) |
|---|
| 249 | (SCM_SET_CLASS obj (& Scm_MeCabClass)) |
|---|
| 250 | (set! (-> obj m) m) |
|---|
| 251 | (Scm_RegisterFinalizer (SCM_OBJ obj) mecab-finalize NULL) |
|---|
| 252 | (return (SCM_OBJ obj)))) |
|---|
| 253 | |
|---|
| 254 | (define-cfn make-mecab-node (n::mecab_node_t*) :static |
|---|
| 255 | ; (when (== node NULL) (mecab-strerror NULL)) |
|---|
| 256 | (if (== n NULL) (return SCM_FALSE) |
|---|
| 257 | (let* ([obj::ScmMeCabNode* (SCM_NEW ScmMeCabNode)]) |
|---|
| 258 | (SCM_SET_CLASS obj (& Scm_MeCabNodeClass)) |
|---|
| 259 | (set! (-> obj node) n) |
|---|
| 260 | (return (SCM_OBJ obj))))) |
|---|
| 261 | |
|---|
| 262 | (define-cfn make-mecab-dictionary-info (dic_info::mecab_dictionary_info_t*) :static |
|---|
| 263 | ; (when (== dic_info NULL) (mecab-strerror NULL)) |
|---|
| 264 | (if (== dic_info NULL) (return SCM_FALSE) |
|---|
| 265 | (let* ([obj::ScmMeCabDictionaryInfo* (SCM_NEW ScmMeCabDictionaryInfo)]) |
|---|
| 266 | (SCM_SET_CLASS obj (& Scm_MeCabDictionaryInfoClass)) |
|---|
| 267 | (set! (-> obj dic_info) dic_info) |
|---|
| 268 | (return (SCM_OBJ obj))))) |
|---|
| 269 | |
|---|
| 270 | ;; |
|---|
| 271 | ;; MeCab API |
|---|
| 272 | ;; |
|---|
| 273 | ;; NB: for the default configuration, MeCab API takes EUC-JP string. |
|---|
| 274 | ;; The conversion is handled in the Scheme level. |
|---|
| 275 | (define-cproc %mecab-do (args::<list>) ::<int> |
|---|
| 276 | (let* ([argc::int (Scm_Length args)] |
|---|
| 277 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 278 | (result (mecab-do argc argv)))) |
|---|
| 279 | |
|---|
| 280 | (define-cproc %mecab-new (args::<list>) |
|---|
| 281 | (let* ([argc::int (Scm_Length args)] |
|---|
| 282 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 283 | (result (make-mecab (mecab-new argc argv))))) |
|---|
| 284 | |
|---|
| 285 | (define-cproc %mecab-new2 (arg::<string>) |
|---|
| 286 | (result (make-mecab (mecab-new2 (Scm_GetString arg))))) |
|---|
| 287 | |
|---|
| 288 | (define-cproc mecab-version () ::<const-cstring> mecab-version) |
|---|
| 289 | |
|---|
| 290 | (define-cproc mecab-destroy (m::<mecab>) ::<void> |
|---|
| 291 | (mecab-cleanup m)) |
|---|
| 292 | |
|---|
| 293 | (define-cproc mecab-destroyed? (m::<mecab>) ::<boolean> |
|---|
| 294 | (result (== (-> m m) NULL))) |
|---|
| 295 | |
|---|
| 296 | (define-cproc %mecab-strerror (m::<mecab>) ::<const-cstring> |
|---|
| 297 | (result (mecab-strerror (-> m m)))) |
|---|
| 298 | (define-cproc %mecab-strerror-with-null () ::<const-cstring> |
|---|
| 299 | (result (mecab-strerror NULL))) |
|---|
| 300 | |
|---|
| 301 | (define-cproc %mecab-sparse-tostr (m::<mecab> str::<const-cstring>) |
|---|
| 302 | ::<const-cstring>? |
|---|
| 303 | (result (mecab-sparse-tostr (-> m m) str))) |
|---|
| 304 | |
|---|
| 305 | (define-cproc %mecab-sparse-tostr2 (m::<mecab> str::<const-cstring> len::<uint>) |
|---|
| 306 | ::<const-cstring>? |
|---|
| 307 | (result (mecab-sparse-tostr2 (-> m m) str len))) |
|---|
| 308 | |
|---|
| 309 | (define-cproc %mecab-nbest-sparse-tostr (m::<mecab> n::<uint> str::<const-cstring>) |
|---|
| 310 | ::<const-cstring>? |
|---|
| 311 | (result (mecab-nbest-sparse-tostr (-> m m) n str))) |
|---|
| 312 | |
|---|
| 313 | (define-cproc %mecab-nbest-sparse-tostr2 (m::<mecab> n::<uint> str::<const-cstring> len::<uint>) |
|---|
| 314 | ::<const-cstring>? |
|---|
| 315 | (result (mecab-nbest-sparse-tostr2 (-> m m) n str len))) |
|---|
| 316 | |
|---|
| 317 | (define-cproc %mecab-nbest-init (m::<mecab> str::<const-cstring>) |
|---|
| 318 | ::<int> |
|---|
| 319 | (result (mecab-nbest-init (-> m m) str))) |
|---|
| 320 | |
|---|
| 321 | (define-cproc %mecab-nbest-init2 (m::<mecab> str::<const-cstring> len::<uint>) |
|---|
| 322 | ::<int> |
|---|
| 323 | (result (mecab-nbest-init2 (-> m m) str len))) |
|---|
| 324 | |
|---|
| 325 | (define-cproc %mecab-nbest-next-tostr (m::<mecab>) |
|---|
| 326 | ; (result (mecab-nbest-next-tostr (-> m m)))) |
|---|
| 327 | " const char *s = mecab_nbest_next_tostr(m->m); |
|---|
| 328 | return s ? SCM_MAKE_STR_COPYING(s) : SCM_FALSE;") |
|---|
| 329 | |
|---|
| 330 | (define-cproc %mecab-nbest-next-tonode (m::<mecab>) |
|---|
| 331 | (result (make-mecab-node (mecab-nbest-next-tonode (-> m m))))) |
|---|
| 332 | ;" mecab_node_t *node = mecab_nbest_next_tonode(m->m); |
|---|
| 333 | ; return node ? make_mecab_node(node) : SCM_FALSE;") |
|---|
| 334 | |
|---|
| 335 | (define-cproc %mecab-sparse-tonode (m::<mecab> str::<const-cstring>) |
|---|
| 336 | (result (make-mecab-node (mecab-sparse-tonode (-> m m) str)))) |
|---|
| 337 | |
|---|
| 338 | (define-cproc %mecab-sparse-tonode2 (m::<mecab> str::<const-cstring> siz::<uint>) |
|---|
| 339 | (result (make-mecab-node (mecab-sparse-tonode2 (-> m m) str siz)))) |
|---|
| 340 | |
|---|
| 341 | (define-cproc %mecab-dictionary-info (m::<mecab>) |
|---|
| 342 | (result (make-mecab-dictionary-info (mecab-dictionary-info (-> m m))))) |
|---|
| 343 | |
|---|
| 344 | (define-cproc %mecab-get-partial (m::<mecab>) |
|---|
| 345 | ::<int> |
|---|
| 346 | (result (mecab-get-partial (-> m m)))) |
|---|
| 347 | |
|---|
| 348 | (define-cproc %mecab-set-partial (m::<mecab> partial::<int>) |
|---|
| 349 | ::<void> |
|---|
| 350 | (mecab-set-partial (-> m m) partial)) |
|---|
| 351 | |
|---|
| 352 | (define-cproc %mecab-get-theta (m::<mecab>) |
|---|
| 353 | ::<float> |
|---|
| 354 | (result (mecab-get-theta (-> m m)))) |
|---|
| 355 | |
|---|
| 356 | (define-cproc %mecab-set-theta (m::<mecab> theta::<float>) |
|---|
| 357 | ::<void> |
|---|
| 358 | (mecab-set-theta (-> m m) theta)) |
|---|
| 359 | |
|---|
| 360 | (define-cproc %mecab-get-lattice-level (m::<mecab>) |
|---|
| 361 | ::<int> |
|---|
| 362 | (result (mecab-get-lattice-level (-> m m)))) |
|---|
| 363 | |
|---|
| 364 | (define-cproc %mecab-set-lattice-level (m::<mecab> level::<int>) |
|---|
| 365 | ::<void> |
|---|
| 366 | (mecab-set-lattice-level (-> m m) level)) |
|---|
| 367 | |
|---|
| 368 | (define-cproc %mecab-get-all-morphs (m::<mecab>) |
|---|
| 369 | ::<int> |
|---|
| 370 | (result (mecab-get-all-morphs (-> m m)))) |
|---|
| 371 | |
|---|
| 372 | (define-cproc %mecab-set-all-morphs (m::<mecab> all_morphs::<int>) |
|---|
| 373 | ::<void> |
|---|
| 374 | (mecab-set-all-morphs (-> m m) all_morphs)) |
|---|
| 375 | |
|---|
| 376 | (define-cproc %mecab-format-node (m::<mecab> n::<mecab-node>) |
|---|
| 377 | ::<const-cstring>? |
|---|
| 378 | (result (mecab-format-node (-> m m) (-> n node)))) |
|---|
| 379 | |
|---|
| 380 | (define-cproc %mecab-dict-index (args::<list>) ::<int> |
|---|
| 381 | (let* ([argc::int (Scm_Length args)] |
|---|
| 382 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 383 | (result (mecab-dict-index argc argv)))) |
|---|
| 384 | |
|---|
| 385 | (define-cproc %mecab-dict-gen (args::<list>) ::<int> |
|---|
| 386 | (let* ([argc::int (Scm_Length args)] |
|---|
| 387 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 388 | (result (mecab-dict-gen argc argv)))) |
|---|
| 389 | |
|---|
| 390 | (define-cproc %mecab-cost-train (args::<list>) ::<int> |
|---|
| 391 | (let* ([argc::int (Scm_Length args)] |
|---|
| 392 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 393 | (result (mecab-cost-train argc argv)))) |
|---|
| 394 | |
|---|
| 395 | (define-cproc %mecab-system-eval (args::<list>) ::<int> |
|---|
| 396 | (let* ([argc::int (Scm_Length args)] |
|---|
| 397 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 398 | (result (mecab-system-eval argc argv)))) |
|---|
| 399 | |
|---|
| 400 | (define-cproc %mecab-test-gen (args::<list>) ::<int> |
|---|
| 401 | (let* ([argc::int (Scm_Length args)] |
|---|
| 402 | [argv::char** (Scm_ListToCStringArray args TRUE NULL)]) |
|---|
| 403 | (result (mecab-test-gen argc argv)))) |
|---|
| 404 | |
|---|
| 405 | ;; |
|---|
| 406 | ;; mecab_node_t |
|---|
| 407 | ;; |
|---|
| 408 | (define-cproc mecab-node-prev (n::<mecab-node>) |
|---|
| 409 | " const mecab_node_t *prev_node = n->node->prev; |
|---|
| 410 | return prev_node ? make_mecab_node(prev_node) : SCM_FALSE;") |
|---|
| 411 | |
|---|
| 412 | (define-cproc mecab-node-next (n::<mecab-node>) |
|---|
| 413 | " const mecab_node_t *next_node = n->node->next; |
|---|
| 414 | return next_node ? make_mecab_node(next_node) : SCM_FALSE;") |
|---|
| 415 | |
|---|
| 416 | (define-cproc mecab-node-enext (n::<mecab-node>) |
|---|
| 417 | " const mecab_node_t *enext_node = n->node->enext; |
|---|
| 418 | return enext_node ? make_mecab_node(enext_node) : SCM_FALSE;") |
|---|
| 419 | |
|---|
| 420 | (define-cproc mecab-node-bnext (n::<mecab-node>) |
|---|
| 421 | " const mecab_node_t *bnext_node = n->node->bnext; |
|---|
| 422 | return bnext_node ? make_mecab_node(bnext_node) : SCM_FALSE;") |
|---|
| 423 | |
|---|
| 424 | ;; NULL terminate�������ޤ���������Ƽ��Ф��ˤ�;; strncpy(buf, node->feature, node->length) �Ȥ������������ |
|---|
| 425 | (define-cproc %mecab-node-surface (n::<mecab-node>) |
|---|
| 426 | " char buf[n->node->length + 1]; |
|---|
| 427 | memcpy(buf, n->node->surface, n->node->length); |
|---|
| 428 | buf[n->node->length] = 0; |
|---|
| 429 | return SCM_MAKE_STR_COPYING(buf);"); |
|---|
| 430 | |
|---|
| 431 | (define-cproc %mecab-node-feature (n::<mecab-node>) ::<const-cstring> |
|---|
| 432 | (result (-> (-> n node) feature))) |
|---|
| 433 | |
|---|
| 434 | (define-cproc mecab-node-length (n::<mecab-node>) ::<uint> |
|---|
| 435 | (result (-> (-> n node) length))) |
|---|
| 436 | |
|---|
| 437 | (define-cproc mecab-node-rlength (n::<mecab-node>) ::<uint> |
|---|
| 438 | (result (-> (-> n node) rlength))) |
|---|
| 439 | |
|---|
| 440 | (define-cproc mecab-node-id (n::<mecab-node>) ::<uint> |
|---|
| 441 | (result (-> (-> n node) id))) |
|---|
| 442 | |
|---|
| 443 | (define-cproc mecab-node-rc-attr (n::<mecab-node>) ::<uint> |
|---|
| 444 | (result (-> (-> n node) rcAttr))) |
|---|
| 445 | |
|---|
| 446 | (define-cproc mecab-node-lc-attr (n::<mecab-node>) ::<uint> |
|---|
| 447 | (result (-> (-> n node) lcAttr))) |
|---|
| 448 | |
|---|
| 449 | (define-cproc mecab-node-posid (n::<mecab-node>) ::<uint> |
|---|
| 450 | (result (-> (-> n node) posid))) |
|---|
| 451 | |
|---|
| 452 | (define-cproc mecab-node-char-type (n::<mecab-node>) ::<uint> |
|---|
| 453 | (result (-> (-> n node) char-type))) |
|---|
| 454 | |
|---|
| 455 | (define-cproc %mecab-node-stat (n::<mecab-node>) ::<int> |
|---|
| 456 | (result (-> (-> n node) stat))) |
|---|
| 457 | |
|---|
| 458 | (define-cproc mecab-node-best? (n::<mecab-node>) ::<boolean> |
|---|
| 459 | (result (-> (-> n node) isbest))) |
|---|
| 460 | |
|---|
| 461 | (define-cproc mecab-node-alpha (n::<mecab-node>) ::<float> |
|---|
| 462 | (result (-> (-> n node) alpha))) |
|---|
| 463 | |
|---|
| 464 | (define-cproc mecab-node-beta (n::<mecab-node>) ::<float> |
|---|
| 465 | (result (-> (-> n node) beta))) |
|---|
| 466 | |
|---|
| 467 | (define-cproc mecab-node-prob (n::<mecab-node>) ::<float> |
|---|
| 468 | (result (-> (-> n node) prob))) |
|---|
| 469 | |
|---|
| 470 | (define-cproc mecab-node-wcost (n::<mecab-node>) ::<int> |
|---|
| 471 | (result (-> (-> n node) wcost))) |
|---|
| 472 | |
|---|
| 473 | (define-cproc mecab-node-cost (n::<mecab-node>) ::<int> |
|---|
| 474 | (result (-> (-> n node) cost))) |
|---|
| 475 | |
|---|
| 476 | ;; |
|---|
| 477 | ;; mecab_dictionary_info_t |
|---|
| 478 | ;; |
|---|
| 479 | ;; #define MECAB_USR_DIC 1 |
|---|
| 480 | ;; #define MECAB_SYS_DIC 0 |
|---|
| 481 | ;; #define MECAB_UNK_DIC 2 |
|---|
| 482 | ;; |
|---|
| 483 | (define-cproc mecab-dictionary-info-filename (dinfo::<mecab-dictionary-info>) |
|---|
| 484 | ::<const-cstring> |
|---|
| 485 | (result (-> (-> dinfo dic_info) filename))) |
|---|
| 486 | |
|---|
| 487 | (define-cproc mecab-dictionary-info-charset (dinfo::<mecab-dictionary-info>) |
|---|
| 488 | ::<const-cstring> |
|---|
| 489 | (result (-> (-> dinfo dic_info) charset))) |
|---|
| 490 | |
|---|
| 491 | (define-cproc mecab-dictionary-info-size (dinfo::<mecab-dictionary-info>) |
|---|
| 492 | ::<uint> (result (-> (-> dinfo dic_info) size))) |
|---|
| 493 | |
|---|
| 494 | (define-cproc %mecab-dictionary-info-type (dinfo::<mecab-dictionary-info>) |
|---|
| 495 | ::<int> (result (-> (-> dinfo dic_info) type))) |
|---|
| 496 | |
|---|
| 497 | (define-cproc mecab-dictionary-info-lsize (dinfo::<mecab-dictionary-info>) |
|---|
| 498 | ::<uint> (result (-> (-> dinfo dic_info) lsize))) |
|---|
| 499 | |
|---|
| 500 | (define-cproc mecab-dictionary-info-rsize (dinfo::<mecab-dictionary-info>) |
|---|
| 501 | ::<uint> (result (-> (-> dinfo dic_info) rsize))) |
|---|
| 502 | |
|---|
| 503 | (define-cproc mecab-dictionary-info-version (dinfo::<mecab-dictionary-info>) |
|---|
| 504 | ::<uint> (result (-> (-> dinfo dic_info) version))) |
|---|
| 505 | |
|---|
| 506 | (define-cproc mecab-dictionary-info-next (dinfo::<mecab-dictionary-info>) |
|---|
| 507 | (result (make-mecab-dictionary-info (-> (-> dinfo dic_info) next)))) |
|---|
| 508 | |
|---|
| 509 | ) |
|---|
| 510 | |
|---|
| 511 | (define-macro (mecab? obj) `(is-a? ,obj <mecab>)) |
|---|
| 512 | (define-macro (mecab-node? obj) `(is-a? ,obj <mecab-node>)) |
|---|
| 513 | (define-macro (mecab-dictionary-info? obj) `(is-a? ,obj <mecab-dictionary-info>)) |
|---|
| 514 | |
|---|
| 515 | (define-method write-object ((m <mecab>) out) |
|---|
| 516 | (format out "#<mecab>")); (mecab-version))) |
|---|
| 517 | (define-method write-object ((m <mecab-node>) out) |
|---|
| 518 | (format out "#<mecab-node>")) |
|---|
| 519 | (define-method write-object ((m <mecab-dictionary-info>) out) |
|---|
| 520 | (format out "#<mecab-dictionary-info>")) |
|---|
| 521 | |
|---|
| 522 | (define-reader-ctor '<mecab> mecab-new2) |
|---|
| 523 | |
|---|
| 524 | (define (mecab-tagger paramstr) |
|---|
| 525 | (let1 mecabobj (mecab-new2 paramstr) |
|---|
| 526 | (define (parse-to-string str . args) |
|---|
| 527 | (let-optionals* args ((len #f)) |
|---|
| 528 | (if len |
|---|
| 529 | (mecab-sparse-tostr2 mecabobj str len) |
|---|
| 530 | (mecab-sparse-tostr mecabobj str) ))) |
|---|
| 531 | |
|---|
| 532 | (define (parse-to-node str) |
|---|
| 533 | (mecab-sparse-tonode mecabobj str)) |
|---|
| 534 | |
|---|
| 535 | ;; ���ε�ǽ������� ��ư�����ץ����Ȥ���-l 1 ��ꤹ����������� |
|---|
| 536 | (define (parse-nbest n str) |
|---|
| 537 | (mecab-nbest-sparse-tostr mecabobj n str)) |
|---|
| 538 | |
|---|
| 539 | (define (parse-nbest-init str) |
|---|
| 540 | (mecab-nbest-init mecabobj str)) |
|---|
| 541 | |
|---|
| 542 | (define (next) |
|---|
| 543 | (mecab-nbest-next-tostr mecabobj)) |
|---|
| 544 | |
|---|
| 545 | (define (next-node) |
|---|
| 546 | (mecab-nbest-next-tonode mecabobj)) |
|---|
| 547 | |
|---|
| 548 | (define (format-node node) |
|---|
| 549 | (mecab-format-node mecabobj node)) |
|---|
| 550 | |
|---|
| 551 | (lambda (m) |
|---|
| 552 | (case m |
|---|
| 553 | [(parse parse-to-string) parse-to-string] |
|---|
| 554 | [(parse-to-node) parse-to-node] |
|---|
| 555 | [(parse-nbest) parse-nbest] |
|---|
| 556 | [(parse-nbest-init) parse-nbest-init] |
|---|
| 557 | [(next) next] |
|---|
| 558 | [(next-node) next-node] |
|---|
| 559 | [(format-node) format-node] |
|---|
| 560 | )))) |
|---|
| 561 | |
|---|
| 562 | ;;; class |
|---|
| 563 | (define-class <mecab-tagger> () (mecab #f)) |
|---|
| 564 | (define (mecab-make-tagger paramstr) |
|---|
| 565 | (make <mecab-tagger> :mecab (mecab-new2 paramstr))) |
|---|
| 566 | (define (tagger-mecab tagger) (slot-ref tagger 'mecab)) |
|---|
| 567 | (define-method parse ((tagger <mecab-tagger>) (str <string>)) |
|---|
| 568 | (mecab-sparse-tostr (tagger-mecab tagger) str)) |
|---|
| 569 | (define-method parse ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
|---|
| 570 | (mecab-sparse-tostr2 (tagger-mecab tagger) str len)) |
|---|
| 571 | (define-method parse-to-string ((tagger <mecab-tagger>) (str <string>)) |
|---|
| 572 | (mecab-sparse-tostr (tagger-mecab tagger) str)) |
|---|
| 573 | (define-method parse-to-string ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
|---|
| 574 | (mecab-sparse-tostr (tagger-mecab tagger) str len)) |
|---|
| 575 | (define-method parse-to-node ((tagger <mecab-tagger>) (str <string>)) |
|---|
| 576 | (mecab-sparse-tonode (tagger-mecab tagger) str)) |
|---|
| 577 | (define-method parse-to-node ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
|---|
| 578 | (mecab-sparse-tonode2 (tagger-mecab tagger) str len)) |
|---|
| 579 | (define-method parse-nbest ((tagger <mecab-tagger>) (n <integer>) (str <string>)) |
|---|
| 580 | (mecab-nbest-sparse-tostr (tagger-mecab tagger) str)) |
|---|
| 581 | (define-method parse-nbest ((tagger <mecab-tagger>) (n <integer>) (str <string>) (len <integer>)) |
|---|
| 582 | (mecab-nbest-sparse-tostr (tagger-mecab tagger) str len)) |
|---|
| 583 | (define-method parse-nbest-init ((tagger <mecab-tagger>) (str <string>)) |
|---|
| 584 | (mecab-nbest-init (tagger-mecab tagger) str)) |
|---|
| 585 | (define-method parse-nbest-init ((tagger <mecab-tagger>) (str <string>) (len <integer>)) |
|---|
| 586 | (mecab-nbest-init (tagger-mecab tagger) str len)) |
|---|
| 587 | (define-method next ((tagger <mecab-tagger>)) |
|---|
| 588 | (mecab-nbest-next-tostr (tagger-mecab tagger))) |
|---|
| 589 | (define-method next-node ((tagger <mecab-tagger>)) |
|---|
| 590 | (mecab-nbest-next-tonode (tagger-mecab tagger))) |
|---|
| 591 | (define-method format-node ((tagger <mecab-tagger>) (node <mecab-node>)) |
|---|
| 592 | (mecab-format-node (tagger-mecab tagger) node)) |
|---|
| 593 | |
|---|
| 594 | (provide "text/mecab") |
|---|
| 595 | |
|---|
| 596 | ;; Local variables: |
|---|
| 597 | ;; mode: scheme |
|---|
| 598 | ;; end: |
|---|