r3016: *** empty log message ***
[umlisp.git] / sql-classes.lisp
index e193f8b89975e5e98daef92295e49b70c056e3e9..2fc138bbe8aa6dcfcfe896e9b59905128dd69d97 100644 (file)
@@ -1,8 +1,24 @@
-;;; $Id: sql-classes.lisp,v 1.1 2002/10/08 22:08:56 kevin Exp $
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name:          sql-classes.lisp
+;;;; Purpose:       Routines for reading UMLS objects from SQL database
+;;;; Programmer:    Kevin M. Rosenberg
+;;;; Date Started:  Apr 2000
+;;;;
+;;;; $Id: sql-classes.lisp,v 1.4 2002/10/14 09:25:20 kevin Exp $
+;;;;
+;;;; This file, part of UMLisp, is
+;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
+;;;;
+;;;; UMLisp users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the GNU General Public License.
+;;;; *************************************************************************
+
 (in-package :umlisp)
+(declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
 
-(declaim (optimize (speed 3) (safety 1)))
 
 (defvar *current-srl* nil)
 (defun current-srl ()
 ;;; Lookup functions for uterms,ustr in ucons
 
 (defun find-uterm-in-ucon (ucon lui)
-  (find lui (s#term ucon) :key #'uterm-lui :test 'equal))
+  (find lui (s#term ucon) :key #'lui :test 'equal))
 
 (defun find-ustr-in-uterm (uterm sui)
-  (find sui (s#str uterm) :key #'ustr-sui :test 'equal))
+  (find sui (s#str uterm) :key #'sui :test 'equal))
 
 (defun find-ustr-in-ucon (ucon sui)
   (let ((found-ustr nil))
@@ -79,9 +95,9 @@
        (if srl
            (string-append ls (format nil " and KCUILRL <= ~d limit 1" srl))
          (string-append ls " limit 1"))
-       (gu:awhen (car (mutex-sql-query ls))
-                 (make-instance 'ucon :cui cui :pfstr (car gu::it) 
-                                :lrl (ensure-integer (cadr gu::it)))))
+       (kmrcl:awhen (car (mutex-sql-query ls))
+                 (make-instance 'ucon :cui cui :pfstr (car kmrcl::it) 
+                                :lrl (ensure-integer (cadr kmrcl::it)))))
     nil))
 
 (defun find-ucon-lui (lui &key (srl *current-srl*))
                        (make-cuisui cui sui))))
        (when srl
            (string-append ls (format nil " and KCUILRL <= ~d" srl)))
-       (gu:aif (car (mutex-sql-query ls))
-            (make-instance 'ucon :cui (ensure-integer (nth 0 gu::it)) 
-                           :pfstr (nth 1 gu::it)
-                           :lrl (ensure-integer (nth 2 gu::it)))
+       (kmrcl:aif (car (mutex-sql-query ls))
+            (make-instance 'ucon :cui (ensure-integer (nth 0 kmrcl::it)) 
+                           :pfstr (nth 1 kmrcl::it)
+                           :lrl (ensure-integer (nth 2 kmrcl::it)))
             nil))
     nil))
 
 
 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
   (mapcar 
-   #'(lambda (cui) (find-ucon-cui cui :key srl))
+   #'(lambda (cui) (find-ucon-cui cui :srl srl))
    (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
 
 (defun find-ucoc-cui (cui &key (srl *current-srl*))
 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
   "List of ucon with co-occurance cui2"
   (mapcar 
-   #'(lambda (cui) (find-ucon-cui cui :key srl))
+   #'(lambda (cui) (find-ucon-cui cui :srl srl))
    (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
 
 (defun find-ulo-cui (cui &key (srl *current-srl*))
   (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
     (when srl
        (string-append ls (format nil " and KLUILRL <= ~d" srl)))
-    (gu:aif (car (mutex-sql-query ls))
+    (kmrcl:aif (car (mutex-sql-query ls))
         (make-instance 'uterm :cui cui
                        :lui lui
-                       :lat (nth 0 gu::it)
-                      :ts (nth 1 gu::it)
-                      :lrl (ensure-integer (nth 2 gu::it)))
+                       :lat (nth 0 kmrcl::it)
+                      :ts (nth 1 kmrcl::it)
+                      :lrl (ensure-integer (nth 2 kmrcl::it)))
         nil)))
 
 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
                    (make-cuisui cui sui))))
     (when srl
        (string-append ls (format nil " and LRL <= ~d" srl)))
-    (gu:aif (car (mutex-sql-query ls))
+    (kmrcl:aif (car (mutex-sql-query ls))
         (make-instance 'ustr :sui sui 
                        :cui cui
                        :cuisui (make-cuisui cui sui)
-                       :lui (ensure-integer (nth 0 gu::it))
-                       :stt (nth 1 gu::it)
-                       :str (nth 2 gu::it)
-                       :lrl (ensure-integer (nth 3 gu::it)))
+                       :lui (ensure-integer (nth 0 kmrcl::it))
+                       :stt (nth 1 kmrcl::it)
+                       :str (nth 2 kmrcl::it)
+                       :lrl (ensure-integer (nth 3 kmrcl::it)))
         nil)))
 
 (defun find-ustr-sui (sui &key (srl *current-srl*))
 (defun find-usty-tui (tui)
   "Find usty for tui"
   (setq tui (parse-tui tui)) 
-    (gu:aif (car (mutex-sql-query 
+    (kmrcl:aif (car (mutex-sql-query 
               (format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
-        (make-instance 'usty :tui tui :sty (nth 0 gu::it))
+        (make-instance 'usty :tui tui :sty (nth 0 kmrcl::it))
         nil))
 
 (defun find-usty-sty (sty)
   "Find usty for a sty"
-  (gu:aif (car (mutex-sql-query 
+  (kmrcl:aif (car (mutex-sql-query 
                (format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
-         (make-instance 'usty :tui (ensure-integer (nth 0 gu::it)) :sty sty)
+         (make-instance 'usty :tui (ensure-integer (nth 0 kmrcl::it)) :sty sty)
          nil))
 
 (defun find-usty-all ()
       (push (find-usty-tui (nth 0 tuple)) ustys))
     (nreverse ustys)))
 
-(defun find-usty_freq-all ()
-  (let ((usty_freqs '()))
-    (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
-      (let* ((tui (car tuple))
-            (freq (ensure-integer 
-                    (caar (mutex-sql-query 
-                           (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
-       (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
-    (sort usty_freqs #'> :key #'usty_freq-freq)))
-       
-
-
 
 (defun find-cui-max ()
   (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
       (setq ucons (append ucons (find-ucon-word word :srl srl))))
     (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
 
-(defun find-ucon-normalized-multiword (str &key (srl *current-srl*))
-  "Return sorted list of ucon's that match a multiword string"
-  (let* ((words (delimited-string-to-list str #\space))
-        (ucons '())
-        (nwords '()))
-    (dolist (word words)
-      (let ((nws (lvg:process-terms word)))
-       (dolist (nword nws)
-         (push nword nwords))))
-    (dolist (word nwords)
-      (setq ucons (append ucons (find-ucon-word word :srl srl))))
-    (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
-
 (defun find-ustr-multiword (str &key (srl *current-srl*))
   "Return sorted list of ustr's that match a multiword string"
   (let* ((words (delimited-string-to-list str #\space))
     (dolist (word words)
       (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
     (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
-
-(defun find-ustr-normalized-multiword (str &key (srl *current-srl*))
-  "Return sorted list of ustr's that match a multiword string"
-  (let* ((words (delimited-string-to-list str #\space))
-        (ustrs '())
-        (nwords '()))
-    (dolist (word words)
-      (let ((nws (lvg:process-terms word)))
-       (dolist (nword nws)
-         (push nword nwords))))
-    (dolist (word nwords)
-      (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
-    (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'ustr-cui))))
-
-(defun a (str)
-  (find-normalized-matches-for-str str #'find-ustr-normalized-word #'ustr-sui))
-
-(defun find-normalized-matches-for-str (str lookup-func key-func)
-  "Return list of objects that normalize match for words in string,
-eliminate duplicates."
-  (let ((objs '())
-       (nwords '()))
-    (dolist (word (delimited-string-to-list str #\space))
-      (dolist (nword (lvg:process-terms word))
-       (unless (member nword nwords :test #'string-equal)
-         (push nword nwords))))
-    (dolist (nw nwords)
-      (setq objs (append objs (funcall lookup-func nw))))
-    (delete-duplicates objs :key key-func :test #'eql)))
        
 (defun sort-score-ucon-str (str ucons)
   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
@@ -732,9 +694,9 @@ eliminate duplicates."
     (decf score (- nlong nshort)) ;; reduce score for extra words
     (dotimes (iword nshort)
       (declare (fixnum iword))
-      (gu:aif (position (nth iword short-list) long-list :test #'string-equal)
+      (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
           (progn
-            (incf score (- 10 (abs (- gu::it iword))))
+            (incf score (- 10 (abs (- kmrcl::it iword))))
             (decf unmatched))))
     (decf score (* 2 unmatched))
     score))
@@ -743,15 +705,15 @@ eliminate duplicates."
 ;;; LEX SQL functions
 
 (defun find-lexterm-eui (eui)
-  (gu:awhen (car (mutex-sql-query
+  (kmrcl:awhen (car (mutex-sql-query
                  (format nil "select WRD from LRWD where EUI=~d" eui)))
-           (make-instance 'lexterm :eui eui :wrd (nth 0 gu:it))))
+           (make-instance 'lexterm :eui eui :wrd (nth 0 kmrcl:it))))
 
 (defun find-lexterm-word (wrd)
-  (gu:awhen (mutex-sql-query
+  (kmrcl:awhen (mutex-sql-query
             (format nil "select EUI from LRWD where WRD='~a'" wrd))
            (let ((terms '()))
-             (dolist (tuple gu:it)
+             (dolist (tuple kmrcl:it)
                (let ((eui (ensure-integer (nth 0 tuple))))
                  (push
                   (make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
@@ -774,10 +736,10 @@ eliminate duplicates."
 ;; LEX SQL Read functions
 
 (defun find-labr-eui (eui)
-    (gu:awhen (mutex-sql-query 
+    (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'labr :eui eui 
                                  :bas (nth 0 tuple) 
@@ -788,10 +750,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-labr-bas (bas)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'labr :eui (ensure-integer (nth 0 tuple))
                                  :bas (copy-seq bas)
@@ -802,10 +764,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lagr-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lagr 
                                  :eui eui
@@ -818,10 +780,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lcmp-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lcmp
                                  :eui eui
@@ -832,10 +794,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lmod-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lmod
                                  :eui eui
@@ -847,10 +809,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lnom-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lnom
                                  :eui eui
@@ -863,10 +825,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lprn-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lprn
                                  :eui eui
@@ -881,10 +843,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lprp-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lprp
                                  :eui eui
@@ -896,10 +858,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lspl-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'lspl
                                  :eui eui
@@ -910,10 +872,10 @@ eliminate duplicates."
 
 
 (defun find-ltrm-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'ltrm
                                  :eui eui
@@ -923,10 +885,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-ltyp-eui (eui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'ltyp
                                  :eui eui
@@ -937,10 +899,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-lwd-wrd (wrd)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
             (format nil "select EUI from LRWD where WRD='~a'" wrd))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push (ensure-integer (nth 0 tuple)) results))
                (make-instance 'lwd :wrd wrd
                               :euilist (nreverse results)))))
@@ -948,24 +910,24 @@ eliminate duplicates."
 ;;; Semantic Network SQL access functions
  
 (defun find-sdef-ui (ui)
-  (gu:awhen (car (mutex-sql-query 
+  (kmrcl:awhen (car (mutex-sql-query 
                  (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
-           (make-instance 'sdef :rt (nth 0 gu::it)
+           (make-instance 'sdef :rt (nth 0 kmrcl::it)
                           :ui ui
-                          :styrl (nth 1 gu::it)
-                          :stnrtn (nth 2 gu::it)
-                          :def (nth 3 gu::it)
-                          :ex (nth 4 gu::it)
-                          :un (nth 5 gu::it)
-                          :rh (nth 6 gu::it)
-                          :abr (nth 7 gu::it)
-                          :rin (nth 8 gu::it))))
+                          :styrl (nth 1 kmrcl::it)
+                          :stnrtn (nth 2 kmrcl::it)
+                          :def (nth 3 kmrcl::it)
+                          :ex (nth 4 kmrcl::it)
+                          :un (nth 5 kmrcl::it)
+                          :rh (nth 6 kmrcl::it)
+                          :abr (nth 7 kmrcl::it)
+                          :rin (nth 8 kmrcl::it))))
 
 (defun find-sstre1-ui (ui)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'sstre1 :ui ui
                                  :ui2 (ensure-integer (nth 0 tuple))
@@ -974,10 +936,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-sstre1-ui2 (ui2)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
                                  :ui2 ui2
@@ -986,10 +948,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-sstr-rl (rl)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'sstr 
                                  :rl rl
@@ -1001,10 +963,10 @@ eliminate duplicates."
 
 
 (defun find-sstre2-sty (sty)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
             (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
            (let ((results '()))
-             (dolist (tuple gu::it)
+             (dolist (tuple kmrcl::it)
                (push
                 (make-instance 'sstre2
                                :sty (copy-seq sty)
@@ -1014,10 +976,10 @@ eliminate duplicates."
                (nreverse results))))
 
 (defun find-sstr-styrl (styrl)
-  (gu:awhen (mutex-sql-query 
+  (kmrcl:awhen (mutex-sql-query 
               (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
              (let ((results '()))
-               (dolist (tuple gu::it)
+               (dolist (tuple kmrcl::it)
                  (push
                   (make-instance 'sstr :styrl styrl
                                  :rl (nth 0 tuple)