001 /*
002 * The contents of this file are subject to the Mozilla Public
003 * License Version 1.1 (the "License"); you may not use this file
004 * except in compliance with the License. You may obtain a copy of
005 * the License at http://www.mozilla.org/MPL/
006 *
007 * Software distributed under the License is distributed on an "AS
008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009 * implied. See the License for the specific language governing
010 * rights and limitations under the License.
011 *
012 * The Original Code is Knowtator.
013 *
014 * The Initial Developer of the Original Code is University of Colorado.
015 * Copyright (C) 2005 - 2008. All Rights Reserved.
016 *
017 * Knowtator was developed by the Center for Computational Pharmacology
018 * (http://compbio.uchcs.edu) at the University of Colorado Health
019 * Sciences Center School of Medicine with support from the National
020 * Library of Medicine.
021 *
022 * Current information about Knowtator can be obtained at
023 * http://knowtator.sourceforge.net/
024 *
025 * Contributor(s):
026 * Philip V. Ogren <philip@ogren.info> (Original Author)
027 */
028 package edu.uchsc.ccp.knowtator.util;
029
030 import java.awt.Color;
031 import java.util.ArrayList;
032 import java.util.Collection;
033 import java.util.HashSet;
034 import java.util.Set;
035
036 import edu.stanford.smi.protege.model.Cls;
037 import edu.stanford.smi.protege.model.Instance;
038 import edu.stanford.smi.protege.model.KnowledgeBase;
039 import edu.stanford.smi.protege.model.Project;
040 import edu.stanford.smi.protege.model.SimpleInstance;
041 import edu.stanford.smi.protege.model.Slot;
042 import edu.stanford.smi.protege.util.CollectionUtilities;
043 import edu.uchsc.ccp.knowtator.AnnotationUtil;
044 import edu.uchsc.ccp.knowtator.DisplayColors;
045 import edu.uchsc.ccp.knowtator.InvalidSpanException;
046 import edu.uchsc.ccp.knowtator.KnowtatorManager;
047 import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil;
048 import edu.uchsc.ccp.knowtator.MentionUtil;
049 import edu.uchsc.ccp.knowtator.Span;
050 import edu.uchsc.ccp.knowtator.TextSourceUtil;
051
052 /**
053 * This class simply iterates over annotations and mentions in a Knowtator
054 * project and accesses the slot values of each. The annotations are a flat data
055 * structure so it is sufficient to simply iterate through them. The mentions
056 * are recursively defined so iterating over them is slightly more complicated.
057 *
058 * This code doesn't really do anything. If you were going to export the code to
059 * xml or to UIMA you would want to do something with the data structures as you
060 * accessed them.
061 *
062 * This code does compile but it has not been run or tested. It is, however,
063 * mostly code stolen from MergeAnnotations.java which does run and has been
064 * "tested".
065 */
066
067 public class ExportAnnotations {
068
069 Project project;
070
071 KnowledgeBase kb;
072
073 KnowtatorProjectUtil kpu;
074
075 KnowtatorManager manager;
076
077 AnnotationUtil annotationUtil;
078
079 TextSourceUtil textSourceUtil;
080
081 MentionUtil mentionUtil;
082
083 DisplayColors displayColors;
084
085 java.util.List<Instance> annotations;
086
087 java.util.List<Instance> mentions;
088
089 public ExportAnnotations(Project project) throws Exception {
090 this.project = project;
091 kb = project.getKnowledgeBase();
092 kpu = new KnowtatorProjectUtil(kb);
093 manager = new KnowtatorManager(kpu);
094 annotationUtil = manager.getAnnotationUtil();
095 textSourceUtil = new TextSourceUtil(annotationUtil, kpu);
096 textSourceUtil.init();
097 annotationUtil.setTextSourceUtil(textSourceUtil);
098 mentionUtil = manager.getMentionUtil();
099 displayColors = manager.getDisplayColors();
100
101 annotations = new ArrayList<Instance>(kb.getInstances(kpu.getAnnotationCls()));
102
103 mentions = new ArrayList<Instance>(kb.getInstances(kpu.getMentionCls()));
104
105 System.out.println("annotations.size()= " + annotations.size());
106 System.out.println("mentions.size()= " + mentions.size());
107
108 exportAnnotations();
109 exportMentions();
110
111 }
112
113 public void exportAnnotations() {
114 for (Instance ann : annotations) {
115 SimpleInstance annotation = (SimpleInstance) ann;
116 SimpleInstance mention = annotationUtil.getMention(annotation);
117 System.out.println("mention = " + mention);
118
119 SimpleInstance annotator = annotationUtil.getAnnotator(annotation);
120 String annotatorID = annotator.getName();
121 System.out.println("annotatorID=" + annotatorID);
122
123 String annotatorName = annotator.getBrowserText();
124 System.out.println("annotatorName=" + annotatorName);
125
126 String comment = annotationUtil.getComment(annotation);
127 System.out.println("comment=" + comment);
128
129 // ?? how to get to Date object
130 String dateString = annotationUtil.getCreationDate(annotation);
131 System.out.println("dateString=" + dateString);
132
133 Color annotationColor = displayColors.getColor(annotation);
134 System.out.println("annotationColor=" + annotationColor);
135
136 try {
137 java.util.List<Span> spans = annotationUtil.getSpans(annotation);
138 System.out.println("spans=" + spans);
139 } catch (InvalidSpanException ise) {
140 }
141
142 String spannedText = annotationUtil.getText(annotation);
143 System.out.println("spannedText=" + spannedText);
144
145 SimpleInstance textSourceInstance = annotationUtil.getTextSource(annotation);
146 String textSourceID = textSourceInstance.getName();
147 System.out.println("textSourceID=" + textSourceID);
148
149 Collection<SimpleInstance> annotationSets = (Collection<SimpleInstance>) annotation.getOwnSlotValues(kpu
150 .getSetSlot());
151 for (SimpleInstance setInstance : annotationSets) {
152 String setName = setInstance.getName();
153 System.out.println("setName=" + setName);
154 }
155 }
156 }
157
158 public void exportMentions() {
159 Set<SimpleInstance> exportedMentions = new HashSet<SimpleInstance>();
160 for (Instance mntn : mentions) {
161 SimpleInstance mention = (SimpleInstance) mntn;
162 exportMention(mention, exportedMentions);
163 }
164 }
165
166 private SimpleInstance exportMention(SimpleInstance mention, Set<SimpleInstance> exportedMentions) {
167 if (!exportedMentions.contains(mention)) {
168 if (mentionUtil.isClassMention(mention)) {
169 Cls mentionCls = mentionUtil.getMentionCls(mention);
170 String mentionClsName = "";
171 System.out.println("mentionClsName=" + mentionClsName);
172
173 if (mentionCls != null)
174 mentionClsName = mentionCls.getName();
175 exportedMentions.add(mention);
176
177 Collection<SimpleInstance> slotMentions = (Collection<SimpleInstance>) mention.getOwnSlotValues(kpu
178 .getSlotMentionSlot());
179 for (SimpleInstance slotMention : slotMentions) {
180 exportMention(slotMention, exportedMentions);
181 }
182 } else if (mentionUtil.isInstanceMention(mention)) {
183 SimpleInstance instance = mentionUtil.getMentionInstance(mention);
184 String instanceName = "";
185 System.out.println("instanceName=" + instanceName);
186
187 if (instance != null)
188 instanceName = instance.getName();
189 exportedMentions.add(mention);
190
191 Collection<SimpleInstance> slotMentions = (Collection<SimpleInstance>) mention.getOwnSlotValues(kpu
192 .getSlotMentionSlot());
193 for (SimpleInstance slotMention : slotMentions) {
194 exportMention(slotMention, exportedMentions);
195 }
196 } else if (mentionUtil.isSlotMention(mention)) {
197 Slot slot = mentionUtil.getSlotMentionSlot(mention);
198 String slotName = slot.getName();
199 System.out.println("slotName=" + slotName);
200
201 exportedMentions.add(mention);
202
203 Collection slotValues = mention.getOwnSlotValues(kpu.getMentionSlotValueSlot());
204
205 if (slotValues != null && slotValues.size() > 0) {
206 Object value = CollectionUtilities.getFirstItem(slotValues);
207 if (value instanceof SimpleInstance) {
208 for (Object slotValue : slotValues) {
209 SimpleInstance slotValueInstance = (SimpleInstance) slotValue;
210 exportMention(slotValueInstance, exportedMentions);
211 }
212 } else {
213 // value is some primitive such as int, String, boolean
214 }
215 }
216 }
217
218 Collection<SimpleInstance> mentionAnnotations = (Collection<SimpleInstance>) mention.getOwnSlotValues(kpu
219 .getMentionAnnotationSlot());
220 if (mentionAnnotations != null) {
221 for (SimpleInstance mentionAnnotation : mentionAnnotations) {
222 System.out.println("mentionAnnotation=" + mentionAnnotation);
223
224 // do something
225 }
226 }
227 }
228 return mention;
229 }
230 }