001 /*
002 * Pattern.java
003 *
004 * Niraj Aswani, 19/March/07
005 *
006 * $Id: Pattern.html,v 1.0 2007/03/19 16:22:01 niraj Exp $
007 */
008 package gate.creole.annic;
009
010 import java.util.ArrayList;
011 import java.util.List;
012 import java.util.Map;
013
014 /**
015 * Pattern is an extension of the HIT class. This provides information
016 * about the underlying annotations as well as the information about its
017 * left and right context.
018 *
019 * @author niraj
020 *
021 */
022 public class Pattern extends Hit {
023
024 /**
025 * serial version id
026 */
027 private static final long serialVersionUID = 3258126955659604530L;
028
029 /**
030 * Left context start offset
031 */
032 protected int leftContextStartOffset;
033
034 /**
035 * right context end offset
036 */
037 protected int rightContextEndOffset;
038
039 /**
040 * pattern text
041 */
042 protected String patternText;
043
044 /**
045 * annotations
046 */
047 protected List<PatternAnnotation> annotations;
048
049 /**
050 * Constructor
051 * @param docID
052 * @param patternText
053 * @param startOffset
054 * @param endOffset
055 * @param leftContextStartOffset
056 * @param rightContextEndOffset
057 * @param annotations
058 * @param queryString
059 */
060 public Pattern(String docID, String annotationSetName, String patternText, int startOffset,
061 int endOffset, int leftContextStartOffset, int rightContextEndOffset,
062 List<PatternAnnotation> annotations, String queryString) {
063 super(docID, annotationSetName, startOffset, endOffset, queryString);
064 this.patternText = patternText;
065 this.leftContextStartOffset = leftContextStartOffset;
066 this.rightContextEndOffset = rightContextEndOffset;
067 this.annotations = annotations;
068 }
069
070 /**
071 * Returns the annotations lying between the start and the end offsets
072 *
073 * @param start
074 * @param end
075 * @return
076 */
077 public List<PatternAnnotation> getPatternAnnotations(int startOffset, int endOffset) {
078 ArrayList<PatternAnnotation> annots = new ArrayList<PatternAnnotation>();
079 for(int i = 0; i < annotations.size(); i++) {
080 PatternAnnotation ga1 = (PatternAnnotation)annotations.get(i);
081 if(ga1.getStartOffset() >= startOffset && ga1.getEndOffset() <= endOffset) {
082 annots.add(ga1);
083 }
084 }
085 return annots;
086 }
087
088
089 /**
090 * Returns all annotations underlying the pattern
091 *
092 * @return
093 */
094 public PatternAnnotation[] getPatternAnnotations() {
095 return annotations.toArray(new PatternAnnotation[0]);
096 }
097
098 /**
099 * Returns the annotations with the given type
100 *
101 * @param type
102 * @return
103 */
104 public PatternAnnotation[] getPatternAnnotations(String type) {
105 List<PatternAnnotation> annots = new ArrayList<PatternAnnotation>();
106 for(int i = 0; i < annotations.size(); i++) {
107 PatternAnnotation ga1 = annotations.get(i);
108 if(ga1.getType().equals(type)) {
109 annots.add(ga1);
110 }
111 }
112 return annots.toArray(new PatternAnnotation[0]);
113 }
114
115 /**
116 * Returns the annotations with the given type and the feature
117 *
118 * @param type
119 * @param feature
120 * @return
121 */
122 public PatternAnnotation[] getPatternAnnotations(String type, String feature) {
123 List<PatternAnnotation> annots = new ArrayList<PatternAnnotation>();
124 for(int i = 0; i < annotations.size(); i++) {
125 PatternAnnotation ga1 = annotations.get(i);
126 if(ga1.getType().equals(type)) {
127 // check if this has the following feature
128 Map<String, String> features = ga1.getFeatures();
129 if(features != null && features.keySet().contains(feature)) {
130 annots.add(ga1);
131 }
132 }
133 }
134 return annots.toArray(new PatternAnnotation[0]);
135 }
136
137 /**
138 * Returns the text of the pattern
139 *
140 * @return
141 */
142 public String getPatternText() {
143 return patternText;
144 }
145
146 /**
147 * Returns the text of the pattern between the given offsets
148 *
149 * @param startOffset
150 * @param endOffset
151 * @return
152 */
153 public String getPatternText(int startOffset, int endOffset) {
154 return patternText.substring(startOffset - leftContextStartOffset,
155 endOffset - leftContextStartOffset);
156 }
157
158 /**
159 * Returns the start offset of the left context
160 *
161 * @return
162 */
163 public int getLeftContextStartOffset() {
164 return leftContextStartOffset;
165 }
166
167 /**
168 * Returns the end offset of the right context
169 *
170 * @return
171 */
172 public int getRightContextEndOffset() {
173 return rightContextEndOffset;
174 }
175 }
|