-
Notifications
You must be signed in to change notification settings - Fork 0
/
BioSEMApplication.java
177 lines (154 loc) · 6.78 KB
/
BioSEMApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package de.julielab.jcore.pipeline.relationextraction;
import java.io.IOException;
import org.apache.commons.cli.*;
import org.apache.uima.UIMAException;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.collection.CollectionReader;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.CollectionReaderFactory;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.InvalidXMLException;
import de.julielab.jcore.consumer.bionlpformat.main.BioEventConsumer;
import de.julielab.jcore.reader.bionlpformat.main.BioEventReader;
public class BioSEMApplication {
static final String PATH_DELIM = System.getProperty("file.separator");
static final String NEWLINE = System.getProperty("line.separator");
static final String WORKDIR = System.getProperty("user.dir");
private static final String INPUT_FOLDER = "inFiles/";
private static final String OUTPUT_FOLDER = "outFiles/";
private static final String AE_09_DESCRIPTOR = "de.julielab.jcore.ae.biosem.desc.jcore-biosem-ae-bionlp-st09";
private static final String AE_11_DESCRIPTOR = "de.julielab.jcore.ae.biosem.desc.jcore-biosem-ae-bionlp-st11";
private static final String AE_13_DESCRIPTOR = "de.julielab.jcore.ae.biosem.desc.jcore-biosem-ae-bionlp-st13";
private static final String TYPES_DESCRIPTOR = "de.julielab.jcore.types.jcore-all-types";
CollectionReader reader;
AnalysisEngine relationExtractor = null;
AnalysisEngine consumer = null;
private static String input_root;
private static String output_root;
private static String model;
public void run() {
initializeComponents(getInput_root(), getOutput_root(), getModel());
processXMIDocuments();
}
public static String getInput_root() {
return input_root;
}
public static String getOutput_root() {
return output_root;
}
public static String getModel() {
return model;
}
private void readCommandLineArgs(String[] args) {
CommandLineParser cmd = new BasicParser();
CommandLine paramParser = null;
HelpFormatter formatter = new HelpFormatter();
Options options = new Options();
options.addOption("i", "input", true, "input folder");
options.addOption("o", "output", true, "output folder");
options.addOption("m", "model", true, "model: {st09, st11, st13}");
options.addOption("h", "help", false, "help");
try {
paramParser = cmd.parse(options, args);
Object param;
/** ----- Help ----- **/
if (paramParser.hasOption("h")) {
formatter.printHelp(this.getClass().getName(), options);
System.exit(0);
}
/** ----- Model ----- **/
param = paramParser.getOptionValue("m");
if (param != null) {
model = (String) param;
if (!(getModel().equalsIgnoreCase("st09") || getModel().equalsIgnoreCase("st11")
|| getModel().equalsIgnoreCase("st13"))) {
System.out.println("Model \"" + getModel() + "\" is not known." + NEWLINE +
"Choose one of 'st09', 'st11' or 'st13' " + "Aborting.");
System.exit(-1);
}
System.out.println("Model: " + getModel());
} else {
model = "st09";
System.out.println("No model specified (-m). Defaulting to: 'st09' model.");
}
/** ----- Input Folder ----- **/
param = paramParser.getOptionValue("i");
if (param != null) {
input_root = (String) param;
System.out.println("Input: " + getInput_root());
} else {
input_root = WORKDIR + PATH_DELIM + INPUT_FOLDER;
System.out.println("No input directory (-i) specified. Defaulting to: '" + input_root +"'");
}
/** ----- Output Folder ----- **/
param = paramParser.getOptionValue("o");
if (param != null) {
output_root = (String) param;
System.out.println("Output: " + getOutput_root());
} else {
output_root = WORKDIR + PATH_DELIM + OUTPUT_FOLDER;
System.out.println("No output directory (-o) specified. Defaulting to: '" + output_root +"'");
}
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(this.getClass().getName(), options);
System.exit(-1);
}
}
private void initializeComponents(String in, String out, String model) {
try {
// init ST Reader
reader = CollectionReaderFactory.createReader(BioEventReader.class,
BioEventReader.DIRECTORY_PARAM, in,
BioEventReader.BIOEVENT_SERVICE_MODE_PARAM, false);
// init BioSEM Event Extractor AE
switch (model) {
case "st09":
relationExtractor = AnalysisEngineFactory.createEngine(AE_09_DESCRIPTOR);
break;
case "st11":
relationExtractor = AnalysisEngineFactory.createEngine(AE_11_DESCRIPTOR);
break;
case "st13":
relationExtractor = AnalysisEngineFactory.createEngine(AE_13_DESCRIPTOR);
break;
default:
break;
}
// init ST Writer
consumer = AnalysisEngineFactory.createEngine(BioEventConsumer.class,
BioEventConsumer.DIRECTORY_PARAM, out,
BioEventConsumer.BIOEVENT_SERVICE_MODE_PARAM, false);
} catch (ResourceInitializationException e) {
e.printStackTrace();
} catch (InvalidXMLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void processXMIDocuments() {
try {
JCas jCas = JCasFactory.createJCas(TYPES_DESCRIPTOR);
while (reader.hasNext()) {
reader.getNext(jCas.getCas());
relationExtractor.process(jCas);
consumer.process(jCas);
jCas.reset();
}
} catch (UIMAException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
BioSEMApplication app = new BioSEMApplication();
app.readCommandLineArgs(args);
app.run();
}
}