1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: sql-classes.lisp
6 ;;;; Purpose: Routines for reading UMLS objects from SQL database
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: sql-classes.lisp,v 1.4 2002/10/14 09:25:20 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
20 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
23 (defvar *current-srl* nil)
26 (defun current-srl! (srl)
27 (setq *current-srl* srl))
30 ;;; Accessors (read on demand)
32 ;; defines a slot-unbound method for class and slot-name, fills
33 ;; the slot by calling reader function with the slot values of
34 ;; the instance's reader-keys
35 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
36 (let* ((the-slot-name (gensym))
38 (the-instance (gensym))
40 (dolist (key reader-keys)
41 (push (list 'slot-value the-instance (list 'quote key)) keys))
42 (setq keys (nreverse keys))
43 `(defmethod slot-unbound (,the-class (,the-instance ,class)
44 (,the-slot-name (eql ',slot-name)))
45 (declare (ignore ,the-class))
46 (setf (slot-value ,the-instance ,the-slot-name)
49 (def-lazy-reader ucon s#term find-uterm-cui cui)
50 (def-lazy-reader ucon s#def find-udef-cui cui)
51 (def-lazy-reader ucon s#sty find-usty-cui cui)
52 (def-lazy-reader ucon s#rel find-urel-cui cui)
53 (def-lazy-reader ucon s#coc find-ucoc-cui cui)
54 (def-lazy-reader ucon s#lo find-ulo-cui cui)
55 (def-lazy-reader ucon s#atx find-uatx-cui cui)
56 (def-lazy-reader ucon s#sat find-usat-ui cui)
59 (def-lazy-reader uterm s#str find-ustr-cuilui cui lui)
60 (def-lazy-reader uterm s#sat find-usat-ui cui lui)
63 (def-lazy-reader ustr s#sat find-usat-ui cui lui sui)
64 (def-lazy-reader ustr s#cxt find-ucxt-cuisui cui sui)
65 (def-lazy-reader ustr s#so find-uso-cuisui cui sui)
69 ;;; Lookup functions for uterms,ustr in ucons
71 (defun find-uterm-in-ucon (ucon lui)
72 (find lui (s#term ucon) :key #'lui :test 'equal))
74 (defun find-ustr-in-uterm (uterm sui)
75 (find sui (s#str uterm) :key #'sui :test 'equal))
77 (defun find-ustr-in-ucon (ucon sui)
78 (let ((found-ustr nil))
79 (dolist (uterm (s#term ucon))
81 (dolist (ustr (s#str uterm))
83 (when (string-equal sui (sui ustr))
84 (setq found-ustr ustr))))))
88 (defun find-ucon-cui (cui &key (srl *current-srl*))
91 (setq cui (parse-cui cui)))
93 (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d"
96 (string-append ls (format nil " and KCUILRL <= ~d limit 1" srl))
97 (string-append ls " limit 1"))
98 (kmrcl:awhen (car (mutex-sql-query ls))
99 (make-instance 'ucon :cui cui :pfstr (car kmrcl::it)
100 :lrl (ensure-integer (cadr kmrcl::it)))))
103 (defun find-ucon-lui (lui &key (srl *current-srl*))
104 "Find list of ucon for lui"
106 (setq lui (parse-lui lui)))
109 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where LUI=~d" lui)))
111 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
112 (dolist (tuple (mutex-sql-query ls))
113 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
115 :lrl (ensure-integer (nth 2 tuple)))
120 (defun find-ucon-sui (sui &key (srl *current-srl*))
121 "Find list of ucon for sui"
123 (setq sui (parse-sui sui)))
126 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where SUI=~d" sui)))
128 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
129 (let ((tuples (mutex-sql-query ls)))
130 (dolist (tuple tuples)
131 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
133 :lrl (ensure-integer (nth 2 tuple)))
138 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
139 "Find ucon for cui/sui"
141 (setq cui (parse-cui cui)))
143 (setq sui (parse-sui sui)))
145 (let ((ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where KCUISUI=~d"
146 (make-cuisui cui sui))))
148 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
149 (kmrcl:aif (car (mutex-sql-query ls))
150 (make-instance 'ucon :cui (ensure-integer (nth 0 kmrcl::it))
151 :pfstr (nth 1 kmrcl::it)
152 :lrl (ensure-integer (nth 2 kmrcl::it)))
156 (defun find-ucon-str (str &key (srl *current-srl*))
157 "Find ucon that are exact match for str"
160 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where STR='~a'" str)))
162 (string-append ls " and KCUILRL <= ~d" srl))
163 (dolist (tuple (mutex-sql-query ls))
164 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
166 :lrl (ensure-integer (nth 2 tuple))) ucons))
170 (defun find-ucon-all (&key (srl *current-srl*))
171 "Return list of all ucon's"
172 (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON"))
174 (string-append ls (format nil " where KCUILRL <= ~d" srl)))
175 (string-append ls " order by CUI asc")
176 (with-sql-connection (db)
179 #'(lambda (cui pfstr cuilrl)
180 (make-instance 'ucon :cui (ensure-integer cui)
182 :lrl (ensure-integer cuilrl)))
188 (defun find-udef-cui (cui &key (srl *current-srl*))
189 "Return a list of udefs for cui"
191 (ls (format nil "select SAB,DEF from MRDEF where CUI=~d" cui)))
193 (string-append ls (format nil " and KSRL <= ~d" srl)))
194 (dolist (tuple (mutex-sql-query ls))
195 (push (make-instance 'udef :sab (car tuple) :def (cadr tuple)) udefs))
198 (defun find-usty-cui (cui &key (srl *current-srl*))
199 "Return a list of usty for cui"
201 (ls (format nil "select TUI,STY from MRSTY where CUI=~d" cui)))
203 (string-append ls (format nil " and KLRL <= ~d" srl)))
204 (dolist (tuple (mutex-sql-query ls))
205 (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys))
208 (defun find-usty-word (word &key (srl *current-srl*))
209 "Return a list of usty that match word"
211 (ls (format nil "select distinct TUI,STY from MRSTY where STY like '%~a%'" word)))
213 (string-append ls (format nil " and KLRL <= ~d" srl)))
214 (dolist (tuple (mutex-sql-query ls))
215 (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys))
218 (defun find-urel-cui (cui &key (srl *current-srl*))
219 "Return a list of urel for cui"
221 (ls (format nil "select REL,CUI2,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI1=~d" cui)))
223 (string-append ls (format nil " and KSRL <= ~d" srl)))
224 (dolist (tuple (mutex-sql-query ls))
225 (push (make-instance 'urel
228 :cui2 (ensure-integer (nth 1 tuple))
233 :pfstr2 (nth 6 tuple))
237 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
238 "Return a list of urel for cui2"
240 (ls (format nil "select REL,CUI1,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI2=~d" cui2)))
242 (string-append ls (format nil " and SRL <= ~d" srl)))
243 (dolist (tuple (mutex-sql-query ls))
244 (push (make-instance 'urel
247 :cui1 (ensure-integer (nth 1 tuple))
252 :pfstr2 (nth 6 tuple))
256 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
258 #'(lambda (cui) (find-ucon-cui cui :srl srl))
259 (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
261 (defun find-ucoc-cui (cui &key (srl *current-srl*))
262 "Return a list of ucoc for cui"
264 (ls (format nil "select CUI2,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI1=~d" cui)))
266 (string-append ls (format nil " and KLRL <= ~d" srl)))
267 (string-append ls " order by COF asc")
268 (dolist (tuple (mutex-sql-query ls))
269 (let ((cui2 (ensure-integer (nth 0 tuple))))
272 (push (make-instance 'ucoc :cui1 cui
276 :cof (ensure-integer (nth 3 tuple))
278 :pfstr2 (nth 5 tuple))
280 ucocs)) ;; akready ordered by SQL select
282 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
283 "Return a list of ucoc for cui2"
285 (ls (format nil "select CUI1,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI2=~d" cui2)))
287 (string-append ls (format nil " and KSRL <= ~d" srl)))
288 (string-append ls " order by COF asc")
289 (dolist (tuple (mutex-sql-query ls))
290 (push (make-instance 'ucoc :cui1 (ensure-integer (nth 0 tuple))
294 :cof (ensure-integer (nth 3 tuple))
296 :pfstr2 (nth 5 tuple))
298 ucocs)) ;; already ordered by SQL select
300 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
301 "List of ucon with co-occurance cui2"
303 #'(lambda (cui) (find-ucon-cui cui :srl srl))
304 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
306 (defun find-ulo-cui (cui &key (srl *current-srl*))
307 "Return a list of ulo for cui"
309 (ls (format nil "select ISN,FR,UN,SUI,SNA,SOUI from MRLO where CUI=~d" cui)))
311 (string-append ls (format nil " and KLRL <= ~d" srl)))
312 (dolist (tuple (mutex-sql-query ls))
313 (push (make-instance 'ulo :isn (nth 0 tuple)
314 :fr (ensure-integer (nth 1 tuple))
316 :sui (ensure-integer (nth 3 tuple))
322 (defmethod suistr ((lo ulo))
323 "Return the string for a ulo object"
324 (find-string-sui (sui lo)))
326 (defun find-uatx-cui (cui &key (srl *current-srl*))
327 "Return a list of uatx for cui"
329 (ls (format nil "select SAB,REL,ATX from MRATX where CUI=~d" cui)))
331 (string-append ls (format nil " and KSRL <= ~d" srl)))
332 (dolist (tuple (mutex-sql-query ls))
333 (push (make-instance 'uatx :sab (nth 0 tuple)
340 (defun find-uterm-cui (cui &key (srl *current-srl*))
341 "Return a list of uterm for cui"
343 (ls (format nil "select distinct LUI,LAT,TS,KLUILRL from MRCON where CUI=~d" cui)))
345 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
346 (dolist (tuple (mutex-sql-query ls))
347 (push (make-instance 'uterm :lui (ensure-integer (nth 0 tuple))
351 :lrl (ensure-integer (nth 3 tuple)))
355 (defun find-uterm-lui (lui &key (srl *current-srl*))
356 "Return a list of uterm for lui"
358 (setq lui (parse-lui lui)))
360 (ls (format nil "select distinct CUI,LAT,TS,KLUILRL from MRCON where LUI=~d" lui)))
362 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
363 (dolist (tuple (mutex-sql-query ls))
364 (push (make-instance 'uterm :cui (ensure-integer (nth 0 tuple))
368 :lrl (ensure-integer (nth 3 tuple)))
372 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
373 "Return single uterm for cui/lui"
374 (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
376 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
377 (kmrcl:aif (car (mutex-sql-query ls))
378 (make-instance 'uterm :cui cui
380 :lat (nth 0 kmrcl::it)
381 :ts (nth 1 kmrcl::it)
382 :lrl (ensure-integer (nth 2 kmrcl::it)))
385 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
386 "Return a list of ustr for cui/lui"
387 (declare (fixnum cui lui))
389 (ls (format nil "select SUI,STT,STR,LRL from MRCON where KCUILUI=~d" (make-cuilui cui lui))))
391 (string-append ls (format nil " and LRL <= ~d" srl)))
392 (dolist (tuple (mutex-sql-query ls))
393 (let* ((sui (ensure-integer (car tuple)))
394 (ustr (make-instance 'ustr :sui sui
396 :cuisui (make-cuisui cui sui)
400 :lrl (ensure-integer (nth 3 tuple)))))
404 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
405 "Return the single ustr for cuisui"
406 (let ((ls (format nil "select LUI,STT,STR,LRL from MRCON where KCUISUI=~d"
407 (make-cuisui cui sui))))
409 (string-append ls (format nil " and LRL <= ~d" srl)))
410 (kmrcl:aif (car (mutex-sql-query ls))
411 (make-instance 'ustr :sui sui
413 :cuisui (make-cuisui cui sui)
414 :lui (ensure-integer (nth 0 kmrcl::it))
415 :stt (nth 1 kmrcl::it)
416 :str (nth 2 kmrcl::it)
417 :lrl (ensure-integer (nth 3 kmrcl::it)))
420 (defun find-ustr-sui (sui &key (srl *current-srl*))
421 "Return the list of ustr for sui"
423 (setq sui (parse-sui sui)))
425 (ls (format nil "select CUI,LUI,STT,STR,LRL from MRCON where SUI=~d" sui)))
427 (string-append ls (format nil " and LRL <= ~d" srl)))
428 (dolist (tuple (mutex-sql-query ls))
429 (let ((cui (ensure-integer (car tuple))))
430 (push (make-instance 'ustr :sui sui
432 :cuisui (make-cuisui cui sui)
433 :lui (ensure-integer (nth 1 tuple))
436 :lrl (ensure-integer (nth 4 tuple)))
440 (defun find-ustr-sab (sab &key (srl *current-srl*))
441 "Return the list of ustr for sab"
443 (ls (format nil "select KCUISUI from MRSO where SAB='~a'" sab)))
445 (string-append ls (format nil " and SRL <= ~d" srl)))
446 (dolist (tuple (mutex-sql-query ls))
447 (let ((cuisui (ensure-integer (car tuple))))
448 (push (apply #'find-ustr-cuisui
450 (multiple-value-list (decompose-cuisui cuisui))
455 (defun find-ustr-all (&key (srl *current-srl*))
456 "Return list of all ustr's"
457 (let ((ls "select distinct CUI,LUI,SUI,STT,LRL,KPFSTR from MRCON"))
459 (string-append ls (format nil " where LRL <= ~d" srl)))
460 (string-append ls " order by SUI asc")
461 (with-sql-connection (db)
464 #'(lambda (cui lui sui stt lrl pfstr)
465 (setq cui (ensure-integer cui))
466 (setq lui (ensure-integer lui))
467 (setq sui (ensure-integer sui))
468 (setq lrl (ensure-integer lrl))
469 (make-instance 'ustr :cui cui
472 :cuisui (make-cuisui cui sui)
479 (defun find-string-sui (sui &key (srl *current-srl*))
480 "Return the string associated with sui"
481 (let ((ls (format nil "select STR from MRCON where SUI=~d" sui)))
483 (string-append ls (format nil " and LRL <= ~d" srl)))
484 (string-append ls " limit 1")
485 (caar (mutex-sql-query ls))))
487 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
488 (declare (fixnum cui sui))
490 (ls (format nil "select SAB,CODE,SRL,TTY from MRSO where KCUISUI=~d"
491 (make-cuisui cui sui))))
493 (string-append ls (format nil " and SRL <= ~d" srl)))
494 (dolist (tuple (mutex-sql-query ls))
495 (push (make-instance 'uso :sab (nth 0 tuple) :code (nth 1 tuple)
496 :srl (nth 2 tuple) :tty (nth 3 tuple))
500 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
501 (declare (fixnum cui sui))
503 (ls (format nil "select SAB,CODE,CXN,CXL,RNK,CXS,CUI2,HCD,RELA,XC from MRCXT where KCUISUI=~d"
504 (make-cuisui cui sui))))
506 (string-append ls (format nil " and KSRL <= ~d" srl)))
507 (dolist (tuple (mutex-sql-query ls))
508 (push (make-instance 'ucxt :sab (nth 0 tuple)
510 :cxn (ensure-integer (nth 2 tuple))
512 :rnk (ensure-integer (nth 4 tuple))
514 :cui2 (ensure-integer (nth 6 tuple))
521 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
522 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
524 (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
525 (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
526 (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
528 (string-append ls (format nil " and KSRL <= ~d" srl)))
530 (dolist (tuple (mutex-sql-query ls))
531 (push (make-instance 'usat :code (nth 0 tuple)
539 (defun find-pfstr-cui (cui)
540 (caar (mutex-sql-query (format nil "select KPFSTR from MRCON where CUI=~d limit 1" cui))))
542 (defun find-usty-tui (tui)
544 (setq tui (parse-tui tui))
545 (kmrcl:aif (car (mutex-sql-query
546 (format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
547 (make-instance 'usty :tui tui :sty (nth 0 kmrcl::it))
550 (defun find-usty-sty (sty)
551 "Find usty for a sty"
552 (kmrcl:aif (car (mutex-sql-query
553 (format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
554 (make-instance 'usty :tui (ensure-integer (nth 0 kmrcl::it)) :sty sty)
557 (defun find-usty-all ()
558 "Return list of usty's for all semantic types"
560 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
561 (push (find-usty-tui (nth 0 tuple)) ustys))
565 (defun find-cui-max ()
566 (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
567 (ensure-integer cui)))
569 ;;;; Cross table find functions
571 (defun find-ucon-tui (tui &key (srl *current-srl*))
572 "Find list of ucon for tui"
574 (setq tui (parse-tui tui)))
576 (ls (format nil "select CUI from MRSTY where TUI=~d" tui)))
578 (string-append ls (format nil " and KLRL <= ~d" srl)))
579 (string-append ls " order by cui desc")
580 (dolist (tuple (mutex-sql-query ls))
581 (push (find-ucon-cui (ensure-integer (car tuple)) :srl srl) ucons))
584 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
585 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
587 (ls (format nil "select distinct cui from MRXW_ENG where wd~A'~A'"
588 (if like " LIKE " "=")
591 (string-append ls (format nil " and KLRL <= ~d" srl)))
592 (string-append ls " order by cui desc")
593 (dolist (tuple (mutex-sql-query ls))
594 (push (find-ucon-cui (car tuple) :srl srl) ucons))
597 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
598 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
600 (ls (format nil "select distinct cui from MRXNW_ENG where nwd~A'~A'"
601 (if like " LIKE " "=")
604 (string-append ls (format nil " and KLRL <= ~d" srl)))
605 (string-append ls " order by cui desc")
606 (dolist (tuple (mutex-sql-query ls))
607 (push (find-ucon-cui (car tuple) :srl srl) ucons))
610 (defun find-ustr-word (word &key (srl *current-srl*))
611 "Return list of ustrs that match word"
613 (ls (format nil "select cui,sui from MRXW_ENG where wd='~a'" word)))
615 (string-append ls (format nil " and KLRL <= ~d" srl)))
616 (string-append ls " order by cui desc,sui desc")
617 (dolist (tuple (mutex-sql-query ls))
618 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
622 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
623 "Return list of ustrs that match word"
625 (ls (format nil "select cui,sui from MRXNW_ENG where nwd='~a'" word)))
627 (string-append ls (format nil " and KLRL <= ~d" srl)))
628 (string-append ls " order by cui desc,sui desc")
629 (dolist (tuple (mutex-sql-query ls))
630 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
635 ;;; Multiword lookup and score functions
637 (defun find-ucon-multiword (str &key (srl *current-srl*))
638 "Return sorted list of ucon's that match a multiword string"
639 (let* ((words (delimited-string-to-list str #\space))
642 (setq ucons (append ucons (find-ucon-word word :srl srl))))
643 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
645 (defun find-ustr-multiword (str &key (srl *current-srl*))
646 "Return sorted list of ustr's that match a multiword string"
647 (let* ((words (delimited-string-to-list str #\space))
650 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
651 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
653 (defun sort-score-ucon-str (str ucons)
654 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
655 (sort-score-umlsclass-str ucons str #'pfstr))
657 (defun sort-score-ustr-str (str ustrs)
658 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
659 (sort-score-umlsclass-str ustrs str #'str))
661 (defun sort-score-umlsclass-str (objs str lookup-func)
662 "Sort a list of objects based on scoring to a string"
667 (score-multiword-match str (funcall lookup-func obj)))
669 (mapcar #'car (sort scored #'> :key #'cadr))))
671 (defun score-multiword-match (s1 s2)
672 "Score a match between two strings with s1 being reference string"
673 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
674 (word-list-2 (delimited-string-to-list s2 #\space))
675 (n1 (length word-list-1))
676 (n2 (length word-list-2))
681 short-list long-list)
682 (declare (fixnum n1 n2 nshort nlong score unmatched))
687 (setq long-list word-list-1)
688 (setq short-list word-list-2))
692 (setq long-list word-list-2)
693 (setq short-list word-list-1)))
694 (decf score (- nlong nshort)) ;; reduce score for extra words
695 (dotimes (iword nshort)
696 (declare (fixnum iword))
697 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
699 (incf score (- 10 (abs (- kmrcl::it iword))))
701 (decf score (* 2 unmatched))
705 ;;; LEX SQL functions
707 (defun find-lexterm-eui (eui)
708 (kmrcl:awhen (car (mutex-sql-query
709 (format nil "select WRD from LRWD where EUI=~d" eui)))
710 (make-instance 'lexterm :eui eui :wrd (nth 0 kmrcl:it))))
712 (defun find-lexterm-word (wrd)
713 (kmrcl:awhen (mutex-sql-query
714 (format nil "select EUI from LRWD where WRD='~a'" wrd))
716 (dolist (tuple kmrcl:it)
717 (let ((eui (ensure-integer (nth 0 tuple))))
719 (make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
723 ;; LEXTERM accessors, read on demand
725 (def-lazy-reader lexterm s#abr find-labr-eui eui)
726 (def-lazy-reader lexterm s#agr find-lagr-eui eui)
727 (def-lazy-reader lexterm s#cmp find-lcmp-eui eui)
728 (def-lazy-reader lexterm s#mod find-lmod-eui eui)
729 (def-lazy-reader lexterm s#nom find-lnom-eui eui)
730 (def-lazy-reader lexterm s#prn find-lprn-eui eui)
731 (def-lazy-reader lexterm s#prp find-lprp-eui eui)
732 (def-lazy-reader lexterm s#spl find-lspl-eui eui)
733 (def-lazy-reader lexterm s#trm find-ltrm-eui eui)
734 (def-lazy-reader lexterm s#typ find-ltyp-eui eui)
736 ;; LEX SQL Read functions
738 (defun find-labr-eui (eui)
739 (kmrcl:awhen (mutex-sql-query
740 (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
742 (dolist (tuple kmrcl::it)
744 (make-instance 'labr :eui eui
747 :eui2 (ensure-integer (nth 2 tuple))
750 (nreverse results))))
752 (defun find-labr-bas (bas)
753 (kmrcl:awhen (mutex-sql-query
754 (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
756 (dolist (tuple kmrcl::it)
758 (make-instance 'labr :eui (ensure-integer (nth 0 tuple))
761 :eui2 (ensure-integer (nth 2 tuple))
764 (nreverse results))))
766 (defun find-lagr-eui (eui)
767 (kmrcl:awhen (mutex-sql-query
768 (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
770 (dolist (tuple kmrcl::it)
780 (nreverse results))))
782 (defun find-lcmp-eui (eui)
783 (kmrcl:awhen (mutex-sql-query
784 (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
786 (dolist (tuple kmrcl::it)
794 (nreverse results))))
796 (defun find-lmod-eui (eui)
797 (kmrcl:awhen (mutex-sql-query
798 (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
800 (dolist (tuple kmrcl::it)
806 :psnmod (nth 2 tuple)
809 (nreverse results))))
811 (defun find-lnom-eui (eui)
812 (kmrcl:awhen (mutex-sql-query
813 (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
815 (dolist (tuple kmrcl::it)
821 :eui2 (ensure-integer (nth 2 tuple))
825 (nreverse results))))
827 (defun find-lprn-eui (eui)
828 (kmrcl:awhen (mutex-sql-query
829 (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
831 (dolist (tuple kmrcl::it)
843 (nreverse results))))
845 (defun find-lprp-eui (eui)
846 (kmrcl:awhen (mutex-sql-query
847 (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
849 (dolist (tuple kmrcl::it)
858 (nreverse results))))
860 (defun find-lspl-eui (eui)
861 (kmrcl:awhen (mutex-sql-query
862 (format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
864 (dolist (tuple kmrcl::it)
871 (nreverse results))))
874 (defun find-ltrm-eui (eui)
875 (kmrcl:awhen (mutex-sql-query
876 (format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
878 (dolist (tuple kmrcl::it)
885 (nreverse results))))
887 (defun find-ltyp-eui (eui)
888 (kmrcl:awhen (mutex-sql-query
889 (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
891 (dolist (tuple kmrcl::it)
899 (nreverse results))))
901 (defun find-lwd-wrd (wrd)
902 (kmrcl:awhen (mutex-sql-query
903 (format nil "select EUI from LRWD where WRD='~a'" wrd))
905 (dolist (tuple kmrcl::it)
906 (push (ensure-integer (nth 0 tuple)) results))
907 (make-instance 'lwd :wrd wrd
908 :euilist (nreverse results)))))
910 ;;; Semantic Network SQL access functions
912 (defun find-sdef-ui (ui)
913 (kmrcl:awhen (car (mutex-sql-query
914 (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
915 (make-instance 'sdef :rt (nth 0 kmrcl::it)
917 :styrl (nth 1 kmrcl::it)
918 :stnrtn (nth 2 kmrcl::it)
919 :def (nth 3 kmrcl::it)
920 :ex (nth 4 kmrcl::it)
921 :un (nth 5 kmrcl::it)
922 :rh (nth 6 kmrcl::it)
923 :abr (nth 7 kmrcl::it)
924 :rin (nth 8 kmrcl::it))))
926 (defun find-sstre1-ui (ui)
927 (kmrcl:awhen (mutex-sql-query
928 (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
930 (dolist (tuple kmrcl::it)
932 (make-instance 'sstre1 :ui ui
933 :ui2 (ensure-integer (nth 0 tuple))
934 :ui3 (ensure-integer (nth 1 tuple)))
936 (nreverse results))))
938 (defun find-sstre1-ui2 (ui2)
939 (kmrcl:awhen (mutex-sql-query
940 (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
942 (dolist (tuple kmrcl::it)
944 (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
946 :ui3 (ensure-integer (nth 1 tuple)))
948 (nreverse results))))
950 (defun find-sstr-rl (rl)
951 (kmrcl:awhen (mutex-sql-query
952 (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
954 (dolist (tuple kmrcl::it)
959 :styrl2 (nth 1 tuple)
962 (nreverse results))))
965 (defun find-sstre2-sty (sty)
966 (kmrcl:awhen (mutex-sql-query
967 (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
969 (dolist (tuple kmrcl::it)
971 (make-instance 'sstre2
976 (nreverse results))))
978 (defun find-sstr-styrl (styrl)
979 (kmrcl:awhen (mutex-sql-query
980 (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
982 (dolist (tuple kmrcl::it)
984 (make-instance 'sstr :styrl styrl
986 :styrl2 (nth 1 tuple)
989 (nreverse results))))