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