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