001 package gate.util.web;
002
003 import java.io.IOException;
004 import java.net.MalformedURLException;
005 import java.net.URL;
006 import java.util.*;
007
008 import javax.servlet.ServletContext;
009
010 import gate.*;
011 import gate.annotation.AnnotationSetImpl;
012 import gate.corpora.RepositioningInfo;
013 import gate.creole.SerialAnalyserController;
014 import gate.util.GateException;
015
016
017 public class WebCrimeReportAnalyser {
018
019 public static final String SOCIS_CONTROLLER_KEY = "socis.controller";
020 public static final String GATE_INIT_KEY = "gate.init";
021
022
023 public String filePath = "";
024
025 private SerialAnalyserController controller;
026
027 public void initCrimeReportAnalyser() throws GateException {
028 controller = (SerialAnalyserController)
029 Factory.createResource("gate.creole.SerialAnalyserController",
030 Factory.newFeatureMap(),
031 Factory.newFeatureMap(),
032 "Crime Report Analyser");
033
034 ProcessingResource tokeniser = (ProcessingResource)
035 Factory.createResource("gate.creole.tokeniser.DefaultTokeniser",
036 Factory.newFeatureMap());
037
038 controller.add(tokeniser);
039
040 ProcessingResource split = (ProcessingResource)
041 Factory.createResource("gate.creole.splitter.SentenceSplitter",
042 Factory.newFeatureMap());
043
044 controller.add(split);
045
046 ProcessingResource postagger = (ProcessingResource)
047 Factory.createResource("gate.creole.POSTagger",
048 Factory.newFeatureMap());
049
050 controller.add(postagger);
051
052 /* ProcessingResource prechunking = (ProcessingResource)
053 Factory.createResource("chunking.PreChunking",
054 Factory.newFeatureMap());
055
056 controller.add(prechunking);
057 System.out.println("after tokeniser");
058 System.out.println("Freemem: " + Runtime.getRuntime().freeMemory());
059
060 FeatureMap fm = Factory.newFeatureMap();
061 fm.put("inputASName","ChunkAnnotations");
062 fm.put("outputASName","ChunkAnnotations");
063
064 try {
065 URL urlnp = new URL("jar:file:" + filePath + "files.jar!/resources/grammars/Chunk/mainNPChunk.jape");
066 fm.put("grammarURL",urlnp);
067 } catch(MalformedURLException e) {
068
069 e.printStackTrace();
070 }
071
072 ProcessingResource npchunk = (ProcessingResource)
073 Factory.createResource("gate.creole.ANNIETransducer",
074 fm);
075
076 controller.add(npchunk);
077
078 FeatureMap fm1 = Factory.newFeatureMap();
079 fm1.put("inputASName","ChunkAnnotations");
080 fm1.put("outputASName","ChunkAnnotations");
081
082 try {
083 URL urlvp = new URL("jar:file:" + filePath + "files.jar!/resources/grammars/Chunk/mainNPChunk.jape");
084 fm1.put("grammarURL",urlvp);
085 } catch(MalformedURLException e) {
086 e.printStackTrace();
087 }
088
089 ProcessingResource vpchunk = (ProcessingResource)
090 Factory.createResource("gate.creole.ANNIETransducer",
091 fm1);
092
093 controller.add(vpchunk);*/
094
095 FeatureMap fm_gaz = Factory.newFeatureMap();
096 fm_gaz.put("encoding","ISO-8859-1");
097
098 try {
099 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 } // initIndexAnalyser()
127
128 public String process(ServletContext app, String url, String[] annotations)
129 throws GateException, IOException {
130
131 long start;
132
133 // Is this the first time a gate demo has been run? If so,
134 // initiali[s|z]e gate. It's a very heavy process, so only do
135 // it once.
136
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 // Do the deed
145 Gate.init();
146
147 app.setAttribute(GATE_INIT_KEY, "true");
148 }
149
150 // Now do the same for the SOCIS controller
151
152 if (app.getAttribute(SOCIS_CONTROLLER_KEY) == null) {
153
154 CreoleRegister reg = Gate.getCreoleRegister();
155
156 filePath = app.getInitParameter("files.path");
157
158 // URL filesURL = new URL("jar:file:" + filePath + "files.jar!/");
159 // try {
160 // reg.registerDirectories(filesURL);
161 // } catch(GateException e) {
162 // System.out.println(e.getMessage());
163 // }
164
165 initCrimeReportAnalyser();
166
167 app.setAttribute(SOCIS_CONTROLLER_KEY, controller);
168 }
169 else {
170 // The SOCIS demo has already run, so take the existing
171 // controller from the application attribute hash
172
173 controller = (SerialAnalyserController)
174 app.getAttribute(SOCIS_CONTROLLER_KEY);
175 }
176
177 Corpus corpus =
178 (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
179
180 /* here the url specified by the user */
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 /* socis stuff */
206 /*annotTypesRequired.add("Location");
207 annotTypesRequired.add("Time");
208 annotTypesRequired.add("Organization");
209 annotTypesRequired.add("Person");
210 annotTypesRequired.add("Id_No");
211 annotTypesRequired.add("Date");
212 annotTypesRequired.add("Money");
213 annotTypesRequired.add("Percent");
214 annotTypesRequired.add("Conv_make");
215 annotTypesRequired.add("Offence");
216 annotTypesRequired.add("Age");
217 annotTypesRequired.add("Drug");
218 annotTypesRequired.add("Address"); */
219
220 /* required chunks */
221 /*
222 chunkTypesRequired.add("NPCHUNK");
223 chunkTypesRequired.add("VPCHUNK"); */
224
225 AnnotationSet socis = defaultAnnotSet.get(annotTypesRequired);
226
227 //AnnotationSet chunks = chunkAnnotSet.get(chunkTypesRequired);
228
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 // The AnnotationSet socis can be null if no annotations have
241 // been found
242 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 /*it = chunks.iterator();
259 while(it.hasNext()) {
260 currAnnot = (Annotation) it.next();
261 sortedAnnotationsChunks.addSortedExclusive(currAnnot);
262 } //while
263
264 AnnotationSet uniqueChunks = new AnnotationSetImpl((Document) null);
265
266 uniqueChunks.addAll(sortedAnnotationsChunks); */
267
268 String xmlDocumentNamedEntities = doc.toXml(uniqueNamedEntities, true);
269 //String xmlDocumentChunks = doc.toXml(uniqueChunks,true);
270
271 //delete the used resources
272 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 } //if
291
292 } //for
293 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 } //if
303
304 } //for
305
306 int size = size();
307 insertElementAt(annot, size);
308 return true;
309 } //addSortedExclusive
310
311 } //SortedAnnotationList
312
313 }
|