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