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