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