言語処理100本ノックを Gauche でやってみる(1)

こんなの見つけましたのでGaucheでやってみようかと思いました www.cl.ecei.tohoku.ac.jp

第一章 4番までなんとかやってみました。 超時間かかっちゃった・・・

コード

以下コード。 汚いなぁ orz

#!/usr/bin/env gosh
(use srfi-1)
(use srfi-13)
(use gauche.sequence)
(use gauche.collection)
(use text.tree)



(define (nlp100-sample)
  "hello")

(define (nlp100-00)
  (list->string (reverse (string->list "stressed"))))

(define (nlp100-01)
  (define (getchar s i)
    (subseq s (- i 1) i))
  (define (getstr str lst)
    (map (lambda (n) (getchar str n)) lst))
  (tree->string (getstr "パタトクカシーー" '#(1 3 5 7))))

(define (nlp100-02)
  (define (getchar s i)
    (subseq s (- i 1) i))
  (define (getstr str1 str2)
    (cond
      ((null? str1) '())
      (else
        (cons (cons (car str1) (car str2)) (getstr (cdr str1) (cdr str2))))))
  (tree->string (getstr (string->list "パトカー") (string->list "タクシー"))))


(define (nlp100-03)
  (define (getchar lst)
    (cond
      ((null? lst) '())
      (else
        (cons (string-length (string-filter char-alphabetic? (car lst))) (getchar (cdr lst))))))
  (tree->string
    (getchar
      (string-split 
        "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." " "))))

(define (nlp100-04)
  (define (mkalist lst)
    (cond
      ((null? lst) '())
      ((assoc (subseq (car lst) 0 1) (mkalist (cdr lst)))
        (acons (subseq (car lst) 0 2) (car lst) (mkalist (cdr lst))))
      (else
        (acons (subseq (car lst) 0 1) (car lst) (mkalist (cdr lst))))))
  (mkalist (string-split "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." " ") ))

テストも書いたお

#!/usr/bin/env gosh
(use gauche.test)

(test-start "ch01-01")
(load "./main.gosh")

(test-section "01")
(test "nlp100-sample" "hello" nlp100-sample)
(test "nlp100-00" "desserts" nlp100-00)
(test "nlp100-01" "パトカー" nlp100-01)
(test "nlp100-02" "パタトクカシーー" nlp100-02)
(test "nlp100-03" "314159265358979" nlp100-03)
(test "nlp100-04" '(("Hi" . "Hi") ("H" . "He") ("L" . "Lied") ("Be" . "Because") ("B" . "Boron") ("Co" . "Could") ("No" . "Not") ("O" . "Oxidize") ("F" . "Fluorine.") ("Ne" . "New") ("N" . "Nations") ("M" . "Might") ("Al" . "Also") ("Si" . "Sign") ("P" . "Peace") ("S" . "Security") ("Cl" . "Clause.") ("A" . "Arthur") ("K" . "King") ("C" . "Can.")) nlp100-04)

結果

~/work/repos/nlp100-gosh/ch01$ ./test.gosh 
Testing ch01-01 ...                                              
<01>---------------------------------------------------------------------------
test nlp100-sample, expects "hello" ==> ok
test nlp100-00, expects "desserts" ==> ok
test nlp100-01, expects "パトカー" ==> ok
test nlp100-02, expects "パタトクカシーー" ==> ok
test nlp100-03, expects "314159265358979" ==> ok
test nlp100-04, expects (("Hi" . "Hi") ("H" . "He") ("L" . "Lied") ("Be" . "Because") ("B" . "Boron") ("Co" . "Could") ("No" . "Not") ("O" . "Oxidize") ("F" . "Fluorine.") ("Ne" . "New") ("N" . "Nations") ("M" . "Might") ("Al" . "Also") ("Si" . "Sign") ("P" . "Peace") ("S" . "Security") ("Cl" . "Clause.") ("A" . "Arthur") ("K" . "King") ("C" . "Can.")) ==> ok

つづく。(かも