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