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.5 2002/10/14 15:25:11 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 (defgeneric suistr (lo))
323 (defmethod suistr ((lo ulo))
324 "Return the string for a ulo object"
325 (find-string-sui (sui lo)))
327 (defun find-uatx-cui (cui &key (srl *current-srl*))
328 "Return a list of uatx for cui"
330 (ls (format nil "select SAB,REL,ATX from MRATX where CUI=~d" cui)))
332 (string-append ls (format nil " and KSRL <= ~d" srl)))
333 (dolist (tuple (mutex-sql-query ls))
334 (push (make-instance 'uatx :sab (nth 0 tuple)
341 (defun find-uterm-cui (cui &key (srl *current-srl*))
342 "Return a list of uterm for cui"
344 (ls (format nil "select distinct LUI,LAT,TS,KLUILRL from MRCON where CUI=~d" cui)))
346 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
347 (dolist (tuple (mutex-sql-query ls))
348 (push (make-instance 'uterm :lui (ensure-integer (nth 0 tuple))
352 :lrl (ensure-integer (nth 3 tuple)))
356 (defun find-uterm-lui (lui &key (srl *current-srl*))
357 "Return a list of uterm for lui"
359 (setq lui (parse-lui lui)))
361 (ls (format nil "select distinct CUI,LAT,TS,KLUILRL from MRCON where LUI=~d" lui)))
363 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
364 (dolist (tuple (mutex-sql-query ls))
365 (push (make-instance 'uterm :cui (ensure-integer (nth 0 tuple))
369 :lrl (ensure-integer (nth 3 tuple)))
373 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
374 "Return single uterm for cui/lui"
375 (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
377 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
378 (kmrcl:aif (car (mutex-sql-query ls))
379 (make-instance 'uterm :cui cui
381 :lat (nth 0 kmrcl::it)
382 :ts (nth 1 kmrcl::it)
383 :lrl (ensure-integer (nth 2 kmrcl::it)))
386 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
387 "Return a list of ustr for cui/lui"
388 (declare (fixnum cui lui))
390 (ls (format nil "select SUI,STT,STR,LRL from MRCON where KCUILUI=~d" (make-cuilui cui lui))))
392 (string-append ls (format nil " and LRL <= ~d" srl)))
393 (dolist (tuple (mutex-sql-query ls))
394 (let* ((sui (ensure-integer (car tuple)))
395 (ustr (make-instance 'ustr :sui sui
397 :cuisui (make-cuisui cui sui)
401 :lrl (ensure-integer (nth 3 tuple)))))
405 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
406 "Return the single ustr for cuisui"
407 (let ((ls (format nil "select LUI,STT,STR,LRL from MRCON where KCUISUI=~d"
408 (make-cuisui cui sui))))
410 (string-append ls (format nil " and LRL <= ~d" srl)))
411 (kmrcl:aif (car (mutex-sql-query ls))
412 (make-instance 'ustr :sui sui
414 :cuisui (make-cuisui cui sui)
415 :lui (ensure-integer (nth 0 kmrcl::it))
416 :stt (nth 1 kmrcl::it)
417 :str (nth 2 kmrcl::it)
418 :lrl (ensure-integer (nth 3 kmrcl::it)))
421 (defun find-ustr-sui (sui &key (srl *current-srl*))
422 "Return the list of ustr for sui"
424 (setq sui (parse-sui sui)))
426 (ls (format nil "select CUI,LUI,STT,STR,LRL from MRCON where SUI=~d" sui)))
428 (string-append ls (format nil " and LRL <= ~d" srl)))
429 (dolist (tuple (mutex-sql-query ls))
430 (let ((cui (ensure-integer (car tuple))))
431 (push (make-instance 'ustr :sui sui
433 :cuisui (make-cuisui cui sui)
434 :lui (ensure-integer (nth 1 tuple))
437 :lrl (ensure-integer (nth 4 tuple)))
441 (defun find-ustr-sab (sab &key (srl *current-srl*))
442 "Return the list of ustr for sab"
444 (ls (format nil "select KCUISUI from MRSO where SAB='~a'" sab)))
446 (string-append ls (format nil " and SRL <= ~d" srl)))
447 (dolist (tuple (mutex-sql-query ls))
448 (let ((cuisui (ensure-integer (car tuple))))
449 (push (apply #'find-ustr-cuisui
451 (multiple-value-list (decompose-cuisui cuisui))
456 (defun find-ustr-all (&key (srl *current-srl*))
457 "Return list of all ustr's"
458 (let ((ls "select distinct CUI,LUI,SUI,STT,LRL,KPFSTR from MRCON"))
460 (string-append ls (format nil " where LRL <= ~d" srl)))
461 (string-append ls " order by SUI asc")
462 (with-sql-connection (db)
465 #'(lambda (cui lui sui stt lrl pfstr)
466 (setq cui (ensure-integer cui))
467 (setq lui (ensure-integer lui))
468 (setq sui (ensure-integer sui))
469 (setq lrl (ensure-integer lrl))
470 (make-instance 'ustr :cui cui
473 :cuisui (make-cuisui cui sui)
480 (defun find-string-sui (sui &key (srl *current-srl*))
481 "Return the string associated with sui"
482 (let ((ls (format nil "select STR from MRCON where SUI=~d" sui)))
484 (string-append ls (format nil " and LRL <= ~d" srl)))
485 (string-append ls " limit 1")
486 (caar (mutex-sql-query ls))))
488 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
489 (declare (fixnum cui sui))
491 (ls (format nil "select SAB,CODE,SRL,TTY from MRSO where KCUISUI=~d"
492 (make-cuisui cui sui))))
494 (string-append ls (format nil " and SRL <= ~d" srl)))
495 (dolist (tuple (mutex-sql-query ls))
496 (push (make-instance 'uso :sab (nth 0 tuple) :code (nth 1 tuple)
497 :srl (nth 2 tuple) :tty (nth 3 tuple))
501 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
502 (declare (fixnum cui sui))
504 (ls (format nil "select SAB,CODE,CXN,CXL,RNK,CXS,CUI2,HCD,RELA,XC from MRCXT where KCUISUI=~d"
505 (make-cuisui cui sui))))
507 (string-append ls (format nil " and KSRL <= ~d" srl)))
508 (dolist (tuple (mutex-sql-query ls))
509 (push (make-instance 'ucxt :sab (nth 0 tuple)
511 :cxn (ensure-integer (nth 2 tuple))
513 :rnk (ensure-integer (nth 4 tuple))
515 :cui2 (ensure-integer (nth 6 tuple))
522 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
523 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
525 (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
526 (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
527 (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
529 (string-append ls (format nil " and KSRL <= ~d" srl)))
531 (dolist (tuple (mutex-sql-query ls))
532 (push (make-instance 'usat :code (nth 0 tuple)
540 (defun find-pfstr-cui (cui)
541 (caar (mutex-sql-query (format nil "select KPFSTR from MRCON where CUI=~d limit 1" cui))))
543 (defun find-usty-tui (tui)
545 (setq tui (parse-tui tui))
546 (kmrcl:aif (car (mutex-sql-query
547 (format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
548 (make-instance 'usty :tui tui :sty (nth 0 kmrcl::it))
551 (defun find-usty-sty (sty)
552 "Find usty for a sty"
553 (kmrcl:aif (car (mutex-sql-query
554 (format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
555 (make-instance 'usty :tui (ensure-integer (nth 0 kmrcl::it)) :sty sty)
558 (defun find-usty-all ()
559 "Return list of usty's for all semantic types"
561 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
562 (push (find-usty-tui (nth 0 tuple)) ustys))
566 (defun find-cui-max ()
567 (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
568 (ensure-integer cui)))
570 ;;;; Cross table find functions
572 (defun find-ucon-tui (tui &key (srl *current-srl*))
573 "Find list of ucon for tui"
575 (setq tui (parse-tui tui)))
577 (ls (format nil "select CUI from MRSTY where TUI=~d" tui)))
579 (string-append ls (format nil " and KLRL <= ~d" srl)))
580 (string-append ls " order by cui desc")
581 (dolist (tuple (mutex-sql-query ls))
582 (push (find-ucon-cui (ensure-integer (car tuple)) :srl srl) ucons))
585 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
586 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
588 (ls (format nil "select distinct cui from MRXW_ENG where wd~A'~A'"
589 (if like " LIKE " "=")
592 (string-append ls (format nil " and KLRL <= ~d" srl)))
593 (string-append ls " order by cui desc")
594 (dolist (tuple (mutex-sql-query ls))
595 (push (find-ucon-cui (car tuple) :srl srl) ucons))
598 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
599 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
601 (ls (format nil "select distinct cui from MRXNW_ENG where nwd~A'~A'"
602 (if like " LIKE " "=")
605 (string-append ls (format nil " and KLRL <= ~d" srl)))
606 (string-append ls " order by cui desc")
607 (dolist (tuple (mutex-sql-query ls))
608 (push (find-ucon-cui (car tuple) :srl srl) ucons))
611 (defun find-ustr-word (word &key (srl *current-srl*))
612 "Return list of ustrs that match word"
614 (ls (format nil "select cui,sui from MRXW_ENG where wd='~a'" word)))
616 (string-append ls (format nil " and KLRL <= ~d" srl)))
617 (string-append ls " order by cui desc,sui desc")
618 (dolist (tuple (mutex-sql-query ls))
619 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
623 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
624 "Return list of ustrs that match word"
626 (ls (format nil "select cui,sui from MRXNW_ENG where nwd='~a'" word)))
628 (string-append ls (format nil " and KLRL <= ~d" srl)))
629 (string-append ls " order by cui desc,sui desc")
630 (dolist (tuple (mutex-sql-query ls))
631 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
636 ;;; Multiword lookup and score functions
638 (defun find-ucon-multiword (str &key (srl *current-srl*))
639 "Return sorted list of ucon's that match a multiword string"
640 (let* ((words (delimited-string-to-list str #\space))
643 (setq ucons (append ucons (find-ucon-word word :srl srl))))
644 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
646 (defun find-ustr-multiword (str &key (srl *current-srl*))
647 "Return sorted list of ustr's that match a multiword string"
648 (let* ((words (delimited-string-to-list str #\space))
651 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
652 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
654 (defun sort-score-ucon-str (str ucons)
655 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
656 (sort-score-umlsclass-str ucons str #'pfstr))
658 (defun sort-score-ustr-str (str ustrs)
659 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
660 (sort-score-umlsclass-str ustrs str #'str))
662 (defun sort-score-umlsclass-str (objs str lookup-func)
663 "Sort a list of objects based on scoring to a string"
668 (score-multiword-match str (funcall lookup-func obj)))
670 (mapcar #'car (sort scored #'> :key #'cadr))))
672 (defun score-multiword-match (s1 s2)
673 "Score a match between two strings with s1 being reference string"
674 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
675 (word-list-2 (delimited-string-to-list s2 #\space))
676 (n1 (length word-list-1))
677 (n2 (length word-list-2))
682 short-list long-list)
683 (declare (fixnum n1 n2 nshort nlong score unmatched))
688 (setq long-list word-list-1)
689 (setq short-list word-list-2))
693 (setq long-list word-list-2)
694 (setq short-list word-list-1)))
695 (decf score (- nlong nshort)) ;; reduce score for extra words
696 (dotimes (iword nshort)
697 (declare (fixnum iword))
698 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
700 (incf score (- 10 (abs (- kmrcl::it iword))))
702 (decf score (* 2 unmatched))
706 ;;; LEX SQL functions
708 (defun find-lexterm-eui (eui)
709 (kmrcl:awhen (car (mutex-sql-query
710 (format nil "select WRD from LRWD where EUI=~d" eui)))
711 (make-instance 'lexterm :eui eui :wrd (nth 0 kmrcl:it))))
713 (defun find-lexterm-word (wrd)
714 (kmrcl:awhen (mutex-sql-query
715 (format nil "select EUI from LRWD where WRD='~a'" wrd))
717 (dolist (tuple kmrcl:it)
718 (let ((eui (ensure-integer (nth 0 tuple))))
720 (make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
724 ;; LEXTERM accessors, read on demand
726 (def-lazy-reader lexterm s#abr find-labr-eui eui)
727 (def-lazy-reader lexterm s#agr find-lagr-eui eui)
728 (def-lazy-reader lexterm s#cmp find-lcmp-eui eui)
729 (def-lazy-reader lexterm s#mod find-lmod-eui eui)
730 (def-lazy-reader lexterm s#nom find-lnom-eui eui)
731 (def-lazy-reader lexterm s#prn find-lprn-eui eui)
732 (def-lazy-reader lexterm s#prp find-lprp-eui eui)
733 (def-lazy-reader lexterm s#spl find-lspl-eui eui)
734 (def-lazy-reader lexterm s#trm find-ltrm-eui eui)
735 (def-lazy-reader lexterm s#typ find-ltyp-eui eui)
737 ;; LEX SQL Read functions
739 (defun find-labr-eui (eui)
740 (kmrcl:awhen (mutex-sql-query
741 (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
743 (dolist (tuple kmrcl::it)
745 (make-instance 'labr :eui eui
748 :eui2 (ensure-integer (nth 2 tuple))
751 (nreverse results))))
753 (defun find-labr-bas (bas)
754 (kmrcl:awhen (mutex-sql-query
755 (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
757 (dolist (tuple kmrcl::it)
759 (make-instance 'labr :eui (ensure-integer (nth 0 tuple))
762 :eui2 (ensure-integer (nth 2 tuple))
765 (nreverse results))))
767 (defun find-lagr-eui (eui)
768 (kmrcl:awhen (mutex-sql-query
769 (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
771 (dolist (tuple kmrcl::it)
781 (nreverse results))))
783 (defun find-lcmp-eui (eui)
784 (kmrcl:awhen (mutex-sql-query
785 (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
787 (dolist (tuple kmrcl::it)
795 (nreverse results))))
797 (defun find-lmod-eui (eui)
798 (kmrcl:awhen (mutex-sql-query
799 (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
801 (dolist (tuple kmrcl::it)
807 :psnmod (nth 2 tuple)
810 (nreverse results))))
812 (defun find-lnom-eui (eui)
813 (kmrcl:awhen (mutex-sql-query
814 (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
816 (dolist (tuple kmrcl::it)
822 :eui2 (ensure-integer (nth 2 tuple))
826 (nreverse results))))
828 (defun find-lprn-eui (eui)
829 (kmrcl:awhen (mutex-sql-query
830 (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
832 (dolist (tuple kmrcl::it)
844 (nreverse results))))
846 (defun find-lprp-eui (eui)
847 (kmrcl:awhen (mutex-sql-query
848 (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
850 (dolist (tuple kmrcl::it)
859 (nreverse results))))
861 (defun find-lspl-eui (eui)
862 (kmrcl:awhen (mutex-sql-query
863 (format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
865 (dolist (tuple kmrcl::it)
872 (nreverse results))))
875 (defun find-ltrm-eui (eui)
876 (kmrcl:awhen (mutex-sql-query
877 (format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
879 (dolist (tuple kmrcl::it)
886 (nreverse results))))
888 (defun find-ltyp-eui (eui)
889 (kmrcl:awhen (mutex-sql-query
890 (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
892 (dolist (tuple kmrcl::it)
900 (nreverse results))))
902 (defun find-lwd-wrd (wrd)
903 (kmrcl:awhen (mutex-sql-query
904 (format nil "select EUI from LRWD where WRD='~a'" wrd))
906 (dolist (tuple kmrcl::it)
907 (push (ensure-integer (nth 0 tuple)) results))
908 (make-instance 'lwd :wrd wrd
909 :euilist (nreverse results)))))
911 ;;; Semantic Network SQL access functions
913 (defun find-sdef-ui (ui)
914 (kmrcl:awhen (car (mutex-sql-query
915 (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
916 (make-instance 'sdef :rt (nth 0 kmrcl::it)
918 :styrl (nth 1 kmrcl::it)
919 :stnrtn (nth 2 kmrcl::it)
920 :def (nth 3 kmrcl::it)
921 :ex (nth 4 kmrcl::it)
922 :un (nth 5 kmrcl::it)
923 :rh (nth 6 kmrcl::it)
924 :abr (nth 7 kmrcl::it)
925 :rin (nth 8 kmrcl::it))))
927 (defun find-sstre1-ui (ui)
928 (kmrcl:awhen (mutex-sql-query
929 (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
931 (dolist (tuple kmrcl::it)
933 (make-instance 'sstre1 :ui ui
934 :ui2 (ensure-integer (nth 0 tuple))
935 :ui3 (ensure-integer (nth 1 tuple)))
937 (nreverse results))))
939 (defun find-sstre1-ui2 (ui2)
940 (kmrcl:awhen (mutex-sql-query
941 (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
943 (dolist (tuple kmrcl::it)
945 (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
947 :ui3 (ensure-integer (nth 1 tuple)))
949 (nreverse results))))
951 (defun find-sstr-rl (rl)
952 (kmrcl:awhen (mutex-sql-query
953 (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
955 (dolist (tuple kmrcl::it)
960 :styrl2 (nth 1 tuple)
963 (nreverse results))))
966 (defun find-sstre2-sty (sty)
967 (kmrcl:awhen (mutex-sql-query
968 (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
970 (dolist (tuple kmrcl::it)
972 (make-instance 'sstre2
977 (nreverse results))))
979 (defun find-sstr-styrl (styrl)
980 (kmrcl:awhen (mutex-sql-query
981 (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
983 (dolist (tuple kmrcl::it)
985 (make-instance 'sstr :styrl styrl
987 :styrl2 (nth 1 tuple)
990 (nreverse results))))