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.6 2002/10/18 03:57:39 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)
637 (defun find-usrl-all ()
639 (tuples (mutex-sql-query "select SAB,SRL from USRL order by SAB desc")))
640 (dolist (tuple tuples)
641 (push (make-instance 'usrl :sab (nth 0 tuple)
642 :srl (ensure-integer (nth 1 tuple))) usrls))
645 ;;; Multiword lookup and score functions
647 (defun find-ucon-multiword (str &key (srl *current-srl*))
648 "Return sorted list of ucon's that match a multiword string"
649 (let* ((words (delimited-string-to-list str #\space))
652 (setq ucons (append ucons (find-ucon-word word :srl srl))))
653 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
655 (defun find-ustr-multiword (str &key (srl *current-srl*))
656 "Return sorted list of ustr's that match a multiword string"
657 (let* ((words (delimited-string-to-list str #\space))
660 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
661 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
663 (defun sort-score-ucon-str (str ucons)
664 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
665 (sort-score-umlsclass-str ucons str #'pfstr))
667 (defun sort-score-ustr-str (str ustrs)
668 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
669 (sort-score-umlsclass-str ustrs str #'str))
671 (defun sort-score-umlsclass-str (objs str lookup-func)
672 "Sort a list of objects based on scoring to a string"
677 (score-multiword-match str (funcall lookup-func obj)))
679 (mapcar #'car (sort scored #'> :key #'cadr))))
681 (defun score-multiword-match (s1 s2)
682 "Score a match between two strings with s1 being reference string"
683 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
684 (word-list-2 (delimited-string-to-list s2 #\space))
685 (n1 (length word-list-1))
686 (n2 (length word-list-2))
691 short-list long-list)
692 (declare (fixnum n1 n2 nshort nlong score unmatched))
697 (setq long-list word-list-1)
698 (setq short-list word-list-2))
702 (setq long-list word-list-2)
703 (setq short-list word-list-1)))
704 (decf score (- nlong nshort)) ;; reduce score for extra words
705 (dotimes (iword nshort)
706 (declare (fixnum iword))
707 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
709 (incf score (- 10 (abs (- kmrcl::it iword))))
711 (decf score (* 2 unmatched))
715 ;;; LEX SQL functions
717 (defun find-lexterm-eui (eui)
718 (kmrcl:awhen (car (mutex-sql-query
719 (format nil "select WRD from LRWD where EUI=~d" eui)))
720 (make-instance 'lexterm :eui eui :wrd (nth 0 kmrcl:it))))
722 (defun find-lexterm-word (wrd)
723 (kmrcl:awhen (mutex-sql-query
724 (format nil "select EUI from LRWD where WRD='~a'" wrd))
726 (dolist (tuple kmrcl:it)
727 (let ((eui (ensure-integer (nth 0 tuple))))
729 (make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
733 ;; LEXTERM accessors, read on demand
735 (def-lazy-reader lexterm s#abr find-labr-eui eui)
736 (def-lazy-reader lexterm s#agr find-lagr-eui eui)
737 (def-lazy-reader lexterm s#cmp find-lcmp-eui eui)
738 (def-lazy-reader lexterm s#mod find-lmod-eui eui)
739 (def-lazy-reader lexterm s#nom find-lnom-eui eui)
740 (def-lazy-reader lexterm s#prn find-lprn-eui eui)
741 (def-lazy-reader lexterm s#prp find-lprp-eui eui)
742 (def-lazy-reader lexterm s#spl find-lspl-eui eui)
743 (def-lazy-reader lexterm s#trm find-ltrm-eui eui)
744 (def-lazy-reader lexterm s#typ find-ltyp-eui eui)
746 ;; LEX SQL Read functions
748 (defun find-labr-eui (eui)
749 (kmrcl:awhen (mutex-sql-query
750 (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
752 (dolist (tuple kmrcl::it)
754 (make-instance 'labr :eui eui
757 :eui2 (ensure-integer (nth 2 tuple))
760 (nreverse results))))
762 (defun find-labr-bas (bas)
763 (kmrcl:awhen (mutex-sql-query
764 (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
766 (dolist (tuple kmrcl::it)
768 (make-instance 'labr :eui (ensure-integer (nth 0 tuple))
771 :eui2 (ensure-integer (nth 2 tuple))
774 (nreverse results))))
776 (defun find-lagr-eui (eui)
777 (kmrcl:awhen (mutex-sql-query
778 (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
780 (dolist (tuple kmrcl::it)
790 (nreverse results))))
792 (defun find-lcmp-eui (eui)
793 (kmrcl:awhen (mutex-sql-query
794 (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
796 (dolist (tuple kmrcl::it)
804 (nreverse results))))
806 (defun find-lmod-eui (eui)
807 (kmrcl:awhen (mutex-sql-query
808 (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
810 (dolist (tuple kmrcl::it)
816 :psnmod (nth 2 tuple)
819 (nreverse results))))
821 (defun find-lnom-eui (eui)
822 (kmrcl:awhen (mutex-sql-query
823 (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
825 (dolist (tuple kmrcl::it)
831 :eui2 (ensure-integer (nth 2 tuple))
835 (nreverse results))))
837 (defun find-lprn-eui (eui)
838 (kmrcl:awhen (mutex-sql-query
839 (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
841 (dolist (tuple kmrcl::it)
853 (nreverse results))))
855 (defun find-lprp-eui (eui)
856 (kmrcl:awhen (mutex-sql-query
857 (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
859 (dolist (tuple kmrcl::it)
868 (nreverse results))))
870 (defun find-lspl-eui (eui)
871 (kmrcl:awhen (mutex-sql-query
872 (format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
874 (dolist (tuple kmrcl::it)
881 (nreverse results))))
884 (defun find-ltrm-eui (eui)
885 (kmrcl:awhen (mutex-sql-query
886 (format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
888 (dolist (tuple kmrcl::it)
895 (nreverse results))))
897 (defun find-ltyp-eui (eui)
898 (kmrcl:awhen (mutex-sql-query
899 (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
901 (dolist (tuple kmrcl::it)
909 (nreverse results))))
911 (defun find-lwd-wrd (wrd)
912 (kmrcl:awhen (mutex-sql-query
913 (format nil "select EUI from LRWD where WRD='~a'" wrd))
915 (dolist (tuple kmrcl::it)
916 (push (ensure-integer (nth 0 tuple)) results))
917 (make-instance 'lwd :wrd wrd
918 :euilist (nreverse results)))))
920 ;;; Semantic Network SQL access functions
922 (defun find-sdef-ui (ui)
923 (kmrcl:awhen (car (mutex-sql-query
924 (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
925 (make-instance 'sdef :rt (nth 0 kmrcl::it)
927 :styrl (nth 1 kmrcl::it)
928 :stnrtn (nth 2 kmrcl::it)
929 :def (nth 3 kmrcl::it)
930 :ex (nth 4 kmrcl::it)
931 :un (nth 5 kmrcl::it)
932 :rh (nth 6 kmrcl::it)
933 :abr (nth 7 kmrcl::it)
934 :rin (nth 8 kmrcl::it))))
936 (defun find-sstre1-ui (ui)
937 (kmrcl:awhen (mutex-sql-query
938 (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
940 (dolist (tuple kmrcl::it)
942 (make-instance 'sstre1 :ui ui
943 :ui2 (ensure-integer (nth 0 tuple))
944 :ui3 (ensure-integer (nth 1 tuple)))
946 (nreverse results))))
948 (defun find-sstre1-ui2 (ui2)
949 (kmrcl:awhen (mutex-sql-query
950 (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
952 (dolist (tuple kmrcl::it)
954 (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
956 :ui3 (ensure-integer (nth 1 tuple)))
958 (nreverse results))))
960 (defun find-sstr-rl (rl)
961 (kmrcl:awhen (mutex-sql-query
962 (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
964 (dolist (tuple kmrcl::it)
969 :styrl2 (nth 1 tuple)
972 (nreverse results))))
975 (defun find-sstre2-sty (sty)
976 (kmrcl:awhen (mutex-sql-query
977 (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
979 (dolist (tuple kmrcl::it)
981 (make-instance 'sstre2
986 (nreverse results))))
988 (defun find-sstr-styrl (styrl)
989 (kmrcl:awhen (mutex-sql-query
990 (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
992 (dolist (tuple kmrcl::it)
994 (make-instance 'sstr :styrl styrl
996 :styrl2 (nth 1 tuple)
999 (nreverse results))))