1 package gate.util.web;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.*;
7
8 import javax.servlet.ServletContext;
9
10 import gate.*;
11 import gate.annotation.AnnotationSetImpl;
12 import gate.corpora.RepositioningInfo;
13 import gate.creole.SerialAnalyserController;
14 import gate.util.GateException;
15
16
17 public class WebCrimeReportAnalyser {
18
19 public static final String SOCIS_CONTROLLER_KEY = "socis.controller";
20 public static final String GATE_INIT_KEY = "gate.init";
21
22
23 public String filePath = "";
24
25 private SerialAnalyserController controller;
26
27 public void initCrimeReportAnalyser() throws GateException {
28 controller = (SerialAnalyserController)
29 Factory.createResource("gate.creole.SerialAnalyserController",
30 Factory.newFeatureMap(),
31 Factory.newFeatureMap(),
32 "Crime Report Analyser");
33
34 ProcessingResource tokeniser = (ProcessingResource)
35 Factory.createResource("gate.creole.tokeniser.DefaultTokeniser",
36 Factory.newFeatureMap());
37
38 controller.add(tokeniser);
39
40 ProcessingResource split = (ProcessingResource)
41 Factory.createResource("gate.creole.splitter.SentenceSplitter",
42 Factory.newFeatureMap());
43
44 controller.add(split);
45
46 ProcessingResource postagger = (ProcessingResource)
47 Factory.createResource("gate.creole.POSTagger",
48 Factory.newFeatureMap());
49
50 controller.add(postagger);
51
52
94
95 FeatureMap fm_gaz = Factory.newFeatureMap();
96 fm_gaz.put("encoding","ISO-8859-1");
97
98 try {
99 URL urlgaz = new URL("jar:file:" + filePath + "files.jar!/resources/gazetters/general/lists.def");
100 fm_gaz.put("listsURL",urlgaz);
101 } catch(MalformedURLException e) {
102 e.printStackTrace();
103 }
104
105 ProcessingResource gazetteer = (ProcessingResource)
106 Factory.createResource("gate.creole.gazetteer.DefaultGazetteer",
107 fm_gaz);
108
109 controller.add(gazetteer);
110
111 FeatureMap fm_gra = Factory.newFeatureMap();
112
113 try {
114 URL urlgra = new URL("jar:file:" + filePath + "files.jar!/resources/grammars/NamedEntities/socismain.jape");
115 fm_gra.put("grammarURL",urlgra);
116 } catch(MalformedURLException e) {
117 e.printStackTrace();
118 }
119
120 ProcessingResource grammar = (ProcessingResource)
121 Factory.createResource("gate.creole.ANNIETransducer",
122 fm_gra);
123
124 controller.add(grammar);
125
126 }
128 public String process(ServletContext app, String url, String[] annotations)
129 throws GateException, IOException {
130
131 long start;
132
133
137 if (app.getAttribute(GATE_INIT_KEY) == null) {
138 Gate.setLocalWebServer(false);
139 Gate.setNetConnected(false);
140
141 System.setProperty("java.protocol.handler.pkgs",
142 "gate.util.protocols");
143
144 Gate.init();
146
147 app.setAttribute(GATE_INIT_KEY, "true");
148 }
149
150
152 if (app.getAttribute(SOCIS_CONTROLLER_KEY) == null) {
153
154 CreoleRegister reg = Gate.getCreoleRegister();
155
156 filePath = app.getInitParameter("files.path");
157
158
165 initCrimeReportAnalyser();
166
167 app.setAttribute(SOCIS_CONTROLLER_KEY, controller);
168 }
169 else {
170
173 controller = (SerialAnalyserController)
174 app.getAttribute(SOCIS_CONTROLLER_KEY);
175 }
176
177 Corpus corpus =
178 (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
179
180
181 URL textURL = new URL(url);
182
183 FeatureMap params = Factory.newFeatureMap();
184 params.put("sourceUrl", textURL);
185 params.put("preserveOriginalContent", new Boolean(true));
186 params.put("collectRepositioningInfo", new Boolean(true));
187
188 Document doc = (Document)
189 Factory.createResource("gate.corpora.DocumentImpl",params);
190
191 corpus.add(doc);
192
193 controller.setCorpus(corpus);
194 controller.execute();
195
196 AnnotationSet defaultAnnotSet = doc.getAnnotations();
197 AnnotationSet chunkAnnotSet = doc.getAnnotations("ChunkAnnotations");
198 Set annotTypesRequired = new HashSet();
199 Set chunkTypesRequired = new HashSet();
200
201 for (int i=0;i<annotations.length;i++) {
202 annotTypesRequired.add(annotations[i]);
203 }
204
205
206
219
220
221
224
225 AnnotationSet socis = defaultAnnotSet.get(annotTypesRequired);
226
227
229 FeatureMap features = doc.getFeatures();
230 String originalContent = (String)
231 features.get(GateConstants.ORIGINAL_DOCUMENT_CONTENT_FEATURE_NAME);
232
233 RepositioningInfo info = (RepositioningInfo)
234 features.get(GateConstants.DOCUMENT_REPOSITIONING_INFO_FEATURE_NAME);
235
236 Annotation currAnnot;
237 SortedAnnotationList sortedAnnotationsNamedEntities =
238 new SortedAnnotationList();
239
240 if (socis != null) {
243 Iterator it = socis.iterator();
244 while(it.hasNext()) {
245 currAnnot = (Annotation) it.next();
246 sortedAnnotationsNamedEntities.addSortedExclusive(currAnnot);
247 }
248 }
249
250 AnnotationSet uniqueNamedEntities =
251 new AnnotationSetImpl(doc);
252
253 uniqueNamedEntities.addAll(sortedAnnotationsNamedEntities);
254
255 SortedAnnotationList sortedAnnotationsChunks =
256 new SortedAnnotationList();
257
258
267
268 String xmlDocumentNamedEntities = doc.toXml(uniqueNamedEntities, true);
269
271 Factory.deleteResource(doc);
273 Factory.deleteResource(corpus);
274 return xmlDocumentNamedEntities;
275
276 }
277
278 public static class SortedAnnotationList extends Vector {
279
280 public SortedAnnotationList() {
281 super();
282 }
283 public boolean addSortedExclusive(Annotation annot) {
284 Annotation currAnnot = null;
285 for(int i=0; i<size() ; ++i) {
286 currAnnot = (Annotation) get(i);
287 if(annot.overlaps(currAnnot)) {
288 return false;
289
290 }
292 } long annotStart = annot.getStartNode().getOffset().longValue();
294 long currStart;
295 for (int i=0; i < size(); ++i) {
296 currAnnot = (Annotation) get(i);
297 currStart = currAnnot.getStartNode().getOffset().longValue();
298 if(annotStart < currStart) {
299 insertElementAt(annot, i);
300 return true;
301
302 }
304 }
306 int size = size();
307 insertElementAt(annot, size);
308 return true;
309 }
311 }
313 }
314
315