Index: lang/scheme/gauche/bindings/cabocha/trunk/test2.scm
===================================================================
--- lang/scheme/gauche/bindings/cabocha/trunk/test2.scm (revision 124)
+++ lang/scheme/gauche/bindings/cabocha/trunk/test2.scm (revision 124)
@@ -0,0 +1,149 @@
+;;;
+;;; Test cabocha
+;;;
+;;;  2009.3.15 by naoya_t
+;;;
+
+(use gauche.test)
+(use srfi-1)
+
+(test-start "cabocha")
+(use cabocha)
+(test-module 'cabocha)
+
+(define c (cabocha-new))
+(test* "cabocha?" #t (cabocha? c))
+;(test* "cabocha-destroyed?" #f (cabocha-destroyed? c))
+;;
+
+(define (cabocha-chunk-desc ch)
+  (format "link:~d head:~d func:~d token-size:~d token-pos:~d score:~d feature:~a"
+		  (cabocha-chunk-link ch)
+		  (cabocha-chunk-head-pos ch)
+		  (cabocha-chunk-func-pos ch)
+		  (cabocha-chunk-token-size ch)
+		  (cabocha-chunk-token-pos ch)
+		  (cabocha-chunk-score ch)
+		  (cabocha-chunk-feature-list ch)
+;		  (cabocha-chunk-feature-list-size ch)
+		  ))
+
+(define (cabocha-token-desc tok)
+  (format "surface:~a (~a) feature:~a feature-list:~a ne:~a chunk:~a"
+		  (cabocha-token-surface tok)
+		  (cabocha-token-normalized-surface tok)
+		  (cabocha-token-feature tok)
+		  (cabocha-token-feature-list tok)
+;		  (cabocha-token-feature-list-size tok)
+		  (cabocha-token-ne tok)
+;		  (cabocha-chunk-desc (cabocha-token-chunk tok))
+		  (cabocha-token-chunk tok)
+		  ))
+(define (cabocha-token->lisp tok)
+  `(token ;(format "surface:~a (~a) feature:~a feature-list:~a ne:~a chunk:~a"
+;	( ,(cabocha-token-surface tok) . ,(cabocha-token-normalized-surface tok) )
+	,(cabocha-token-normalized-surface tok)
+	;(cabocha-token-feature tok)
+	,(cabocha-token-feature-list tok)
+;		  (cabocha-token-feature-list-size tok)
+	;(cabocha-token-ne tok)
+;		  (cabocha-chunk-desc (cabocha-token-chunk tok))
+	;(cabocha-token-chunk tok)
+	))
+
+(define (vector-range vec from size)
+  (let1 vec* (make-vector size)
+	(dotimes (i size)
+	  (vector-set! vec* i (vector-ref vec (+ from i))))
+	vec*))
+
+(define (cabocha-chunk->lisp i ch tokens)
+  (let* ([token-pos (cabocha-chunk-token-pos ch)]
+		 [token-size (cabocha-chunk-token-size ch)]
+		 [tokens-in-chunk (vector-range tokens token-pos token-size)]
+		 [token-head-pos (cabocha-chunk-head-pos ch)]
+		 [token-func-pos (cabocha-chunk-func-pos ch)]
+		)
+	`(chunk ;(format "link:~d head:~d func:~d token-size:~d token-pos:~d score:~d feature:~a"
+	  ,i
+	  ,(cabocha-chunk-link ch)
+;	  ,(map token-surface (vector->list tokens-in-chunk))
+;	  (head ,(token-surface (vector-ref tokens-in-chunk token-head-pos)))
+;	  (func ,(token-surface (vector-ref tokens-in-chunk token-func-pos)))
+	  ,tokens-in-chunk
+	  ,token-head-pos
+	  ,token-func-pos
+;	  ,token-size
+;	  ,token-pos
+	  ,(cabocha-chunk-score ch)
+;	  ,(cabocha-chunk-feature-list ch)
+;		  (cabocha-chunk-feature-list-size ch)
+	  )))
+(define (pp-chunk chunk)
+;  (print " % " chunk)
+  (let1 tokens-in-chunk (fourth chunk)
+	(format #t "~d) => ~d ~a // head=~a func=~a score:~a\n"
+			(second chunk)
+			(third chunk)
+			(map token-surface (vector->list tokens-in-chunk))
+			(token-surface (vector-ref tokens-in-chunk (fifth chunk)))
+			(token-surface (vector-ref tokens-in-chunk (sixth chunk)))
+			(seventh chunk) )))
+
+(define (token-surface token) (cadr token))
+
+#|
+(define (cabocha-tree-chunk-list t)
+  (let loop ([i (- (cabocha-tree-chunk-size t) 1)] [lis '()])
+	(if (< i 0) lis
+		(loop (- i 1)
+			  (cons (cabocha-chunk->lisp i (cabocha-tree-chunk t i)) lis) ))))
+
+(define (cabocha-tree-token-list t)
+  (let loop ([i (- (cabocha-tree-token-size t) 1)] [lis '()])
+	(if (< i 0) lis
+		(loop (- i 1)
+			  (cons (cabocha-token->lisp (cabocha-tree-token t i)) lis) ))))
+|#
+
+(define (cabocha-tree-chunks t)
+  (let* ([tokens (cabocha-tree-tokens t)]
+		 [chunk-size (cabocha-tree-chunk-size t)]
+		 [vec (make-vector chunk-size)])
+	(dotimes (i chunk-size)
+	  (vector-set! vec i (cabocha-chunk->lisp i (cabocha-tree-chunk t i) tokens) ))
+	vec))
+
+(define (cabocha-tree-tokens t)
+  (let* ([token-size (cabocha-tree-token-size t)]
+		 [vec (make-vector token-size)])
+	(dotimes (i token-size)
+	  (vector-set! vec i (cabocha-token->lisp (cabocha-tree-token t i)) ))
+	vec))
+
+(define (cparse sentence)
+  (let* ([s (string-append sentence "。")]
+		 [tree (cabocha-sparse-totree c s)]
+		 )
+	(format #t "\n「~a」\n" s)
+
+;	(cabocha-tree-dump tree)
+	(let* ([token-size (cabocha-tree-token-size tree)]
+		   [chunk-size (cabocha-tree-chunk-size tree)]
+		   [chunks (cabocha-tree-chunks tree)]
+		   )
+	  (format #t "token size: ~d, " token-size)
+	  (format #t "chunk size: ~d\n" chunk-size)
+	  (dotimes (i chunk-size)
+		(pp-chunk (vector-ref chunks i)))
+	  )))
+										;				(format #t " - ~s\n" (cabocha-tree-sentence tree))
+										;			  (display (cabocha-sparse-tostr c s)))
+
+(load "sentences.scm")
+(for-each cparse sentences)
+
+;;
+(cabocha-destroy c)
+;(test* "cabocha-destroyed?" #t (cabocha-destroyed? c))
+(test-end)
Index: lang/scheme/gauche/bindings/cabocha/trunk/wrap-test.scm
===================================================================
--- lang/scheme/gauche/bindings/cabocha/trunk/wrap-test.scm (revision 124)
+++ lang/scheme/gauche/bindings/cabocha/trunk/wrap-test.scm (revision 124)
@@ -0,0 +1,53 @@
+(require "./wrap")
+
+;(use gauche.test)
+;(test-start "cabocha")
+;(test-module 'cabocha)
+
+(define c (cabocha-new))
+;(test* "cabocha?" #t (cabocha? c))
+
+(define (pp-chunk chunk)
+  (let1 tokens-in-chunk (fourth chunk)
+;	(format #t "~d) => ~d ~a // head=~a func=~a score:~a\n"
+;	(format #t "~a // head=~a func=~a score:~a\n"
+	(format #t "~a // head=~a func=~a\n"
+;			(second chunk)
+;			(third chunk)
+			(map token-surface (vector->list tokens-in-chunk))
+			(token-surface (vector-ref tokens-in-chunk (fifth chunk)))
+			(token-surface (vector-ref tokens-in-chunk (sixth chunk)))
+;			(seventh chunk)
+			)))
+
+(define (cparse sentence)
+  (let* ([s (string-append sentence "。")]
+		 [tree (cabocha-sparse-totree c s)]
+		 )
+	(format #t "\n「~a」\n" s)
+	(let* ([token-size (cabocha-tree-token-size tree)]
+		   [chunk-size (cabocha-tree-chunk-size tree)]
+		   [chunk-list (cabocha-tree-chunk-list tree)])
+	  (format #t "token size: ~d, " token-size)
+	  (format #t "chunk size: ~d\n" chunk-size)
+;	  (for-each pp-chunk chunk-list)
+;	  (print "-")
+	  (let1 terminal-chunk (find (lambda (c) (= -1 (third c))) chunk-list)
+		;(pp-chunk terminal-chunk)
+		(let loop ((level 0) (chunk terminal-chunk))
+		  (dotimes (i level) (display ": ")) (display "+- ")
+		  (pp-chunk chunk)
+		  (let1 link-from (filter (lambda (c) (= (second chunk) (third c))) chunk-list)
+			;(map pp-chunk link-from)
+			(map (cut loop (+ level 1) <>) link-from))
+;		  (dotimes (i level) (display ": ")) (newline)
+		  ))
+	  )))
+
+;(load "sentences.scm")
+;(for-each cparse sentences)
+(load "sentences.scm")
+(for-each cparse sentences)
+
+(cabocha-destroy c)
+;(test-end)
Index: lang/scheme/gauche/bindings/cabocha/trunk/sentences.scm
===================================================================
--- lang/scheme/gauche/bindings/cabocha/trunk/sentences.scm (revision 124)
+++ lang/scheme/gauche/bindings/cabocha/trunk/sentences.scm (revision 124)
@@ -0,0 +1,7 @@
+(define (split-by-maru text)
+  (reverse! (cdr (reverse! (string-split text #\。)))))
+
+(define sentences (append-map split-by-maru (list
+"ここに何か適当な文章を入れてください。"
+"そうすると良いことがあなたにあるかもしれません。"
+)))
Index: lang/scheme/gauche/bindings/cabocha/trunk/wrap.scm
===================================================================
--- lang/scheme/gauche/bindings/cabocha/trunk/wrap.scm (revision 124)
+++ lang/scheme/gauche/bindings/cabocha/trunk/wrap.scm (revision 124)
@@ -0,0 +1,76 @@
+;;;
+;;; cabocha wrapper
+;;;
+;;;  2009.3.22 by naoya_t
+;;;
+
+(use srfi-1)
+(use cabocha)
+
+;;; lib
+(define (vector-range vec from size)
+  (let1 vec* (make-vector size)
+	(dotimes (i size)
+	  (vector-set! vec* i (vector-ref vec (+ from i))))
+	vec*))
+
+;;; cabocha token/chunk -> sexp
+(define (cabocha-token->sexp tok)
+  `(cabocha-token ,(cabocha-token-normalized-surface tok)
+				  ,(cabocha-token-feature-list tok)
+				  ))
+
+(define (token-surface token) (second token))
+(define (token-feature token) (third token))
+
+(define (cabocha-chunk->sexp i ch tokens)
+  (let* ([token-pos (cabocha-chunk-token-pos ch)]
+		 [token-size (cabocha-chunk-token-size ch)]
+		 [tokens-in-chunk (vector-range tokens token-pos token-size)]
+		 [token-head-pos (cabocha-chunk-head-pos ch)]
+		 [token-func-pos (cabocha-chunk-func-pos ch)])
+	`(cabocha-chunk ,i
+					,(cabocha-chunk-link ch)
+					,tokens-in-chunk
+					,token-head-pos
+					,token-func-pos
+					,(cabocha-chunk-score ch)
+					)))
+
+#|
+(define (cabocha-tree-chunk-list t)
+  (let loop ([i (- (cabocha-tree-chunk-size t) 1)] [lis '()])
+	(if (< i 0) lis
+		(loop (- i 1)
+			  (cons (cabocha-chunk->sexp i (cabocha-tree-chunk t i)) lis) ))))
+
+(define (cabocha-tree-token-list t)
+  (let loop ([i (- (cabocha-tree-token-size t) 1)] [lis '()])
+	(if (< i 0) lis
+		(loop (- i 1)
+			  (cons (cabocha-token->sexp (cabocha-tree-token t i)) lis) ))))
+|#
+
+(define (cabocha-tree-chunks t) ;vec
+  (let* ([tokens (cabocha-tree-tokens t)]
+		 [chunk-size (cabocha-tree-chunk-size t)]
+		 [vec (make-vector chunk-size)])
+	(dotimes (i chunk-size)
+	  (vector-set! vec i (cabocha-chunk->sexp i (cabocha-tree-chunk t i) tokens) ))
+	vec))
+(define (cabocha-tree-chunk-list t) ;list
+  (let ([tokens (cabocha-tree-tokens t)]
+		[chunk-size (cabocha-tree-chunk-size t)])
+	(map (lambda (i) (cabocha-chunk->sexp i (cabocha-tree-chunk t i) tokens))
+		 (iota chunk-size))))
+
+(define (cabocha-tree-tokens t)
+  (let* ([token-size (cabocha-tree-token-size t)]
+		 [vec (make-vector token-size)])
+	(dotimes (i token-size)
+	  (vector-set! vec i (cabocha-token->sexp (cabocha-tree-token t i)) ))
+	vec))
+(define (cabocha-tree-token-list t)
+  (let1 token-size (cabocha-tree-token-size t)
+	(map (lambda (i) (cabocha-token->sexp (cabocha-tree-token t i)))
+		 (iota token-size))))
