mots quotidiens.
Daichi Mochihashi (持橋大地) daichi <at> ism.ac.jp by hns, version 2.10-pl1.

先月 2024年05月 来月
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

2014年06月20日(金) [n年日記]

#1 木構造

木構造を画面に表示したい時, 一番簡単なのは (S NP VP) のようなS式で表現すること ですが, 長くなると読みづらくなるので, 分かりやすく表示するには, 簡単には例えば guileで次のような短いスクリプトを書けばいいようです。
#!/fink/bin/guile \
-e main -s
;; simple pretty printer of S expressions.
;; $Id: pp.scm,v 1.4 2014/06/21 13:05:00 daichi Exp $
 !#
(use-modules (ice-9 pretty-print))

(define (pretty-print-port port)
  (let ((s (read port)))
    (if (eof-object? s)
        (close-input-port port)
        (begin (pretty-print s) (pretty-print-port port)))))

(define (pretty-print-file file)
  (let ((port (open-input-file file)))
    (pretty-print-port port)))

(define (pretty-print-stdin)
  (pretty-print-port (current-input-port)))

(define (main args)
  (let ((files (cdr args)))
    (if (eq? files '())
        (pretty-print-stdin)
        (map pretty-print-file files))))
結果は下のような感じ。ちなみに, 日本語が入っていても問題なく通ります。
% echo '(S (NP (DT the) (N man)) (VP (VP (V gives) (NP (DT a) (N man))) (NP (DT a) (N present))))' | ./pp.scm
(S (NP (DT the) (N man))
   (VP (VP (V gives) (NP (DT a) (N man)))
       (NP (DT a) (N present))))
これだと標準入力をすべてS式だと思ってしまうので, "("が行頭にある時(たとえば) だけ上のフィルタを通すには, 次のようなawkスクリプトを書くと上手くいきました。
#!/usr/bin/gawk -f
/^\(/ {
    print | "pp.scm";
    next;
}
{
    print;
}


2013年06月20日(木) [n年日記]

#1 -

読んだので, 自分用メモ。
統数研の40周年記念時の, 林知己夫所長による文書「40周年を記念する号 発刊に当って」(1984年)。
統計モデルの意義について, 非常に考えさせられる内容で, 統数研で言われていることの意味もわかったような気がします。 http://ismrepo.ism.ac.jp/dspace/bitstream/10787/2852/1/ihou1984-a_004.pdf


2009年06月20日() [n年日記]

#1 -

下の math++.el は math.el を基にしたものですが, 実は複数行の編集に対応して いない等がわかったので, mathematica.el を基に一からモードを書き直したもの を math++.el として上げておきました。


Mathematica愉快すぎる。 2次元のMixture of Gaussiansのコードはこんな感じになるようです (NormalDistribution[]は2次元には対応していない。)

(*
   gaussian.ma
   Sat Jun 20 00:12:25 2009 daichi<at>cslab.kecl.ntt.co.jp
 *)

gauss[x_,y_,mu_,S_] :=
    Block[
        {v = {x,y}},
        1 / (2 Pi Sqrt[Abs[Det[S]]])
        Exp[- (v - mu) . Inverse[S] . (v - mu) / 2]
    ]
gauss::usage = "
gauss[x,y,mu,S] returns a Gaussian density of mean mu, covariance S
at the coordinate {x,y}.";

mgauss[x_,y_,Pi_,Mu_,S_] :=
    If[ MatchQ[{}, Pi],
        0,
        Block[
            {pi = First[Pi],
             mu = First[Mu],
             s  = First[S]},
            pi gauss[x, y, mu, s] +
            mgauss[x, y, Rest[Pi], Rest[Mu], Rest[S]]
        ]
    ]
mgauss::usage = "
mgauss[x,y,Pi,Mu,S] returns a mixture of Gaussians of
gauss[{x,y},Mu[[1]],S[[1]]], gauss[{x,y},Mu[[2]],S[[2]]], ...
with mixing coefficients Pi.";

後ろのコードは, scheme で書くと

(define (mgauss x y Pi Mu S)
  (if (null? Pi)
      0
      (let ((pi (car Pi))
            (mu (car Mu))
            (s  (car S)))
        (+ (* pi (gauss x y mu s))
           (mgauss x y (cdr Pi) (cdr Mu) (cdr S))))))
で, まんまLispなのがよくわかる感じです。(car=First, cdr=Restらしい。)

3 days displayed.
タイトル一覧
カテゴリ分類
 なかのひと
Powered by hns-2.10-pl1, HyperNikkiSystem Project