1 ;;; $Id: sql-classes.lisp,v 1.1 2002/10/08 22:08:56 kevin Exp $
5 (declaim (optimize (speed 3) (safety 1)))
7 (defvar *current-srl* nil)
10 (defun current-srl! (srl)
11 (setq *current-srl* srl))
14 ;;; Accessors (read on demand)
16 ;; defines a slot-unbound method for class and slot-name, fills
17 ;; the slot by calling reader function with the slot values of
18 ;; the instance's reader-keys
19 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
20 (let* ((the-slot-name (gensym))
22 (the-instance (gensym))
24 (dolist (key reader-keys)
25 (push (list 'slot-value the-instance (list 'quote key)) keys))
26 (setq keys (nreverse keys))
27 `(defmethod slot-unbound (,the-class (,the-instance ,class)
28 (,the-slot-name (eql ',slot-name)))
29 (declare (ignore ,the-class))
30 (setf (slot-value ,the-instance ,the-slot-name)
33 (def-lazy-reader ucon s#term find-uterm-cui cui)
34 (def-lazy-reader ucon s#def find-udef-cui cui)
35 (def-lazy-reader ucon s#sty find-usty-cui cui)
36 (def-lazy-reader ucon s#rel find-urel-cui cui)
37 (def-lazy-reader ucon s#coc find-ucoc-cui cui)
38 (def-lazy-reader ucon s#lo find-ulo-cui cui)
39 (def-lazy-reader ucon s#atx find-uatx-cui cui)
40 (def-lazy-reader ucon s#sat find-usat-ui cui)
43 (def-lazy-reader uterm s#str find-ustr-cuilui cui lui)
44 (def-lazy-reader uterm s#sat find-usat-ui cui lui)
47 (def-lazy-reader ustr s#sat find-usat-ui cui lui sui)
48 (def-lazy-reader ustr s#cxt find-ucxt-cuisui cui sui)
49 (def-lazy-reader ustr s#so find-uso-cuisui cui sui)
53 ;;; Lookup functions for uterms,ustr in ucons
55 (defun find-uterm-in-ucon (ucon lui)
56 (find lui (s#term ucon) :key #'uterm-lui :test 'equal))
58 (defun find-ustr-in-uterm (uterm sui)
59 (find sui (s#str uterm) :key #'ustr-sui :test 'equal))
61 (defun find-ustr-in-ucon (ucon sui)
62 (let ((found-ustr nil))
63 (dolist (uterm (s#term ucon))
65 (dolist (ustr (s#str uterm))
67 (when (string-equal sui (sui ustr))
68 (setq found-ustr ustr))))))
72 (defun find-ucon-cui (cui &key (srl *current-srl*))
75 (setq cui (parse-cui cui)))
77 (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d"
80 (string-append ls (format nil " and KCUILRL <= ~d limit 1" srl))
81 (string-append ls " limit 1"))
82 (gu:awhen (car (mutex-sql-query ls))
83 (make-instance 'ucon :cui cui :pfstr (car gu::it)
84 :lrl (ensure-integer (cadr gu::it)))))
87 (defun find-ucon-lui (lui &key (srl *current-srl*))
88 "Find list of ucon for lui"
90 (setq lui (parse-lui lui)))
93 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where LUI=~d" lui)))
95 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
96 (dolist (tuple (mutex-sql-query ls))
97 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
99 :lrl (ensure-integer (nth 2 tuple)))
104 (defun find-ucon-sui (sui &key (srl *current-srl*))
105 "Find list of ucon for sui"
107 (setq sui (parse-sui sui)))
110 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where SUI=~d" sui)))
112 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
113 (let ((tuples (mutex-sql-query ls)))
114 (dolist (tuple tuples)
115 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
117 :lrl (ensure-integer (nth 2 tuple)))
122 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
123 "Find ucon for cui/sui"
125 (setq cui (parse-cui cui)))
127 (setq sui (parse-sui sui)))
129 (let ((ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where KCUISUI=~d"
130 (make-cuisui cui sui))))
132 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
133 (gu:aif (car (mutex-sql-query ls))
134 (make-instance 'ucon :cui (ensure-integer (nth 0 gu::it))
135 :pfstr (nth 1 gu::it)
136 :lrl (ensure-integer (nth 2 gu::it)))
140 (defun find-ucon-str (str &key (srl *current-srl*))
141 "Find ucon that are exact match for str"
144 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where STR='~a'" str)))
146 (string-append ls " and KCUILRL <= ~d" srl))
147 (dolist (tuple (mutex-sql-query ls))
148 (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple))
150 :lrl (ensure-integer (nth 2 tuple))) ucons))
154 (defun find-ucon-all (&key (srl *current-srl*))
155 "Return list of all ucon's"
156 (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON"))
158 (string-append ls (format nil " where KCUILRL <= ~d" srl)))
159 (string-append ls " order by CUI asc")
160 (with-sql-connection (db)
163 #'(lambda (cui pfstr cuilrl)
164 (make-instance 'ucon :cui (ensure-integer cui)
166 :lrl (ensure-integer cuilrl)))
172 (defun find-udef-cui (cui &key (srl *current-srl*))
173 "Return a list of udefs for cui"
175 (ls (format nil "select SAB,DEF from MRDEF where CUI=~d" cui)))
177 (string-append ls (format nil " and KSRL <= ~d" srl)))
178 (dolist (tuple (mutex-sql-query ls))
179 (push (make-instance 'udef :sab (car tuple) :def (cadr tuple)) udefs))
182 (defun find-usty-cui (cui &key (srl *current-srl*))
183 "Return a list of usty for cui"
185 (ls (format nil "select TUI,STY from MRSTY where CUI=~d" cui)))
187 (string-append ls (format nil " and KLRL <= ~d" srl)))
188 (dolist (tuple (mutex-sql-query ls))
189 (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys))
192 (defun find-usty-word (word &key (srl *current-srl*))
193 "Return a list of usty that match word"
195 (ls (format nil "select distinct TUI,STY from MRSTY where STY like '%~a%'" word)))
197 (string-append ls (format nil " and KLRL <= ~d" srl)))
198 (dolist (tuple (mutex-sql-query ls))
199 (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys))
202 (defun find-urel-cui (cui &key (srl *current-srl*))
203 "Return a list of urel for cui"
205 (ls (format nil "select REL,CUI2,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI1=~d" cui)))
207 (string-append ls (format nil " and KSRL <= ~d" srl)))
208 (dolist (tuple (mutex-sql-query ls))
209 (push (make-instance 'urel
212 :cui2 (ensure-integer (nth 1 tuple))
217 :pfstr2 (nth 6 tuple))
221 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
222 "Return a list of urel for cui2"
224 (ls (format nil "select REL,CUI1,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI2=~d" cui2)))
226 (string-append ls (format nil " and SRL <= ~d" srl)))
227 (dolist (tuple (mutex-sql-query ls))
228 (push (make-instance 'urel
231 :cui1 (ensure-integer (nth 1 tuple))
236 :pfstr2 (nth 6 tuple))
240 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
242 #'(lambda (cui) (find-ucon-cui cui :key srl))
243 (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
245 (defun find-ucoc-cui (cui &key (srl *current-srl*))
246 "Return a list of ucoc for cui"
248 (ls (format nil "select CUI2,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI1=~d" cui)))
250 (string-append ls (format nil " and KLRL <= ~d" srl)))
251 (string-append ls " order by COF asc")
252 (dolist (tuple (mutex-sql-query ls))
253 (let ((cui2 (ensure-integer (nth 0 tuple))))
256 (push (make-instance 'ucoc :cui1 cui
260 :cof (ensure-integer (nth 3 tuple))
262 :pfstr2 (nth 5 tuple))
264 ucocs)) ;; akready ordered by SQL select
266 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
267 "Return a list of ucoc for cui2"
269 (ls (format nil "select CUI1,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI2=~d" cui2)))
271 (string-append ls (format nil " and KSRL <= ~d" srl)))
272 (string-append ls " order by COF asc")
273 (dolist (tuple (mutex-sql-query ls))
274 (push (make-instance 'ucoc :cui1 (ensure-integer (nth 0 tuple))
278 :cof (ensure-integer (nth 3 tuple))
280 :pfstr2 (nth 5 tuple))
282 ucocs)) ;; already ordered by SQL select
284 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
285 "List of ucon with co-occurance cui2"
287 #'(lambda (cui) (find-ucon-cui cui :key srl))
288 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
290 (defun find-ulo-cui (cui &key (srl *current-srl*))
291 "Return a list of ulo for cui"
293 (ls (format nil "select ISN,FR,UN,SUI,SNA,SOUI from MRLO where CUI=~d" cui)))
295 (string-append ls (format nil " and KLRL <= ~d" srl)))
296 (dolist (tuple (mutex-sql-query ls))
297 (push (make-instance 'ulo :isn (nth 0 tuple)
298 :fr (ensure-integer (nth 1 tuple))
300 :sui (ensure-integer (nth 3 tuple))
306 (defmethod suistr ((lo ulo))
307 "Return the string for a ulo object"
308 (find-string-sui (sui lo)))
310 (defun find-uatx-cui (cui &key (srl *current-srl*))
311 "Return a list of uatx for cui"
313 (ls (format nil "select SAB,REL,ATX from MRATX where CUI=~d" cui)))
315 (string-append ls (format nil " and KSRL <= ~d" srl)))
316 (dolist (tuple (mutex-sql-query ls))
317 (push (make-instance 'uatx :sab (nth 0 tuple)
324 (defun find-uterm-cui (cui &key (srl *current-srl*))
325 "Return a list of uterm for cui"
327 (ls (format nil "select distinct LUI,LAT,TS,KLUILRL from MRCON where CUI=~d" cui)))
329 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
330 (dolist (tuple (mutex-sql-query ls))
331 (push (make-instance 'uterm :lui (ensure-integer (nth 0 tuple))
335 :lrl (ensure-integer (nth 3 tuple)))
339 (defun find-uterm-lui (lui &key (srl *current-srl*))
340 "Return a list of uterm for lui"
342 (setq lui (parse-lui lui)))
344 (ls (format nil "select distinct CUI,LAT,TS,KLUILRL from MRCON where LUI=~d" lui)))
346 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
347 (dolist (tuple (mutex-sql-query ls))
348 (push (make-instance 'uterm :cui (ensure-integer (nth 0 tuple))
352 :lrl (ensure-integer (nth 3 tuple)))
356 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
357 "Return single uterm for cui/lui"
358 (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
360 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
361 (gu:aif (car (mutex-sql-query ls))
362 (make-instance 'uterm :cui cui
366 :lrl (ensure-integer (nth 2 gu::it)))
369 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
370 "Return a list of ustr for cui/lui"
371 (declare (fixnum cui lui))
373 (ls (format nil "select SUI,STT,STR,LRL from MRCON where KCUILUI=~d" (make-cuilui cui lui))))
375 (string-append ls (format nil " and LRL <= ~d" srl)))
376 (dolist (tuple (mutex-sql-query ls))
377 (let* ((sui (ensure-integer (car tuple)))
378 (ustr (make-instance 'ustr :sui sui
380 :cuisui (make-cuisui cui sui)
384 :lrl (ensure-integer (nth 3 tuple)))))
388 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
389 "Return the single ustr for cuisui"
390 (let ((ls (format nil "select LUI,STT,STR,LRL from MRCON where KCUISUI=~d"
391 (make-cuisui cui sui))))
393 (string-append ls (format nil " and LRL <= ~d" srl)))
394 (gu:aif (car (mutex-sql-query ls))
395 (make-instance 'ustr :sui sui
397 :cuisui (make-cuisui cui sui)
398 :lui (ensure-integer (nth 0 gu::it))
401 :lrl (ensure-integer (nth 3 gu::it)))
404 (defun find-ustr-sui (sui &key (srl *current-srl*))
405 "Return the list of ustr for sui"
407 (setq sui (parse-sui sui)))
409 (ls (format nil "select CUI,LUI,STT,STR,LRL from MRCON where SUI=~d" sui)))
411 (string-append ls (format nil " and LRL <= ~d" srl)))
412 (dolist (tuple (mutex-sql-query ls))
413 (let ((cui (ensure-integer (car tuple))))
414 (push (make-instance 'ustr :sui sui
416 :cuisui (make-cuisui cui sui)
417 :lui (ensure-integer (nth 1 tuple))
420 :lrl (ensure-integer (nth 4 tuple)))
424 (defun find-ustr-sab (sab &key (srl *current-srl*))
425 "Return the list of ustr for sab"
427 (ls (format nil "select KCUISUI from MRSO where SAB='~a'" sab)))
429 (string-append ls (format nil " and SRL <= ~d" srl)))
430 (dolist (tuple (mutex-sql-query ls))
431 (let ((cuisui (ensure-integer (car tuple))))
432 (push (apply #'find-ustr-cuisui
434 (multiple-value-list (decompose-cuisui cuisui))
439 (defun find-ustr-all (&key (srl *current-srl*))
440 "Return list of all ustr's"
441 (let ((ls "select distinct CUI,LUI,SUI,STT,LRL,KPFSTR from MRCON"))
443 (string-append ls (format nil " where LRL <= ~d" srl)))
444 (string-append ls " order by SUI asc")
445 (with-sql-connection (db)
448 #'(lambda (cui lui sui stt lrl pfstr)
449 (setq cui (ensure-integer cui))
450 (setq lui (ensure-integer lui))
451 (setq sui (ensure-integer sui))
452 (setq lrl (ensure-integer lrl))
453 (make-instance 'ustr :cui cui
456 :cuisui (make-cuisui cui sui)
463 (defun find-string-sui (sui &key (srl *current-srl*))
464 "Return the string associated with sui"
465 (let ((ls (format nil "select STR from MRCON where SUI=~d" sui)))
467 (string-append ls (format nil " and LRL <= ~d" srl)))
468 (string-append ls " limit 1")
469 (caar (mutex-sql-query ls))))
471 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
472 (declare (fixnum cui sui))
474 (ls (format nil "select SAB,CODE,SRL,TTY from MRSO where KCUISUI=~d"
475 (make-cuisui cui sui))))
477 (string-append ls (format nil " and SRL <= ~d" srl)))
478 (dolist (tuple (mutex-sql-query ls))
479 (push (make-instance 'uso :sab (nth 0 tuple) :code (nth 1 tuple)
480 :srl (nth 2 tuple) :tty (nth 3 tuple))
484 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
485 (declare (fixnum cui sui))
487 (ls (format nil "select SAB,CODE,CXN,CXL,RNK,CXS,CUI2,HCD,RELA,XC from MRCXT where KCUISUI=~d"
488 (make-cuisui cui sui))))
490 (string-append ls (format nil " and KSRL <= ~d" srl)))
491 (dolist (tuple (mutex-sql-query ls))
492 (push (make-instance 'ucxt :sab (nth 0 tuple)
494 :cxn (ensure-integer (nth 2 tuple))
496 :rnk (ensure-integer (nth 4 tuple))
498 :cui2 (ensure-integer (nth 6 tuple))
505 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
506 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
508 (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
509 (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
510 (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
512 (string-append ls (format nil " and KSRL <= ~d" srl)))
514 (dolist (tuple (mutex-sql-query ls))
515 (push (make-instance 'usat :code (nth 0 tuple)
523 (defun find-pfstr-cui (cui)
524 (caar (mutex-sql-query (format nil "select KPFSTR from MRCON where CUI=~d limit 1" cui))))
526 (defun find-usty-tui (tui)
528 (setq tui (parse-tui tui))
529 (gu:aif (car (mutex-sql-query
530 (format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
531 (make-instance 'usty :tui tui :sty (nth 0 gu::it))
534 (defun find-usty-sty (sty)
535 "Find usty for a sty"
536 (gu:aif (car (mutex-sql-query
537 (format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
538 (make-instance 'usty :tui (ensure-integer (nth 0 gu::it)) :sty sty)
541 (defun find-usty-all ()
542 "Return list of usty's for all semantic types"
544 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
545 (push (find-usty-tui (nth 0 tuple)) ustys))
548 (defun find-usty_freq-all ()
549 (let ((usty_freqs '()))
550 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
551 (let* ((tui (car tuple))
552 (freq (ensure-integer
553 (caar (mutex-sql-query
554 (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
555 (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
556 (sort usty_freqs #'> :key #'usty_freq-freq)))
561 (defun find-cui-max ()
562 (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
563 (ensure-integer cui)))
565 ;;;; Cross table find functions
567 (defun find-ucon-tui (tui &key (srl *current-srl*))
568 "Find list of ucon for tui"
570 (setq tui (parse-tui tui)))
572 (ls (format nil "select CUI from MRSTY where TUI=~d" tui)))
574 (string-append ls (format nil " and KLRL <= ~d" srl)))
575 (string-append ls " order by cui desc")
576 (dolist (tuple (mutex-sql-query ls))
577 (push (find-ucon-cui (ensure-integer (car tuple)) :srl srl) ucons))
580 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
581 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
583 (ls (format nil "select distinct cui from MRXW_ENG where wd~A'~A'"
584 (if like " LIKE " "=")
587 (string-append ls (format nil " and KLRL <= ~d" srl)))
588 (string-append ls " order by cui desc")
589 (dolist (tuple (mutex-sql-query ls))
590 (push (find-ucon-cui (car tuple) :srl srl) ucons))
593 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
594 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
596 (ls (format nil "select distinct cui from MRXNW_ENG where nwd~A'~A'"
597 (if like " LIKE " "=")
600 (string-append ls (format nil " and KLRL <= ~d" srl)))
601 (string-append ls " order by cui desc")
602 (dolist (tuple (mutex-sql-query ls))
603 (push (find-ucon-cui (car tuple) :srl srl) ucons))
606 (defun find-ustr-word (word &key (srl *current-srl*))
607 "Return list of ustrs that match word"
609 (ls (format nil "select cui,sui from MRXW_ENG where wd='~a'" word)))
611 (string-append ls (format nil " and KLRL <= ~d" srl)))
612 (string-append ls " order by cui desc,sui desc")
613 (dolist (tuple (mutex-sql-query ls))
614 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
618 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
619 "Return list of ustrs that match word"
621 (ls (format nil "select cui,sui from MRXNW_ENG where nwd='~a'" word)))
623 (string-append ls (format nil " and KLRL <= ~d" srl)))
624 (string-append ls " order by cui desc,sui desc")
625 (dolist (tuple (mutex-sql-query ls))
626 (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl)
631 ;;; Multiword lookup and score functions
633 (defun find-ucon-multiword (str &key (srl *current-srl*))
634 "Return sorted list of ucon's that match a multiword string"
635 (let* ((words (delimited-string-to-list str #\space))
638 (setq ucons (append ucons (find-ucon-word word :srl srl))))
639 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
641 (defun find-ucon-normalized-multiword (str &key (srl *current-srl*))
642 "Return sorted list of ucon's that match a multiword string"
643 (let* ((words (delimited-string-to-list str #\space))
647 (let ((nws (lvg:process-terms word)))
649 (push nword nwords))))
650 (dolist (word nwords)
651 (setq ucons (append ucons (find-ucon-word word :srl srl))))
652 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
654 (defun find-ustr-multiword (str &key (srl *current-srl*))
655 "Return sorted list of ustr's that match a multiword string"
656 (let* ((words (delimited-string-to-list str #\space))
659 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
660 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
662 (defun find-ustr-normalized-multiword (str &key (srl *current-srl*))
663 "Return sorted list of ustr's that match a multiword string"
664 (let* ((words (delimited-string-to-list str #\space))
668 (let ((nws (lvg:process-terms word)))
670 (push nword nwords))))
671 (dolist (word nwords)
672 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
673 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'ustr-cui))))
676 (find-normalized-matches-for-str str #'find-ustr-normalized-word #'ustr-sui))
678 (defun find-normalized-matches-for-str (str lookup-func key-func)
679 "Return list of objects that normalize match for words in string,
680 eliminate duplicates."
683 (dolist (word (delimited-string-to-list str #\space))
684 (dolist (nword (lvg:process-terms word))
685 (unless (member nword nwords :test #'string-equal)
686 (push nword nwords))))
688 (setq objs (append objs (funcall lookup-func nw))))
689 (delete-duplicates objs :key key-func :test #'eql)))
691 (defun sort-score-ucon-str (str ucons)
692 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
693 (sort-score-umlsclass-str ucons str #'pfstr))
695 (defun sort-score-ustr-str (str ustrs)
696 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
697 (sort-score-umlsclass-str ustrs str #'str))
699 (defun sort-score-umlsclass-str (objs str lookup-func)
700 "Sort a list of objects based on scoring to a string"
705 (score-multiword-match str (funcall lookup-func obj)))
707 (mapcar #'car (sort scored #'> :key #'cadr))))
709 (defun score-multiword-match (s1 s2)
710 "Score a match between two strings with s1 being reference string"
711 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
712 (word-list-2 (delimited-string-to-list s2 #\space))
713 (n1 (length word-list-1))
714 (n2 (length word-list-2))
719 short-list long-list)
720 (declare (fixnum n1 n2 nshort nlong score unmatched))
725 (setq long-list word-list-1)
726 (setq short-list word-list-2))
730 (setq long-list word-list-2)
731 (setq short-list word-list-1)))
732 (decf score (- nlong nshort)) ;; reduce score for extra words
733 (dotimes (iword nshort)
734 (declare (fixnum iword))
735 (gu:aif (position (nth iword short-list) long-list :test #'string-equal)
737 (incf score (- 10 (abs (- gu::it iword))))
739 (decf score (* 2 unmatched))
743 ;;; LEX SQL functions
745 (defun find-lexterm-eui (eui)
746 (gu:awhen (car (mutex-sql-query
747 (format nil "select WRD from LRWD where EUI=~d" eui)))
748 (make-instance 'lexterm :eui eui :wrd (nth 0 gu:it))))
750 (defun find-lexterm-word (wrd)
751 (gu:awhen (mutex-sql-query
752 (format nil "select EUI from LRWD where WRD='~a'" wrd))
754 (dolist (tuple gu:it)
755 (let ((eui (ensure-integer (nth 0 tuple))))
757 (make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
761 ;; LEXTERM accessors, read on demand
763 (def-lazy-reader lexterm s#abr find-labr-eui eui)
764 (def-lazy-reader lexterm s#agr find-lagr-eui eui)
765 (def-lazy-reader lexterm s#cmp find-lcmp-eui eui)
766 (def-lazy-reader lexterm s#mod find-lmod-eui eui)
767 (def-lazy-reader lexterm s#nom find-lnom-eui eui)
768 (def-lazy-reader lexterm s#prn find-lprn-eui eui)
769 (def-lazy-reader lexterm s#prp find-lprp-eui eui)
770 (def-lazy-reader lexterm s#spl find-lspl-eui eui)
771 (def-lazy-reader lexterm s#trm find-ltrm-eui eui)
772 (def-lazy-reader lexterm s#typ find-ltyp-eui eui)
774 ;; LEX SQL Read functions
776 (defun find-labr-eui (eui)
777 (gu:awhen (mutex-sql-query
778 (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
780 (dolist (tuple gu::it)
782 (make-instance 'labr :eui eui
785 :eui2 (ensure-integer (nth 2 tuple))
788 (nreverse results))))
790 (defun find-labr-bas (bas)
791 (gu:awhen (mutex-sql-query
792 (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
794 (dolist (tuple gu::it)
796 (make-instance 'labr :eui (ensure-integer (nth 0 tuple))
799 :eui2 (ensure-integer (nth 2 tuple))
802 (nreverse results))))
804 (defun find-lagr-eui (eui)
805 (gu:awhen (mutex-sql-query
806 (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
808 (dolist (tuple gu::it)
818 (nreverse results))))
820 (defun find-lcmp-eui (eui)
821 (gu:awhen (mutex-sql-query
822 (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
824 (dolist (tuple gu::it)
832 (nreverse results))))
834 (defun find-lmod-eui (eui)
835 (gu:awhen (mutex-sql-query
836 (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
838 (dolist (tuple gu::it)
844 :psnmod (nth 2 tuple)
847 (nreverse results))))
849 (defun find-lnom-eui (eui)
850 (gu:awhen (mutex-sql-query
851 (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
853 (dolist (tuple gu::it)
859 :eui2 (ensure-integer (nth 2 tuple))
863 (nreverse results))))
865 (defun find-lprn-eui (eui)
866 (gu:awhen (mutex-sql-query
867 (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
869 (dolist (tuple gu::it)
881 (nreverse results))))
883 (defun find-lprp-eui (eui)
884 (gu:awhen (mutex-sql-query
885 (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
887 (dolist (tuple gu::it)
896 (nreverse results))))
898 (defun find-lspl-eui (eui)
899 (gu:awhen (mutex-sql-query
900 (format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
902 (dolist (tuple gu::it)
909 (nreverse results))))
912 (defun find-ltrm-eui (eui)
913 (gu:awhen (mutex-sql-query
914 (format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
916 (dolist (tuple gu::it)
923 (nreverse results))))
925 (defun find-ltyp-eui (eui)
926 (gu:awhen (mutex-sql-query
927 (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
929 (dolist (tuple gu::it)
937 (nreverse results))))
939 (defun find-lwd-wrd (wrd)
940 (gu:awhen (mutex-sql-query
941 (format nil "select EUI from LRWD where WRD='~a'" wrd))
943 (dolist (tuple gu::it)
944 (push (ensure-integer (nth 0 tuple)) results))
945 (make-instance 'lwd :wrd wrd
946 :euilist (nreverse results)))))
948 ;;; Semantic Network SQL access functions
950 (defun find-sdef-ui (ui)
951 (gu:awhen (car (mutex-sql-query
952 (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
953 (make-instance 'sdef :rt (nth 0 gu::it)
955 :styrl (nth 1 gu::it)
956 :stnrtn (nth 2 gu::it)
962 :rin (nth 8 gu::it))))
964 (defun find-sstre1-ui (ui)
965 (gu:awhen (mutex-sql-query
966 (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
968 (dolist (tuple gu::it)
970 (make-instance 'sstre1 :ui ui
971 :ui2 (ensure-integer (nth 0 tuple))
972 :ui3 (ensure-integer (nth 1 tuple)))
974 (nreverse results))))
976 (defun find-sstre1-ui2 (ui2)
977 (gu:awhen (mutex-sql-query
978 (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
980 (dolist (tuple gu::it)
982 (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
984 :ui3 (ensure-integer (nth 1 tuple)))
986 (nreverse results))))
988 (defun find-sstr-rl (rl)
989 (gu:awhen (mutex-sql-query
990 (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
992 (dolist (tuple gu::it)
997 :styrl2 (nth 1 tuple)
1000 (nreverse results))))
1003 (defun find-sstre2-sty (sty)
1004 (gu:awhen (mutex-sql-query
1005 (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
1006 (let ((results '()))
1007 (dolist (tuple gu::it)
1009 (make-instance 'sstre2
1012 :sty2 (nth 1 tuple))
1014 (nreverse results))))
1016 (defun find-sstr-styrl (styrl)
1017 (gu:awhen (mutex-sql-query
1018 (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
1019 (let ((results '()))
1020 (dolist (tuple gu::it)
1022 (make-instance 'sstr :styrl styrl
1024 :styrl2 (nth 1 tuple)
1027 (nreverse results))))