r3463: *** empty log message ***
[umlisp.git] / composite.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          composite.lisp
6 ;;;; Purpose:       Composite Classes for UMLisp
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: composite.lisp,v 1.10 2002/11/23 20:59:22 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 (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
21
22
23 ;;; Semantic type constants
24
25 (defun find-tui-word (words)
26   (kmrcl:aif (car (find-usty-word words))
27        (tui kmrcl::it)
28        nil))
29 (kmrcl:memoize 'find-tui-word)
30
31 (defun tui-disease-or-syndrome ()
32   (find-tui-word "disease or syndrome"))
33 (defun tui-sign-or-symptom () 
34   (find-tui-word "sign or symptom"))
35 (defun tui-finding ()
36   (find-tui-word "finding"))
37
38
39 ;;;; Related concepts with specific tui lookup functions
40
41 (defun ucon-is-tui? (ucon tui)
42   "Returns t if ucon has a semantic type of tui"
43   (find tui (s#sty ucon) :key #'tui))
44
45 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
46   "Returns a list of related ucons that have specific tui"
47   (remove-duplicates 
48    (filter
49     #'(lambda (c) 
50         (kmrcl:aif (funcall cui2-func c)
51              (let ((ucon2 (find-ucon-cui kmrcl::it)))
52                (when (ucon-is-tui? ucon2 tui)
53                  ucon2))
54              nil))
55     (funcall related-con-func ucon))
56    :key #'cui))
57
58 (defun find-ucon2-coc-tui (ucon tui)
59   "Return list of ucon's that have co-occuring concepts of semantic type tui"
60   (find-ucon2-tui ucon tui #'cui2 #'s#coc))
61   
62 (defun find-ucon2-rel-tui (ucon tui)
63   "Return list of ucon's that have related concepts to ucon and semantic type tui"
64   (find-ucon2-tui ucon tui #'cui2 #'s#rel))
65
66 ;;; Composite Objects
67
68 (defclass ucon_freq (ucon)
69   ((freq :type integer :initarg :freq :accessor freq :print-formatter ho:comma-integer))
70   (:metaclass hyperobject-class)
71   (:default-initargs :freq 0)
72   (:title "Concept and Count")
73   (:print-slots cui freq pfstr)
74   (:ref-fields (cui find-ucon-cui))
75   (:documentation "Composite object of ucon/freq"))
76
77 (defclass ustr_freq (ustr)
78   ((freq :type fixnum :initarg :freq :accessor freq :print-formatter ho:comma-integer))
79   (:metaclass hyperobject-class)
80   (:default-initargs :freq 0)
81   (:title "String and Count")
82   (:print-slots sui freq stt lrl str)
83   (:documentation "Composite object of ustr/freq"))
84
85 (defclass usty_freq (usty)
86   ((freq :type fixnum :initarg :freq :accessor freq))
87   (:metaclass hyperobject-class)
88   (:default-initargs :freq 0)
89   (:title "Semantic Type and Count")
90   (:print-slots tui freq sty))
91   (:documentation "Composite object of usty/freq"))
92
93 (defun find-usty_freq-all ()
94   (let ((usty_freqs '()))
95     (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
96       (let* ((tui (car tuple))
97              (freq (ensure-integer 
98                      (caar (mutex-sql-query 
99                             (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
100         (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
101     (sort usty_freqs #'> :key #'freq)))
102
103
104 (defclass usrl_freq (usrl)
105   ((freq :type fixnum :initarg :freq :accessor freq :print-formatter ho:comma-integer))
106   (:metaclass hyperobject-class)
107   (:default-initargs :freq 0)
108   (:title "Source and Count")
109   (:print-slots sab freq srl)
110   (:documentation "Composite object of usrl/freq"))
111
112 ;; Frequency finding functions
113
114 (defun find-usrl_freq-all ()
115   (let ((freqs '()))
116     (dolist (usrl (find-usrl-all))
117       (let ((freq (ensure-integer 
118                    (caar (mutex-sql-query 
119                           (format nil "select count(*) from MRSO where SAB='~a'" 
120                                   (sab usrl)))))))
121         (push (make-instance 'usrl_freq :usrl usrl :freq freq) freqs)))
122     (sort freqs #'> :key #'usrl_freq-freq)))
123
124 (defun find-ucon2_freq-coc-tui (ucon tui)
125 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui" 
126   (let ((ucon_freqs '())) 
127     (dolist (ucoc (s#coc ucon)) 
128       (kmrcl:aif (cui2 ucoc) 
129            (let ((ucon2 (find-ucon-cui kmrcl::it))) 
130              (when (ucon-is-tui? ucon2 tui)
131                (push (make-instance 'ucon_freq :ucon ucon2 :freq (cof ucoc)) 
132                      ucon_freqs)))))
133     (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
134     (sort ucon_freqs #'> :key #'freq)))
135  
136 (defun find-ucon2-str&sty (str sty lookup-func)
137   "Call lookup-func for ucon and usty for given str and sty"
138   (let ((ucon (car (find-ucon-str str)))
139         (usty (car (find-usty-word sty))))
140     (if (and ucon usty)
141         (funcall lookup-func ucon (tui usty))
142       nil)))
143   
144 (defun find-ucon2-coc-str&sty (str sty)
145   "Find all ucons that are a co-occuring concept for concept named str
146    and that have semantic type of sty"
147   (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
148
149 (defun find-ucon2-rel-str&sty (str sty)
150   "Find all ucons that are a relationship to concept named str
151    and that have semantic type of sty"
152   (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
153
154 ;;; Most common relationships, co-occurances
155
156 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
157   "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
158   (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
159     (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
160       (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
161         (kmrcl:aif (aref ucon_freqs (cui ucon2))
162              (setf (freq kmrcl::it) (1+ (freq kmrcl::it)))
163              (setf (aref ucon_freqs (cui ucon2)) 
164                (make-instance 'ucon_freq :ucon ucon2 :freq 1)))))
165     (let ((ucon_freq-list '()))
166       (dotimes (i (find-cui-max))
167         (declare (fixnum i))
168         (kmrcl:awhen (aref ucon_freqs i)
169              (push kmrcl::it ucon_freq-list)))
170       (sort ucon_freq-list #'> :key #'freq))))
171
172 (defun find-ucon2_freq-rel-tui-all (tui)
173   "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
174   (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
175
176 (defun find-ucon2_freq-coc-tui-all (tui)
177   (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
178
179 #+(or cmu scl)
180 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
181   (let ((cl #+cmu (pcl:find-class c)
182             #+scl (find-class c)))
183     #+cmu (pcl:finalize-inheritance cl)
184     #+scl (clos:finalize-inheritance cl)))