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