-
Notifications
You must be signed in to change notification settings - Fork 4
/
SUTimeModuleNew.java
288 lines (219 loc) · 11.6 KB
/
SUTimeModuleNew.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
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
package cz.cvut.spipes.modules;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.sutime.AnnforModel;
import cz.cvut.spipes.sutime.DescriptorModel;
import cz.cvut.spipes.util.JenaUtils;
import cz.cvut.spipes.util.QueryUtils;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.time.TimeAnnotations;
import edu.stanford.nlp.time.TimeAnnotator;
import edu.stanford.nlp.time.TimeExpression;
import edu.stanford.nlp.util.CoreMap;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Construct;
import org.topbraid.spin.vocabulary.SP;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class SUTimeModuleNew extends AbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(SUTimeModuleNew.class);
private static final String TYPE_URI = KBSS_MODULE.getURI() + "temporal-v1";
private static final String TYPE_PREFIX = TYPE_URI + "/";
private static final int DEFAULT_PAGE_SIZE = 10000;
private static final String LIMIT_OFFSET_CLAUSE_MARKER_NAME = "LIMIT_OFFSET";
private static final Property P_PAGE_SIZE = ResourceFactory.createProperty(TYPE_PREFIX + "page-size");
private Integer pageSize = DEFAULT_PAGE_SIZE;
private List<Resource> constructQueries;
//sml:replace
private boolean isReplace;
//kbss:parseText
/**
* Whether the query should be taken from sp:text property instead of from SPIN serialization
*/
private boolean parseText;
private List<Path> ruleFilePaths = new LinkedList<>();
private String documentDate; // TODO support other formats ?
private AnnotationPipeline pipeline;
public SUTimeModuleNew() {
pipeline = loadPipeline();
}
@Override
public String getTypeURI() {
return TYPE_URI;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
@Override
public void loadConfiguration() {
if (this.resource.getProperty(DescriptorModel.has_document_date) != null) { // TODO set current date if not specified
documentDate = getEffectiveValue(DescriptorModel.has_document_date).asLiteral().toString();
}
if (this.resource.getProperty(DescriptorModel.has_rule_file) != null) { //TODO support more rule files
ruleFilePaths.add(Paths.get(getEffectiveValue(DescriptorModel.has_rule_file).asLiteral().toString()));
}
parseText = this.getPropertyValue(KBSS_MODULE.is_parse_text, true);
pageSize = this.getPropertyValue(P_PAGE_SIZE, DEFAULT_PAGE_SIZE);
constructQueries = getResourcesByProperty(SML.constructQuery);
isReplace = this.getPropertyValue(SML.replace, false);
}
@Override
public ExecutionContext executeSelf() {
Model defaultModel = executionContext.getDefaultModel();
QuerySolution bindings = executionContext.getVariablesBinding().asQuerySolution();
int count = 0;
Model inferredModel = ModelFactory.createDefaultModel();
Model previousInferredModel = ModelFactory.createDefaultModel();
boolean queriedModelIsEmpty = false;
while (!queriedModelIsEmpty) {
count++;
Model inferredInSingleIterationModel = ModelFactory.createDefaultModel();
queriedModelIsEmpty = true;
for (Resource constructQueryRes : constructQueries) {
Construct spinConstructRes = constructQueryRes.as(Construct.class);
Query query;
if (parseText) {
String queryStr = spinConstructRes.getProperty(SP.text).getLiteral().getString();
query = QueryFactory.create(substituteQueryMarkers(count, queryStr));
} else {
query = ARQFactory.get().createQuery(spinConstructRes);
}
Model queriedModel = QueryUtils.execConstruct(
query,
defaultModel,
bindings
);
if (queriedModel.size() > 0) {
queriedModelIsEmpty = false;
}
Model constructedModel = analyzeModel(queriedModel);
inferredInSingleIterationModel = JenaUtils.createUnion(inferredInSingleIterationModel, constructedModel);
}
previousInferredModel = inferredModel;
inferredModel = JenaUtils.createUnion(inferredModel, inferredInSingleIterationModel);
}
return createOutputContext(isReplace, inferredModel);
}
private Model analyzeModel(Model m) {
LOG.debug("Extracting temporal information from model of size {}", m.size());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
List<ReifiedStatement> temporalAnnotationStmts = new LinkedList<>();
m.listStatements()
.filterDrop(st -> !st.getObject().isLiteral())
.forEachRemaining(
st -> {
String objectStr = st.getObject().asLiteral().getLexicalForm();
try {
ArrayList<AnnforModel> singleStDates = temporalAnalysis(pipeline, objectStr);
if (!singleStDates.isEmpty()) {
Model mm = ModelFactory.createDefaultModel();
ReifiedStatement reifiedSt = mm.createReifiedStatement(st);
for (AnnforModel s : singleStDates) {
Literal beginLiteral = mm.createTypedLiteral(sdf.format(s.getDateBegin().getTime()));
Literal endLiteral = mm.createTypedLiteral(sdf.format(s.getDateEnd().getTime()));
reifiedSt.addProperty(RDF.type, DescriptorModel.sutime_extraction);
reifiedSt.addProperty(DescriptorModel.extracted, s.getDateExtracted());
reifiedSt.addProperty(DescriptorModel.beginDate, beginLiteral);
reifiedSt.addProperty(DescriptorModel.endDate, endLiteral);
reifiedSt.addProperty(DescriptorModel.type, s.getDateType());
temporalAnnotationStmts.add(reifiedSt);
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
Model outputModel = ModelFactory.createDefaultModel();
temporalAnnotationStmts.forEach(
st -> outputModel.add(st.listProperties())
);
return outputModel;
}
private ArrayList<AnnforModel> temporalAnalysis(AnnotationPipeline pipeline, String s) throws IOException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Annotation annotation = new Annotation(s);
annotation.set(CoreAnnotations.DocDateAnnotation.class, sdf.format(cal.getTime()));
pipeline.annotate(annotation);
ArrayList<AnnforModel> afmArr = new ArrayList<>();
List<CoreMap> timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);
if (!timexAnnsAll.isEmpty()) {
for (CoreMap cm : timexAnnsAll) {
List<CoreLabel> tokens = cm.get(CoreAnnotations.TokensAnnotation.class);
edu.stanford.nlp.time.SUTime.Temporal temporal = cm.get(TimeExpression.Annotation.class).getTemporal();
if (!temporal.getTimexType().toString().equals("DURATION") && !temporal.getTimexType().toString().equals("SET")) {
String extractionDateforModel = cm.toString();
edu.stanford.nlp.time.SUTime.Time suTimeBegin;
edu.stanford.nlp.time.SUTime.Time suTimeEnd;
String typeDateforModel = temporal.getTimexType().toString();
Calendar beginDateforModel = Calendar.getInstance();
Calendar endDateforModel = Calendar.getInstance();
Date date;
AnnforModel afm = null;
try {
if(((temporal.getRange().beginTime().getTimexValue()) != null) || ((temporal.getRange().endTime().getTimexValue()) != null)) {
suTimeBegin = temporal.getRange().beginTime();
suTimeEnd = temporal.getRange().endTime();
if ((!suTimeBegin.toString().equals("PRESENT_REF")) && (!suTimeBegin.toString().contains("X")) && (!suTimeBegin.toString().contains("UNKNOWN")) && (!suTimeBegin.toString().contains("REF")) && (!suTimeBegin.toString().contains("x")) && (suTimeBegin != null)) {
if (sdf.parse(suTimeBegin.toString()) != null) {
date = sdf.parse(suTimeBegin.toString());
beginDateforModel.setTime(date);
}
}
if ((!suTimeEnd.toString().equals("PRESENT_REF")) && (!suTimeEnd.toString().contains("X")) && (!suTimeEnd.toString().contains("UNKNOWN")) && (!suTimeEnd.toString().contains("REF")) && (!suTimeEnd.toString().contains("x")) && (suTimeEnd != null)) {
if (sdf.parse(suTimeEnd.toString()) != null) {
date = sdf.parse(suTimeEnd.toString());
endDateforModel.setTime(date);
}
}
}
afm = new AnnforModel(beginDateforModel, endDateforModel, typeDateforModel, extractionDateforModel);
} catch (NullPointerException e) {
LOG.info("catched in temporalAnalyze " + e.getMessage());
} catch (ParseException e) {
LOG.info("catched in parse exception " + e.getMessage());
}
afmArr = new ArrayList<>();
afmArr.add(afm);
}
}
}
return afmArr;
}
private AnnotationPipeline loadPipeline() {
Properties props = new Properties();
props.setProperty("sutime.includeRange", "true");
props.setProperty("sutime.rules", "sutime/defs.txt, sutime/defs.sutime.txt, sutime/english.holidays.sutime.txt, sutime/english.sutime.txt");
AnnotationPipeline pipeline = new AnnotationPipeline();
pipeline.addAnnotator(new TokenizerAnnotator(false));
pipeline.addAnnotator(new WordsToSentencesAnnotator(false));
pipeline.addAnnotator(new POSTaggerAnnotator(false));
pipeline.addAnnotator(new TimeAnnotator("sutime", props));
return pipeline;
}
private String substituteQueryMarkers(int currentIteration, String queryStr) {
int offset = pageSize * (currentIteration - 1);
LOG.debug("Creating query with LIMIT {} for OFFSET {}.", pageSize, offset);
String markerValue = "\n" + "OFFSET " + offset
+ "\n" + "LIMIT " + pageSize + "\n";
return QueryUtils
.substituteMarkers(LIMIT_OFFSET_CLAUSE_MARKER_NAME, markerValue, queryStr);
}
}