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