r4783: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          sql-classes.lisp
6 ;;;; Purpose:       Routines for reading UMLS objects from SQL database
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: sql-classes.lisp,v 1.57 2003/05/03 22:48:55 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
14 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package :umlisp)
20 (declaim (optimize (compilation-speed 0) (debug 3)))
21
22
23 (defvar *current-srl* nil)
24 (defun current-srl ()
25   *current-srl*)
26 (defun current-srl! (srl)
27   (setq *current-srl* srl))
28
29 ;; only WHERE-VALUE and SRL are evaluated
30 (defmacro with-umlisp-query ((table fields srl where-name where-value
31                                     &key (lrl "KCUILRL") distinct single
32                                     order like)
33                              &body body)
34   (let ((value (gensym)))
35     (if single
36         `(let ((,value ,where-value))
37           (when ,value 
38             (let ((tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
39                                             :lrl ,lrl :single ,single
40                                             :distinct ,distinct :order ,order
41                                             :like ,like))))
42               (when tuple
43                 (destructuring-bind ,fields tuple
44                   ,@body)))))
45         `(let ((,value ,where-value))
46           (when ,value 
47             (loop for tuple in
48                   (umlisp-query ,table ,fields ,srl ,where-name ,value
49                                 :lrl ,lrl :single ,single :distinct ,distinct
50                                 :order ,order :like ,like)
51                   collect (destructuring-bind ,fields tuple
52                             ,@body)))))))
53
54 (defmacro umlisp-query (table fields srl where-name where-value
55                      &key (lrl "KCUILRL") single distinct order like)
56   "Query the UMLisp database. Return a list of umlisp objects whose name
57 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
58   `(mutex-sql-query
59     (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
60      :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
61
62   
63 (defmacro query-string-macro (table fields &optional srl where-name where-value
64                         &key (lrl "KCUILRL") single distinct order like)
65   (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
66                            (if distinct "distinct " "") fields table))
67          (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) ""))
68          (%%lrl (format nil " and ~:@(~A~)<=" lrl))
69          (%%where (when where-name
70                     (format nil " where ~:@(~A~)~A" where-name
71                           (if like " like " "=")))))
72     `(concatenate
73       'string
74       ,%%fields
75       ,@(when %%where (list %%where))
76       ,@(when %%where
77               `((if (numberp ,where-value)
78                     (write-to-string ,where-value)
79                     (format nil ,(if like "'%~A%'" "'~A'") ,where-value))))
80       (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
81       ,@(when %%order (list %%order))
82       ,@(when single (list " limit 1")))))
83
84 (defun query-string (table fields &optional srl where-name where-value
85                      &key (lrl "KCUILRL") single distinct order like)
86   (concatenate
87    'string
88    (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)" 
89            (if distinct "distinct " "") fields table)
90    (if where-name
91        (format nil
92                (if (stringp where-value)
93                    (if like
94                        " where ~A like '%~A%'"
95                        " where ~A='~A'")
96                    " where ~A=~A")
97                where-name  where-value)
98        "")
99    (if srl (format nil " and ~:@(~A~) <= ~D" lrl srl) "")
100    (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
101    (if single " limit 1" "")))
102
103 (defun find-ucon-cui (cui &key (srl *current-srl*))
104   "Find ucon for a cui"
105   (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui (parse-cui cui) :single t)
106     (make-instance 'ucon :cui (parse-cui cui)
107                    :pfstr kpfstr
108                    :lrl (ensure-integer kcuilrl))))
109
110 (defun find-ucon-cui-old (cui &key (srl *current-srl*))
111   "Find ucon for a cui"
112   (when (stringp cui) (setq cui (parse-cui cui)))
113   (when cui
114     (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d" cui)))
115       (when srl
116         (string-append ls (format nil " and KCUILRL <= ~d" srl)))
117       (string-append ls " limit 1")
118       (let ((tuple (car (mutex-sql-query ls))))
119         (destructuring-bind (kpfstr kcuilrl) tuple
120           (make-instance 'ucon :cui cui :pfstr kpfstr
121                          :lrl (ensure-integer kcuilrl)))))))
122
123 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
124   "Find ucon for a cui"
125   (with-umlisp-query (mrcon (kcuilrl) srl cui (parse-cui cui) :single t)
126     (make-instance 'ucon :cui (parse-cui cui)
127                    :lrl (ensure-integer kcuilrl)
128                    :pfstr nil)))
129
130 (defun find-pfstr-cui (cui &key (srl *current-srl*))
131   "Find preferred string for a cui"
132   (with-umlisp-query (mrcon (kpfstr) srl cui (parse-cui cui) :single t)
133     kpfstr))
134
135 (defun find-ucon-lui (lui &key (srl *current-srl*))
136   "Find list of ucon for lui"
137   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui (parse-lui lui) :distinct t)
138     (make-instance 'ucon :cui (ensure-integer cui)
139                    :pfstr kpfstr
140                    :lrl (ensure-integer kcuilrl))))
141
142 (defun find-ucon-sui (sui &key (srl *current-srl*))
143   "Find list of ucon for sui"
144   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui (parse-sui sui) :distinct t)
145     (make-instance 'ucon :cui (ensure-integer cui)
146                    :pfstr kpfstr
147                    :lrl (ensure-integer kcuilrl))))
148
149 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
150   "Find ucon for cui/sui"
151   (when (and cui sui)
152     (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl kcuisui 
153                                (make-cuisui (parse-cui cui) (parse-sui sui)))
154       (make-instance 'ucon :cui (ensure-integer cui)
155                      :pfstr kpfstr
156                      :lrl (ensure-integer kcuilrl)))))
157
158 (defun find-ucon-str (str &key (srl *current-srl*))
159   "Find ucon that are exact match for str"
160   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
161     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
162                    :lrl (ensure-integer kcuilrl))))
163
164 (defun find-ucon-all (&key (srl *current-srl*))
165   "Return list of all ucon's"
166   (with-sql-connection (db)
167     (clsql:map-query 
168      'list
169      #'(lambda (cui pfstr cuilrl)
170          (make-instance 'ucon :cui (ensure-integer cui)
171                         :pfstr pfstr
172                         :lrl (ensure-integer cuilrl)))
173      (query-string-macro mrcon (cui kpfstr kcuilrl) srl nil nil
174                    :order (cui asc) :distinct t)
175      :database db)))
176
177 (defun map-ucon-all (fn &key (srl *current-srl*))
178   "Map a function over all ucon's"
179   (with-sql-connection (db)
180     (clsql:map-query 
181      nil
182      #'(lambda (cui pfstr cuilrl)
183          (funcall fn
184                   (make-instance 'ucon :cui (ensure-integer cui)
185                                  :pfstr pfstr
186                                  :lrl (ensure-integer cuilrl))))
187      (query-string-macro mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
188      :database db)))
189
190
191 (defun find-udef-cui (cui &key (srl *current-srl*))
192   "Return a list of udefs for cui"
193   (with-umlisp-query (mrdef (sab def) srl cui (parse-cui cui) :lrl "KSRL")
194     (make-instance 'udef :sab sab :def def)))
195
196 (defun find-usty-cui (cui &key (srl *current-srl*))
197   "Return a list of usty for cui"
198   (with-umlisp-query (mrsty (tui sty) srl cui (parse-cui cui) :lrl "KLRL")
199     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
200
201 (defun find-usty-word (word &key (srl *current-srl*))
202   "Return a list of usty that match word"
203   (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t :distinct t)
204     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
205
206 (defun find-urel-cui (cui &key (srl *current-srl*))
207   "Return a list of urel for cui"
208   (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1 (parse-cui cui) :lrl "KSRL")
209     (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela
210                    :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
211
212 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
213   "Return a list of urel for cui2"
214   (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2 (parse-cui cui2) :lrl "KSRL")
215     (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela
216                    :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
217
218 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
219   (mapcar 
220    #'(lambda (cui) (find-ucon-cui cui :srl srl))
221    (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
222
223 (defun find-ucoc-cui (cui &key (srl *current-srl*))
224   "Return a list of ucoc for cui"
225   (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1 (parse-cui cui) 
226                              :lrl "KSRL" :order (cof asc))
227     (setq cui2 (ensure-integer cui2))
228     (when (zerop cui2) (setq cui2 nil))
229     (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc :cot cot
230                    :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
231
232 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
233   "Return a list of ucoc for cui2"
234   (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2 (parse-cui cui2) 
235                              :lrl "KSRL" :order (cof asc))
236     (setq cui2 (ensure-integer cui2))
237     (when (zerop cui2) (setq cui2 nil))
238     (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc :cot cot
239                    :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
240
241 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
242   "List of ucon with co-occurance cui2"
243   (mapcar 
244    #'(lambda (cui) (find-ucon-cui cui :srl srl))
245    (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
246
247 (defun find-ulo-cui (cui &key (srl *current-srl*))
248   "Return a list of ulo for cui"
249   (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui (parse-cui cui) :lrl "KLRL")
250     (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui) :sna sna
251                    :soui soui)))
252
253 (defgeneric suistr (lo))
254 (defmethod suistr ((lo ulo))
255   "Return the string for a ulo object"
256   (find-string-sui (sui lo)))
257
258 (defun find-uatx-cui (cui &key (srl *current-srl*))
259   "Return a list of uatx for cui"
260   (with-umlisp-query (mratx (sab rel atx) srl cui (parse-cui cui) :lrl ksrl)
261     (make-instance 'uatx :sab sab :rel rel :atx atx)))
262
263
264 (defun find-uterm-cui (cui &key (srl *current-srl*))
265   "Return a list of uterm for cui"
266   (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui (parse-cui cui) :lrl kluilrl
267                              :distinct t)
268     (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui)
269                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
270
271 (defun find-uterm-lui (lui &key (srl *current-srl*))
272   "Return a list of uterm for lui"
273   (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui (parse-lui lui) 
274                              :lrl kluilrl :distinct t)
275     (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui)
276                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
277
278 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
279   "Return single uterm for cui/lui"
280   (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
281                              (make-cuilui (parse-cui cui) (parse-lui lui))
282                              :lrl kluilrl :single t)
283     (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer kluilrl))))
284
285 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
286   "Return a list of ustr for cui/lui"
287   (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui (make-cuilui cui lui) :lrl lrl)
288     (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
289                    :cuisui (make-cuisui cui sui) :stt stt :str str
290                    :lrl (ensure-integer lrl))))
291
292 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
293   "Return the single ustr for cuisui"
294   (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui (make-cuisui cui sui) :lrl lrl :single t)
295     (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
296                    :lui (ensure-integer lui) :stt stt :str str :lrl (ensure-integer lrl))))
297
298 (defun find-ustr-sui (sui &key (srl *current-srl*))
299   "Return the list of ustr for sui"
300   (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui (parse-sui sui) :lrl lrl)
301     (make-instance 'ustr :sui sui :cui cui :stt stt :str str
302                    :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui))
303                    :lui (ensure-integer lui)
304                    :lrl (ensure-integer lrl))))
305       
306 (defun find-ustr-sab (sab &key (srl *current-srl*))
307   "Return the list of ustr for sab"
308   (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
309     (let ((cuisui (ensure-integer kcuisui)))
310       (apply #'find-ustr-cuisui 
311              (append
312               (multiple-value-list (decompose-cuisui cuisui)) (list :srl srl))))))
313
314 (defun find-ustr-all (&key (srl *current-srl*))
315   "Return list of all ustr's"
316     (with-sql-connection (db)
317       (clsql:map-query 
318        'list
319        #'(lambda (cui lui sui stt lrl pfstr)
320            (setq cui (ensure-integer cui))
321            (setq lui (ensure-integer lui))
322            (setq sui (ensure-integer sui))      
323            (setq lrl (ensure-integer lrl))
324            (make-instance 'ustr :cui cui
325                           :lui lui
326                           :sui sui
327                           :cuisui (make-cuisui cui sui)
328                           :stt stt
329                           :lrl lrl
330                           :str pfstr))
331        (query-string-macro mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
332                      :order (sui asc))
333        :database db)))
334
335 (defun find-string-sui (sui &key (srl *current-srl*))
336   "Return the string associated with sui"
337   (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
338     str))
339
340 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
341   (with-umlisp-query (mrso (sab code srl tty) srl kcuisui (make-cuisui cui sui) :lrl srl)
342       (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
343
344 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
345   (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc) srl kcuisui
346                              (make-cuisui cui sui) :lrl ksrl)
347     (make-instance 'ucxt :sab sab :code code
348                    :cxn (ensure-integer cxn)
349                    :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc
350                    :rnk (ensure-integer rnk)
351                    :cui2 (ensure-integer cui2))))
352
353 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
354   (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
355     (cond
356       (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
357       (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
358       (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
359     (when srl
360       (string-append ls (format nil " and KSRL <= ~d" srl)))
361     (loop for tuple in (mutex-sql-query ls) collect 
362           (destructuring-bind (code atn sab atv) tuple
363             (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
364
365 (defun find-usty-tui (tui)
366   "Find usty for tui"
367   (with-umlisp-query (mrsty (sty) nil tui (parse-tui tui) :single t)
368     (make-instance 'usty :tui (parse-tui tui) :sty sty)))
369
370 (defun find-usty-sty (sty)
371   "Find usty for a sty"
372   (with-umlisp-query (mrsty (tui) nil sty sty :single t)
373     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
374
375 (defun find-usty-all ()
376   "Return list of usty's for all semantic types"
377   (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
378     (find-usty-tui tui)))
379
380 (defun find-usab-all ()
381   "Find usab for a key"
382   (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil nil nil)
383     (make-instance 'usab :vcui (ensure-integer vcui) 
384                    :rcui (ensure-integer rcui)
385                    :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
386                    :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
387                    :srl (ensure-integer srl) 
388                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
389                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
390                    :curver curver :sabin sabin)))
391
392 (defun find-usab-by-key (key-name key)
393   "Find usab for a key"
394   (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil key-name key :single t)
395     (make-instance 'usab :vcui (ensure-integer vcui) 
396                    :rcui (ensure-integer rcui)
397                    :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
398                    :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
399                    :srl (ensure-integer srl) 
400                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
401                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
402                    :curver curver :sabin sabin)))
403
404 (defun find-usab-rsab (rsab)
405   "Find usab for rsab"
406   (find-usab-by-key "RSAB" rsab))
407
408 (defun find-usab-vsab (vsab)
409   "Find usab for vsab"
410   (find-usab-by-key "VSAB" vsab))
411
412 (defun find-cui-max ()
413   (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
414
415 ;;;; Cross table find functions
416
417 (defun find-ucon-tui (tui &key (srl *current-srl*))
418   "Find list of ucon for tui"
419   (with-umlisp-query (mrsty (cui) srl tui (parse-tui tui) :lrl klrl
420                              :order (cui asc))
421     (find-ucon-cui (ensure-integer cui) :srl srl)))
422   
423 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
424   "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
425   (with-umlisp-query (mrxw_eng (cui) srl wd word :like like :distinct t
426                                 :lrl klrl :order (cui asc))
427     (find-ucon-cui cui :srl srl)))
428
429 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
430   "Return list of ucons that match word, optionally use SQL's LIKE syntax"
431   (with-umlisp-query (mrxnw_eng (cui) srl nwd word :like like :distinct t
432                                 :lrl klrl :order (cui asc))
433     (find-ucon-cui cui :srl srl)))
434
435 (defun find-ustr-word (word &key (srl *current-srl*))
436   "Return list of ustrs that match word"
437   (with-umlisp-query (mrxw_eng (cui sui) srl wd word
438                                 :lrl klrl
439                                 :order (cui asc sui asc))
440     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
441
442 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
443   "Return list of ustrs that match word"
444   (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
445                                  :order (cui asc sui asc))
446     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
447
448 ;; Special tables
449
450 (defun find-usrl-all ()
451   (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
452     (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
453
454 ;;; Multiword lookup and score functions
455
456 (defun find-ucon-multiword (str &key (srl *current-srl*))
457   "Return sorted list of ucon's that match a multiword string"
458   (let* ((words (delimited-string-to-list str #\space))
459          (ucons '()))
460     (dolist (word words)
461       (setq ucons (append ucons (find-ucon-word word :srl srl))))
462     (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
463
464 (defun find-ustr-multiword (str &key (srl *current-srl*))
465   "Return sorted list of ustr's that match a multiword string"
466   (let* ((words (delimited-string-to-list str #\space))
467          (ustrs '()))
468     (dolist (word words)
469       (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
470     (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
471         
472 (defun sort-score-ucon-str (str ucons)
473   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
474   (sort-score-umlsclass-str ucons str #'pfstr))
475
476 (defun sort-score-ustr-str (str ustrs)
477   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
478   (sort-score-umlsclass-str ustrs str #'str))
479
480 (defun sort-score-umlsclass-str (objs str lookup-func)
481   "Sort a list of objects based on scoring to a string"
482   (let ((scored '()))
483     (dolist (obj objs)
484       (push 
485        (list obj 
486              (score-multiword-match str (funcall lookup-func obj))) 
487        scored))
488     (mapcar #'car (sort scored #'> :key #'cadr))))
489
490 (defun score-multiword-match (s1 s2)
491   "Score a match between two strings with s1 being reference string"
492   (let* ((word-list-1 (delimited-string-to-list s1 #\space))
493          (word-list-2 (delimited-string-to-list s2 #\space))
494          (n1 (length word-list-1))
495          (n2 (length word-list-2))
496          (unmatched n1)
497          (score 0)
498          (nlong 0)
499          (nshort 0)
500          short-list long-list)
501     (declare (fixnum n1 n2 nshort nlong score unmatched))
502     (if (> n1 n2)
503         (progn
504           (setq nlong n1)
505           (setq nshort n2)
506           (setq long-list word-list-1)
507           (setq short-list word-list-2))
508       (progn
509         (setq nlong n2)
510         (setq nshort n1)
511         (setq long-list word-list-2)
512         (setq short-list word-list-1)))
513     (decf score (- nlong nshort)) ;; reduce score for extra words
514     (dotimes (iword nshort)
515       (declare (fixnum iword))
516       (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
517            (progn
518              (incf score (- 10 (abs (- kmrcl::it iword))))
519              (decf unmatched))))
520     (decf score (* 2 unmatched))
521     score))
522
523
524 ;;; LEX SQL functions
525
526 (defun find-lexterm-eui (eui)
527   (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
528     (make-instance 'lexterm :eui eui :wrd wrd)))
529
530 (defun find-lexterm-word (wrd)
531   (with-umlisp-query (lrwd (eui) nil wrd wrd)
532     (make-instance 'lexterm :eui (ensure-integer eui)
533                    :wrd (copy-seq wrd))))
534
535 ;; LEX SQL Read functions
536
537 (defun find-labr-eui (eui)
538   (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui) 
539     (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
540                    :eui2 (ensure-integer eui2))))
541
542 (defun find-labr-bas (bas)
543   (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
544     (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
545                    :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
546
547 (defun find-lagr-eui (eui)
548   (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
549     (make-instance 'lagr :eui eui :str str :sca sca :agr agr
550                    :cit cit :bas bas)))
551
552 (defun find-lcmp-eui (eui)
553   (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
554     (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
555
556 (defun find-lmod-eui (eui)
557   (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
558     (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
559
560 (defun find-lnom-eui (eui)
561   (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
562     (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
563                    :eui2 (ensure-integer eui2))))
564
565 (defun find-lprn-eui (eui)
566   (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
567     (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
568                    :cas cas :pos pos :qnt qnt :fea fea)))
569
570 (defun find-lprp-eui (eui)
571   (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
572     (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
573
574 (defun find-lspl-eui (eui)
575   (with-umlisp-query (lrspl (spv bas) nil eui eui)
576     (make-instance 'lspl :eui eui :spv spv :bas bas)))
577
578 (defun find-ltrm-eui (eui)
579   (with-umlisp-query (lrtrm (bas gen) nil eui eui) 
580     (make-instance 'ltrm :eui eui :bas bas :gen gen)))
581
582 (defun find-ltyp-eui (eui)
583   (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
584     (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
585
586 (defun find-lwd-wrd (wrd)
587   (make-instance 'lwd :wrd
588                  :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
589                             (ensure-integer eui))))
590
591 ;;; Semantic Network SQL access functions
592
593 (defun find-sdef-ui (ui)
594   (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
595                             nil ui ui :single t)
596     (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
597                    :def def :ex ex :un un :rh rh :abr abr :rin rin)))
598
599 (defun find-sstre1-ui (ui)
600   (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
601     (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
602                    :ui3 (ensure-integer ui3))))
603
604 (defun find-sstre1-ui2 (ui2)
605   (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
606     (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
607                    :ui3 (ensure-integer ui3))))
608
609 (defun find-sstr-rl (rl)
610   (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
611     (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
612
613 (defun find-sstre2-sty (sty)
614   (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
615     (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
616
617 (defun find-sstr-styrl (styrl)
618   (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
619     (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))