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
029 package edu.uchsc.ccp.knowtator.textsource;
030
031 import java.util.ArrayList;
032 import java.util.Collections;
033 import java.util.List;
034
035 import edu.stanford.smi.protege.model.Cls;
036 import edu.stanford.smi.protege.model.Instance;
037 import edu.stanford.smi.protege.model.KnowledgeBase;
038 import edu.uchsc.ccp.knowtator.Span;
039
040 public class DefaultTextSource implements TextSource {
041
042 public static final String CLS_NAME = "knowtator text source";
043
044 protected String text;
045
046 protected String name;
047
048 protected String protegeClsName;
049
050 /** Creates a new instance of TextSource */
051 public DefaultTextSource() {
052 }
053
054 public DefaultTextSource(String name, String text) {
055 this.text = text;
056 this.name = name;
057 this.protegeClsName = CLS_NAME;
058 }
059
060 public DefaultTextSource(String name, String text, String protegeClsName) {
061 this.text = text;
062 this.name = name;
063 this.protegeClsName = protegeClsName;
064 }
065
066 /**
067 * It might be prudent for extensions of this class to override this method
068 * such that the member variable text is not used - esp. if the texts are
069 * large and there are many of them.
070 *
071 */
072 public String getText() throws TextSourceAccessException {
073 return text;
074 }
075
076 public String getName() {
077 return name;
078 }
079
080 public String getProtegeClsName() {
081 return this.protegeClsName;
082 }
083
084 public Instance createTextSourceInstance(KnowledgeBase knowledgeBase) {
085 Cls textSourceCls = knowledgeBase.getCls(getProtegeClsName());
086
087 Instance textSourceInstance = knowledgeBase.createInstance(getName(), textSourceCls, true);
088 return textSourceInstance;
089 }
090
091 public int hashCode() {
092 return (text + "|" + name).hashCode();
093 }
094
095 public boolean equals(Object object) {
096 if (object instanceof DefaultTextSource) {
097 DefaultTextSource ts = (DefaultTextSource) object;
098 try {
099 if (name.equals(ts.getName()) && text.equals(ts.getText()))
100 return true;
101 } catch (Exception ex) {
102 return false;
103 }
104 }
105
106 return false;
107 }
108
109 public String toString() {
110 return name;
111 }
112
113 public String getText(List<Span> annotationSpans) throws TextSourceAccessException {
114 return getText(annotationSpans, getText());
115 }
116
117 public static String getText(List<Span> annotationSpans, String text) throws TextSourceAccessException {
118 StringBuffer spanText = new StringBuffer();
119
120 List<Span> spans = new ArrayList<Span>(annotationSpans);
121 Collections.sort(spans);
122
123 if (spans.size() > 0) {
124 spanText.append(text.substring(Math.max(0, spans.get(0).getStart()), Math.min(text.length(), spans.get(0)
125 .getEnd())));
126 }
127 if (spans.size() == 1)
128 return spanText.toString();
129
130 for (int i = 1; i < spans.size(); i++) {
131 spanText.append(" ... ");
132 spanText.append(text.substring(Math.max(0, spans.get(i).getStart()), Math.min(text.length(), spans.get(i)
133 .getEnd())));
134 }
135 return spanText.toString();
136 }
137
138 }