mots quotidiens. | |
Daichi Mochihashi (持橋大地) daichi <at> ism.ac.jp | by hns, version 2.10-pl1. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||
#!/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; }
(* 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らしい。)
タイトル一覧 |