1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
10 ;;;; $Id: sql-classes.lisp,v 1.88 2003/07/21 00:53:27 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2003 by Kevin M. Rosenberg, M.D.
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 ;;;; *************************************************************************
22 (defvar *current-srl* nil)
25 (defun current-srl! (srl)
26 (setq *current-srl* srl))
28 (defmacro query-string (table fields &optional srl where-name where-value
29 &key (lrl "KCUILRL") single distinct order like)
30 (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
31 (if distinct "distinct " "") fields table))
32 (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}"
35 (%%lrl (format nil " and ~:@(~A~)<=" lrl))
36 (%%where (when where-name
37 (format nil " where ~:@(~A~)~A" where-name
38 (if like " like " "")))))
42 ,@(when %%where (list %%where))
44 `((typecase ,where-value
46 (prefixed-fixnum-string ,where-value #\= 10))
48 (concatenate 'string "=" (write-to-string ,where-value)))
52 (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
53 (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
54 ,@(when %%order (list %%order))
55 ,@(when single (list " limit 1")))))
57 (defun query-string-eval (table fields &optional srl where-name where-value
58 &key (lrl "KCUILRL") single distinct order like)
61 (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
62 (if distinct "distinct " "") fields table)
63 (if where-name (format nil " where ~:@(~A~)" where-name) "")
70 (if like " like '%~A%""='~A'")))
73 (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
74 (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
75 (if single " limit 1" "")))
78 (defmacro umlisp-query (table fields srl where-name where-value
79 &key (lrl "KCUILRL") single distinct order like)
80 "Query the UMLisp database. Return a list of umlisp objects whose name
81 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
83 (query-string ,table ,fields ,srl ,where-name ,where-value
84 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
86 (defmacro umlisp-query-eval (table fields srl where-name where-value
87 &key (lrl "KCUILRL") single distinct order like)
88 "Query the UMLisp database. Return a list of umlisp objects whose name
89 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
91 (query-string-eval ,table ,fields ,srl ,where-name ,where-value
92 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
94 ;; only WHERE-VALUE and SRL are evaluated
95 (defmacro collect-umlisp-query ((table fields srl where-name where-value
96 &key (lrl "KCUILRL") distinct single
99 (let ((value (gensym))
102 `(let* ((,value ,where-value)
103 (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
104 :lrl ,lrl :single ,single
105 :distinct ,distinct :order ,order
107 ,@(unless where-name `((declare (ignore ,value))))
109 (destructuring-bind ,fields tuple
111 `(let ((,value ,where-value))
112 ,@(unless where-name `((declare (ignore ,value))))
114 (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
115 :lrl ,lrl :single ,single :distinct ,distinct
116 :order ,order :like ,like))
117 (push (destructuring-bind ,fields tuple ,@body) ,r))
121 (umlisp-query ,table ,fields ,srl ,where-name ,value
122 :lrl ,lrl :single ,single :distinct ,distinct
123 :order ,order :like ,like)
124 collect (destructuring-bind ,fields tuple ,@body))))))
126 (defmacro collect-umlisp-query-eval ((table fields srl where-name where-value
127 &key (lrl "KCUILRL") distinct single
130 (let ((value (gensym))
132 (eval-fields (cadr fields)))
134 `(let* ((,value ,where-value)
135 (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
136 :lrl ,lrl :single ,single
137 :distinct ,distinct :order ,order
140 (destructuring-bind ,eval-fields tuple
142 `(let ((,value ,where-value)
144 (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
145 :lrl ,lrl :single ,single :distinct ,distinct
146 :order ,order :like ,like))
147 (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
151 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
152 :lrl ,lrl :single ,single :distinct ,distinct
153 :order ,order :like ,like)
154 collect (destructuring-bind ,eval-fields tuple ,@body))))))
157 ;;; Read from SQL database
159 (defmacro ensure-cui-integer (cui)
161 (setq ,cui (parse-cui ,cui))
164 (defmacro ensure-lui-integer (lui)
166 (setq ,lui (parse-lui ,lui))
169 (defmacro ensure-sui-integer (sui)
171 (setq ,sui (parse-sui ,sui))
174 (defmacro ensure-tui-integer (tui)
176 (setq ,tui (parse-tui ,tui))
179 (defmacro ensure-eui-integer (eui)
181 (setq ,eui (parse-eui ,eui))
184 (defun find-ucon-cui (cui &key (srl *current-srl*))
185 "Find ucon for a cui"
186 (ensure-cui-integer cui)
187 (collect-umlisp-query (mrcon (kpfstr kcuilrl) srl cui cui :single t)
188 (make-instance 'ucon :cui cui :pfstr kpfstr
189 :lrl (ensure-integer kcuilrl))))
191 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
192 "Find ucon for a cui"
193 (ensure-cui-integer cui)
194 (collect-umlisp-query (mrcon (kcuilrl) srl cui cui :single t)
195 (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
198 (defun find-pfstr-cui (cui &key (srl *current-srl*))
199 "Find preferred string for a cui"
200 (ensure-cui-integer cui)
201 (collect-umlisp-query (mrcon (kpfstr) srl cui cui :single t)
204 (defun find-ucon-lui (lui &key (srl *current-srl*))
205 "Find list of ucon for lui"
206 (ensure-lui-integer lui)
207 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui lui
209 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
210 :lrl (ensure-integer kcuilrl))))
212 (defun find-ucon-sui (sui &key (srl *current-srl*))
213 "Find list of ucon for sui"
214 (ensure-sui-integer sui)
215 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui sui :distinct t)
216 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
217 :lrl (ensure-integer kcuilrl))))
219 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
220 "Find ucon for cui/sui"
221 (ensure-cui-integer cui)
222 (ensure-sui-integer sui)
224 (collect-umlisp-query (mrcon (kpfstr kcuilrl) srl kcuisui
225 (make-cuisui cui sui))
226 (make-instance 'ucon :cui cui
228 :lrl (ensure-integer kcuilrl)))))
230 (defun find-ucon-str (str &key (srl *current-srl*))
231 "Find ucon that are exact match for str"
232 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
233 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
234 :lrl (ensure-integer kcuilrl))))
236 (defun find-ucon-all (&key (srl *current-srl*))
237 "Return list of all ucon's"
238 (with-sql-connection (db)
241 #'(lambda (cui pfstr cuilrl)
242 (make-instance 'ucon :cui (ensure-integer cui)
244 :lrl (ensure-integer cuilrl)))
245 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
246 :order (cui asc) :distinct t)
249 (defun find-ucon-all2 (&key (srl *current-srl*))
250 "Return list of all ucon's"
251 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
253 (make-instance 'ucon :cui (ensure-integer cui)
255 :lrl (ensure-integer kcuilrl))))
257 (defun map-ucon-all (fn &key (srl *current-srl*))
258 "Map a function over all ucon's"
259 (with-sql-connection (db)
262 #'(lambda (cui pfstr cuilrl)
264 (make-instance 'ucon :cui (ensure-integer cui)
266 :lrl (ensure-integer cuilrl))))
267 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
272 (defun find-udef-cui (cui &key (srl *current-srl*))
273 "Return a list of udefs for cui"
274 (ensure-cui-integer cui)
275 (collect-umlisp-query (mrdef (sab def) srl cui cui :lrl "KSRL")
276 (make-instance 'udef :sab sab :def def)))
278 (defun find-usty-cui (cui &key (srl *current-srl*))
279 "Return a list of usty for cui"
280 (ensure-cui-integer cui)
281 (collect-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
282 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
284 (defun find-usty-word (word &key (srl *current-srl*))
285 "Return a list of usty that match word"
286 (collect-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
288 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
290 (defun find-urel-cui (cui &key (srl *current-srl*))
291 "Return a list of urel for cui"
292 (ensure-cui-integer cui)
293 (collect-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
295 (make-instance 'urel :cui1 cui :rel rel
296 :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
297 :mg mg :pfstr2 kpfstr2)))
299 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
300 "Return a list of urel for cui2"
301 (ensure-cui-integer cui2)
302 (collect-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
304 (make-instance 'urel :cui2 cui2 :rel rel
305 :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
306 :mg mg :pfstr2 kpfstr2)))
308 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
309 (ensure-cui-integer cui2)
310 (loop for cui in (remove-duplicates
311 (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
312 collect (find-ucon-cui cui :srl srl)))
314 (defun find-ucoc-cui (cui &key (srl *current-srl*))
315 "Return a list of ucoc for cui"
316 (ensure-cui-integer cui)
317 (collect-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
318 cui :lrl klrl :order (cof asc))
319 (setq cui2 (ensure-integer cui2))
320 (when (zerop cui2) (setq cui2 nil))
321 (make-instance 'ucoc :cui1 cui :cui2 (ensure-integer cui2)
322 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
325 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
326 "Return a list of ucoc for cui2"
327 (ensure-cui-integer cui2)
328 (collect-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
329 cui2 :lrl klrl :order (cof asc))
330 (when (zerop cui2) (setq cui2 nil))
331 (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
332 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
335 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
336 "List of ucon with co-occurance cui2"
337 (ensure-cui-integer cui2)
339 #'(lambda (cui) (find-ucon-cui cui :srl srl))
340 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
342 (defun find-ulo-cui (cui &key (srl *current-srl*))
343 "Return a list of ulo for cui"
344 (ensure-cui-integer cui)
345 (collect-umlisp-query (mrlo (isn fr un sui sna soui) srl cui cui
347 (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
348 :sui (ensure-integer sui) :sna sna :soui soui)))
350 (defun find-uatx-cui (cui &key (srl *current-srl*))
351 "Return a list of uatx for cui"
352 (ensure-cui-integer cui)
353 (collect-umlisp-query (mratx (sab rel atx) srl cui cui :lrl ksrl)
354 (make-instance 'uatx :sab sab :rel rel :atx atx)))
357 (defun find-uterm-cui (cui &key (srl *current-srl*))
358 "Return a list of uterm for cui"
359 (ensure-cui-integer cui)
360 (collect-umlisp-query (mrcon (lui lat ts kluilrl) srl cui cui
361 :lrl kluilrl :distinct t)
362 (make-instance 'uterm :lui (ensure-integer lui) :cui cui
363 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
365 (defun find-uterm-lui (lui &key (srl *current-srl*))
366 "Return a list of uterm for lui"
367 (ensure-lui-integer lui)
368 (collect-umlisp-query (mrcon (cui lat ts kluilrl) srl lui lui
369 :lrl kluilrl :distinct t)
370 (make-instance 'uterm :cui (ensure-integer cui) :lui lui
371 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
373 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
374 "Return single uterm for cui/lui"
375 (ensure-cui-integer cui)
376 (ensure-lui-integer lui)
377 (collect-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
378 (make-cuilui cui lui)
379 :lrl kluilrl :single t)
380 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
381 :lrl (ensure-integer kluilrl))))
383 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
384 "Return a list of ustr for cui/lui"
385 (ensure-cui-integer cui)
386 (ensure-lui-integer lui)
387 (collect-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
388 (make-cuilui cui lui) :lrl lrl)
389 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
390 :cuisui (make-cuisui cui sui) :stt stt :str str
391 :lrl (ensure-integer lrl))))
393 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
394 "Return the single ustr for cuisui"
395 (ensure-cui-integer cui)
396 (ensure-sui-integer sui)
397 (collect-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
398 (make-cuisui cui sui) :lrl lrl :single t)
399 (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
400 :lui (ensure-integer lui) :stt stt :str str
401 :lrl (ensure-integer lrl))))
403 (defun find-ustr-sui (sui &key (srl *current-srl*))
404 "Return the list of ustr for sui"
405 (ensure-sui-integer sui)
406 (collect-umlisp-query (mrcon (cui lui stt str lrl) srl sui sui
408 (make-instance 'ustr :sui sui :cui cui :stt stt :str str
409 :cuisui (make-cuisui (ensure-integer cui) sui)
410 :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
412 (defun find-ustr-sab (sab &key (srl *current-srl*))
413 "Return the list of ustr for sab"
414 (collect-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
415 (let ((cuisui (ensure-integer kcuisui)))
416 (apply #'find-ustr-cuisui
418 (multiple-value-list (decompose-cuisui cuisui))
421 (defun find-ustr-all (&key (srl *current-srl*))
422 "Return list of all ustr's"
423 (with-sql-connection (db)
426 #'(lambda (cui lui sui stt lrl pfstr)
427 (make-instance 'ustr :cui (ensure-integer cui)
428 :lui (ensure-integer lui) :sui (ensure-integer sui)
430 :cuisui (make-cuisui (ensure-integer cui)
431 (ensure-integer sui))
432 :lrl (ensure-integer lrl)))
433 (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
438 (defun find-string-sui (sui &key (srl *current-srl*))
439 "Return the string associated with sui"
440 (ensure-sui-integer sui)
441 (collect-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
444 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
445 (ensure-sui-integer sui)
446 (ensure-cui-integer cui)
447 (collect-umlisp-query (mrso (sab code srl tty) srl kcuisui
448 (make-cuisui cui sui) :lrl srl)
449 (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
451 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
452 (ensure-cui-integer cui)
453 (ensure-sui-integer sui)
454 (collect-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
455 srl kcuisui (make-cuisui cui sui) :lrl ksrl)
456 (make-instance 'ucxt :sab sab :code code
457 :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
458 :rela rela :xc xc :rnk (ensure-integer rnk)
459 :cui2 (ensure-integer cui2))))
461 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
462 (ensure-cui-integer cui)
463 (ensure-lui-integer lui)
464 (ensure-sui-integer sui)
465 (let ((ls "select CODE,ATN,SAB,ATV from MRSAT where "))
467 (sui (string-append ls "KCUISUI="
468 (integer-string (make-cuisui cui sui) 14)))
469 (lui (string-append ls "KCUILUI="
470 (integer-string (make-cuilui cui lui) 14)
472 (t (string-append ls "cui=" (prefixed-fixnum-string cui nil 7)
473 " and lui=0 and sui=0")))
475 (string-append ls " and KSRL<=" (prefixed-fixnum-string srl nil 3)))
476 (loop for tuple in (mutex-sql-query ls) collect
477 (destructuring-bind (code atn sab atv) tuple
478 (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
480 (defun find-usty-tui (tui)
482 (ensure-tui-integer tui)
483 (collect-umlisp-query (mrsty (sty) nil tui tui :single t)
484 (make-instance 'usty :tui tui :sty sty)))
486 (defun find-usty-sty (sty)
487 "Find usty for a sty"
488 (collect-umlisp-query (mrsty (tui) nil sty sty :single t)
489 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
491 (defun find-usty-all ()
492 "Return list of usty's for all semantic types"
493 (collect-umlisp-query (mrsty (tui) nil nil nil :distinct t)
494 (find-usty-tui tui)))
496 (defun find-usab-all ()
497 "Find usab for a key"
498 (collect-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta
499 rmeta slc scc srl tfr cfr cxty ttyl atnl lat
500 cenc curver sabin) nil nil nil)
501 (make-instance 'usab :vcui (ensure-integer vcui)
502 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
503 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
504 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
505 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
506 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
507 :curver curver :sabin sabin)))
509 (defun find-usab-by-key (key-name key)
510 "Find usab for a key"
511 (collect-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart
512 mend imeta rmeta slc scc srl tfr cfr cxty
513 ttyl atnl lat cenc curver sabin)
514 nil key-name key :single t)
515 (make-instance 'usab :vcui (ensure-integer vcui)
516 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
517 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
518 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
519 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
520 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
521 :curver curver :sabin sabin)))
523 (defun find-usab-rsab (rsab)
525 (find-usab-by-key 'rsab rsab))
527 (defun find-usab-vsab (vsab)
529 (find-usab-by-key 'vsab vsab))
531 (defun find-cui-max ()
532 (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
534 ;;;; Cross table find functions
536 (defun find-ucon-tui (tui &key (srl *current-srl*))
537 "Find list of ucon for tui"
538 (ensure-tui-integer tui)
539 (collect-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
540 (find-ucon-cui (ensure-integer cui) :srl srl)))
542 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
543 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
544 (collect-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
545 :lrl 'klrl :order '(cui asc))
546 (find-ucon-cui cui :srl srl)))
548 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
549 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
550 (collect-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
551 :lrl 'klrl :order '(cui asc))
552 (find-ucon-cui cui :srl srl)))
554 (defun find-ustr-word (word &key (srl *current-srl*))
555 "Return list of ustrs that match word"
556 (collect-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
557 :order (cui asc sui asc))
558 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
560 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
561 "Return list of ustrs that match word"
562 (collect-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
563 :order (cui asc sui asc))
564 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
566 (defun find-uterm-word (word &key (srl *current-srl*))
567 "Return list of uterms that match word"
568 (collect-umlisp-query (mrxw_eng (cui lui) srl wd word :lrl klrl
569 :order (cui asc lui asc))
570 (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
572 (defun find-uterm-normalized-word (word &key (srl *current-srl*))
573 "Return list of uterms that match word"
574 (collect-umlisp-query (mrxnw_eng (cui lui) srl nwd word :lrl klrl
575 :order (cui asc lui asc))
576 (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
578 (defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
579 "Return list of ucons that match non-english word"
580 (collect-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
581 :distinct t :lrl 'klrl :order '(cui asc))
582 (find-ucon-cui cui :srl srl)))
584 (defun find-ustr-noneng-word (word &key (srl *current-srl*))
585 "Return list of ustrs that match non-english word"
586 (collect-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
587 :order (cui asc sui asc))
588 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
592 (defun find-usrl-all ()
593 (collect-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
594 (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
596 ;;; Multiword lookup and score functions
598 (defun find-uobj-multiword (str obj-lookup-fun sort-fun key srl
601 (dolist (word (delimited-string-to-list str #\space))
602 (setq uobjs (append uobjs (funcall obj-lookup-fun word :srl srl))))
604 (funcall sort-fun str
605 (delete-duplicates uobjs :test #'= :key key))))
606 (if (and (plusp (length sorted))
608 (multiword-match str (pfstr (first sorted))))
612 (defun find-ucon-multiword (str &key (srl *current-srl*)
613 (only-exact-if-match t))
614 (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str
615 #'cui srl only-exact-if-match))
617 (defun find-uterm-multiword (str &key (srl *current-srl*)
618 (only-exact-if-match t))
619 (find-uobj-multiword str #'find-uterm-word #'sort-score-pfstr-str
620 #'lui srl only-exact-if-match))
622 (defun find-ustr-multiword (str &key (srl *current-srl*)
623 (only-exact-if-match t))
624 (find-uobj-multiword str #'find-ustr-word #'sort-score-ustr-str
625 #'sui srl only-exact-if-match))
627 (defun sort-score-pfstr-str (str uobjs)
628 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
629 (sort-score-umlsclass-str uobjs str #'pfstr))
631 (defun sort-score-ustr-str (str ustrs)
632 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
633 (sort-score-umlsclass-str ustrs str #'str))
635 (defun sort-score-umlsclass-str (objs str lookup-func)
636 "Sort a list of objects based on scoring to a string"
639 (push (list obj (score-multiword-match str (funcall lookup-func obj)))
641 (mapcar #'car (sort scored #'> :key #'cadr))))
644 ;;; LEX SQL functions
646 (defun find-lexterm-eui (eui)
647 (ensure-eui-integer eui)
648 (collect-umlisp-query (lrwd (wrd) nil eui eui :single t)
649 (make-instance 'lexterm :eui eui :wrd wrd)))
651 (defun find-lexterm-word (wrd)
652 (collect-umlisp-query (lrwd (eui) nil wrd wrd)
653 (make-instance 'lexterm :eui (ensure-integer eui)
654 :wrd (copy-seq wrd))))
656 ;; LEX SQL Read functions
658 (defun find-labr-eui (eui)
659 (ensure-eui-integer eui)
660 (collect-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
661 (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
662 :eui2 (ensure-integer eui2))))
664 (defun find-labr-bas (bas)
665 (collect-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
666 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
667 :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
669 (defun find-lagr-eui (eui)
670 (ensure-eui-integer eui)
671 (collect-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
672 (make-instance 'lagr :eui eui :str str :sca sca :agr agr
675 (defun find-lcmp-eui (eui)
676 (ensure-eui-integer eui)
677 (collect-umlisp-query (lrcmp (bas sca com) nil eui eui)
678 (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
680 (defun find-lmod-eui (eui)
681 (ensure-eui-integer eui)
682 (collect-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
683 (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
685 (defun find-lnom-eui (eui)
686 (ensure-eui-integer eui)
687 (collect-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
688 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
689 :eui2 (ensure-integer eui2))))
691 (defun find-lprn-eui (eui)
692 (ensure-eui-integer eui)
693 (collect-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
694 (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
695 :cas cas :pos pos :qnt qnt :fea fea)))
697 (defun find-lprp-eui (eui)
698 (ensure-eui-integer eui)
699 (collect-umlisp-query (lrprp (bas str sca fea) nil eui eui)
700 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
702 (defun find-lspl-eui (eui)
703 (ensure-eui-integer eui)
704 (collect-umlisp-query (lrspl (spv bas) nil eui eui)
705 (make-instance 'lspl :eui eui :spv spv :bas bas)))
707 (defun find-ltrm-eui (eui)
708 (ensure-eui-integer eui)
709 (collect-umlisp-query (lrtrm (bas gen) nil eui eui)
710 (make-instance 'ltrm :eui eui :bas bas :gen gen)))
712 (defun find-ltyp-eui (eui)
713 (ensure-eui-integer eui)
714 (collect-umlisp-query (lrtyp (bas sca typ) nil eui eui)
715 (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
717 (defun find-lwd-wrd (wrd)
718 (make-instance 'lwd :wrd
719 :euilist (collect-umlisp-query (lrwd (eui) nil wrd wrd)
720 (ensure-integer eui))))
722 ;;; Semantic Network SQL access functions
724 (defun find-sdef-ui (ui)
725 (collect-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
727 (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
728 :def def :ex ex :un un :rh rh :abr abr :rin rin)))
730 (defun find-sstre1-ui (ui)
731 (collect-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
732 (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
733 :ui3 (ensure-integer ui3))))
735 (defun find-sstre1-ui2 (ui2)
736 (collect-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
737 (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
738 :ui3 (ensure-integer ui3))))
740 (defun find-sstr-rl (rl)
741 (collect-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
742 (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
744 (defun find-sstre2-sty (sty)
745 (collect-umlisp-query (srstre2 (rl sty2) nil sty sty)
746 (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
748 (defun find-sstr-styrl (styrl)
749 (collect-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
750 (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))