5. Принципы Lisp
* Всё — выражение
* Всё — первоклассное
* Программа — это живой образ
* Ядро языка — 25 базовых форм,
всё остальное — надстройка над ними,
которую можно менять
12. Оборачиваем черные ящики – клиент redis
(def-cmd HMGET (key field &rest fields) :multi
"Get the values associated with the specified FIELDS
in the hash stored at KEY.")
http://lisp-univ-etc.blogspot.com/2012/11/cl-redis-separation-of-concerns-in.html
15. Оборачиваем сложные алгоритмы - CKY
(macrolet
((CKY (&body body)
`(with-slots (rules nts->idx) grammar
(let* ((pi0 #{}) (bps #{}) ;; also need to init them
(min most-negative-single-float))
(do ((pos 1 (1+ pos)))
((>= pos *sentence-length*))
(do ((i 1 (1+ i)))
((> i (- *sentence-length* pos)))
(let ((j (+ i pos)))
(dotable (_ k nts->idx)
(let (max arg)
(do ((s i (1+ s)))
((>= s j))
(dotable (rule q rules)
(when (and (tryadic rule)
(= k (first rule)))
,@body)))
(when (if (listp max) max (> max min))
(setf (@ pi0 (1- i) (1- j) k) max
(@ bps (1- i) (1- j) k) arg)))))))
(values pi0 bps)))))
16. Оборачиваем сложные алгоритмы - CKY
(defmethod parse ((grammar pcfg) (sentence list))
"Return the parse tree of SENTENCE for PCFG.
Parsing is performed in 2 steps:
- the main method returns as values the matrices PI0 (pi matrix)
and BPS (backpointers matrix)
- the :around method restores the parse tree from these matrices
This brings additional flexibility for the return values of the
method: for instance, we can try to return several trees or have
several roots."
(CKY (let* ((cur (cons rule (1- s)))
(l (@ pi0 (1- i) (1- s) (second rule)))
(r (@ pi0 s (1- j) (third rule)))
(score (if (and l r)
(+ (log q) l r)
min)))
(when (> score (or max min))
(setf max score
arg cur)))))