-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiohubNLP.java
More file actions
297 lines (266 loc) · 9.71 KB
/
BiohubNLP.java
File metadata and controls
297 lines (266 loc) · 9.71 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Calendar;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import edu.stanford.nlp.time.SUTime.PartialTime;
import edu.stanford.nlp.time.SUTime.Time.*;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.AnnotationPipeline;
import edu.stanford.nlp.pipeline.TokenizerAnnotator;
import edu.stanford.nlp.time.SUTime;
import edu.stanford.nlp.time.SUTime.Temporal;
import edu.stanford.nlp.time.TimeAnnotations;
import edu.stanford.nlp.time.TimeAnnotations.TimexAnnotations;
import edu.stanford.nlp.time.TimeAnnotator;
import edu.stanford.nlp.time.TimeExpression;
import edu.stanford.nlp.util.CoreMap;
import nu.xom.jaxen.function.SubstringAfterFunction;
import edu.stanford.nlp.ling.CoreAnnotations.NormalizedNamedEntityTagAnnotation;
import edu.stanford.nlp.optimization.QNMinimizer.eScaling;
import edu.stanford.nlp.time.SUTime.IsoDate;
public class BiohubNLP {
static AnnotationPipeline pipeline = null;
public static void setup() {
try {
String defs_sutime = "/root/NLP/stanford-corenlp-full-2018-02-27/sutime/defs.sutime.txt";
String holiday_sutime = "/root/NLP/stanford-corenlp-full-2018-02-27/sutime/english.holidays.sutime.txt";
String _sutime = "/root/NLP/stanford-corenlp-full-2018-02-27/sutime/english.sutime.txt";
pipeline = new AnnotationPipeline();
Properties properties = new Properties();
String sutimeRules = defs_sutime + "," + holiday_sutime + "," + _sutime;
properties.setProperty("sutime.rules", sutimeRules);
properties.setProperty("sutimes.binders", "0");
properties.setProperty("sutime.markTimeRanges", "true");
properties.setProperty("sutime.includeRange", "true");
pipeline.addAnnotator(new TokenizerAnnotator(false));
pipeline.addAnnotator(new TimeAnnotator("sutime", properties));
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
public static String[] annotateText(String text, String referenceDate) {
try {
if (referenceDate == null || referenceDate.isEmpty()) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
referenceDate = dateFormat.format(new Date());
}
else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
dateFormat.parse(referenceDate);
}catch (Exception e) {
referenceDate = dateFormat.format(new Date());
// TODO: handle exception
}
}
if (pipeline!=null)
{
Annotation annotation = new Annotation(text);
annotation.set(CoreAnnotations.DocDateAnnotation.class, referenceDate);
pipeline.annotate(annotation);
List<CoreMap>timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);
for (CoreMap cm:timexAnnsAll)
{
try
{
List<CoreLabel>tokens = cm.get(CoreAnnotations.TokensAnnotation.class);
String beginOffset = tokens.get(0).get(CoreAnnotations.CharacterOffsetBeginAnnotation.class).toString();
String endOffset = tokens.get(tokens.size()-1).get(CoreAnnotations.CharacterOffsetEndAnnotation.class).toString();
Temporal temporal = cm.get(TimeExpression.Annotation.class).getTemporal();
/*System.out.println("Token text:" + cm.toString());
System.out.println("Temporal Value:" + temporal.toString());
System.out.println("Timex:" + temporal.getTimexValue());
System.out.println("Timex type:" + temporal.getTimexType().name());
System.out.println("begin offset:" + beginOffset);
System.out.println("End Offset:" + endOffset);*/
String type = temporal.getTimexType().name();
String value = temporal.toString();
//System.out.println(temporal.getRange().begin());
//System.out.println(temporal.getRange().end());
//System.out.println("!");
String[] timeRange = null;
if (type == "DATE" || type == "DURATION")
{
timeRange = NormalizeDurationAndDate(temporal, cm.toString());
}
else if (type == "TIME")
{
timeRange = NormalizeTime(value, cm.toString());
}
return timeRange;
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
else {
//System.out.println("Annotation Pipeline object is NULL.");
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return null;
}
public static String[] NormalizeDurationAndDate(Temporal temporal, String tokenText) //默认beginTime,endTime 格式一样。
{
String[] returnTime = new String[3];
String beginTime = temporal.getRange().begin().toString();
String endTime = temporal.getRange().end().toString();
Pattern pattern1 = Pattern.compile("\\d{4}-[A-Z]+-[A-Z]+");
Matcher matcher1 = pattern1.matcher(beginTime);
Matcher matcher2 = pattern1.matcher(endTime);
while(matcher1.find())
{
beginTime = matcher1.group().substring(0, 4);
while(matcher2.find())
{
//System.out.println(matcher2.group());
endTime = matcher2.group().substring(0, 4);
}
}
if (beginTime.length() <= 4)
{
beginTime = beginTime + "-01-01T00:00:00";
endTime = endTime + "-12-31T23:59:59";
}
else if (beginTime.length() <= 7)
{
beginTime = beginTime + "-01T00:00:00";
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.YEAR, Integer.parseInt(endTime.substring(0,4)));
calendar.set(calendar.MONTH, Integer.parseInt(endTime.substring(5,7))-1);
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(calendar.getTime());
endTime = endTime + "-" + lastDayOfMonth.substring(8, 10) + "T23:59:59";
}
else if (beginTime.length() <= 10)
{
beginTime = beginTime + "T00:00:00";
endTime = endTime + "T23:59:59";
}
else if (beginTime.length() <= 13)
{
beginTime = beginTime + ":00:00";
endTime = endTime + ":00:00";
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
beginTime = beginTime.replace("T", " ");
endTime = endTime.replace("T", " ");
try {
Date beginDate = dateFormat.parse(beginTime);
Date endDate = dateFormat.parse(endTime);
//System.out.println(beginDate.getTime());
//System.out.println(endDate.getTime());
Long longBeginDate = beginDate.getTime();
Long longEndDate = endDate.getTime();
returnTime[0] = longBeginDate.toString();
returnTime[1] = longEndDate.toString();
returnTime[2] = tokenText;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(beginTime);
//System.out.println(endTime);
return returnTime;
}
public static String[] NormalizeTime(String value, String tokenText) {
String[] returnTime = new String[3];
Pattern pattern1 = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
Pattern pattern2 = Pattern.compile("T[a-zA-Z0-9:]+");
Matcher matcher1 = pattern1.matcher(value);
Matcher matcher2 = pattern2.matcher(value);
String beginTime = null;
String endTime = null;
while (matcher1.find() && matcher2.find())
{
if (matcher2.group().compareTo("TMO") == 0)
{
String tempTime = matcher1.group();
beginTime = matcher1.group() + "T08:00:00";
endTime = matcher1.group() + "T11:59:59";
//System.out.println(tempTime);
}
else if (matcher2.group().compareTo("TAF") == 0)
{
String tempTime = matcher1.group();
beginTime = matcher1.group() + "T13:00:00";
endTime = matcher1.group() + "T18:59:59";
//System.out.println(tempTime);
}
else if (matcher2.group().compareTo("TEV") == 0)
{
String tempTime = matcher1.group();
beginTime = matcher1.group() + "T19:00:00";
endTime = matcher1.group() + "T23:59:59";
//System.out.println(tempTime);
}
else
{
String tempTime = matcher1.group();
if (matcher2.group().length() >= 6)
{
beginTime = matcher1.group() + matcher2.group().substring(0, 6) + ":00";
endTime = matcher1.group() + matcher2.group().substring(0, 6) + ":59";
}
else
{
beginTime = matcher1.group() + matcher2.group();
endTime = matcher1.group() + matcher2.group();
}
//System.out.println(tempTime);
}
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
beginTime = beginTime.replaceAll("T", " ");
endTime = endTime.replaceAll("T", " ");
Date beginDate = dateFormat.parse(beginTime);
Date endDate = dateFormat.parse(endTime);
//System.out.println(beginTime);
//System.out.println(endTime);
Long longBeginDate = beginDate.getTime();
Long longEndDate = endDate.getTime();
returnTime[0] = longBeginDate.toString();
returnTime[1] = longEndDate.toString();
returnTime[2] = tokenText;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnTime;
}
public static String[] Parse(String string) {
setup();
String currentTime = SUTime.getCurrentTime().toString();
String formatTime = currentTime.substring(0, 10);
String[] message = null;
message = annotateText(string,formatTime);
return message;
}
public static void main(String[] args)
{
for(String string:args)
{
Parse(string);
}
}
}