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