-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
DocxTriplifier.java
223 lines (182 loc) · 9.14 KB
/
DocxTriplifier.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
/*
* Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.sparqlanything.docs;
import com.google.common.collect.Sets;
import io.github.sparqlanything.model.*;
import io.github.sparqlanything.model.annotations.Example;
import io.github.sparqlanything.model.annotations.Option;
import org.apache.jena.graph.NodeFactory;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTMarkupRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
@io.github.sparqlanything.model.annotations.Triplifier
public class DocxTriplifier implements Triplifier {
@Example(resource = "https://sparql-anything.cc/examples/Doc1.docx", description = "Construct the graph by merging multiple consecutive paragraphs into single a single slot.", query = "CONSTRUCT { ?s ?p ?o . } WHERE { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/examples/Doc1.docx,docs.merge-paragraphs=true> { ?s ?p ?o } }")
@Option(description = "It tells the document triplifier to merge all the paragraphs of the document into a single slot (new line characters are preserved)", validValues = "true/false")
public final static IRIArgument MERGE_PARAGRAPHS = new IRIArgument("docs.merge-paragraphs", "false");
@Example(description = "Construct the dataset by using the headers of the columns of the tables to mint the property URIs.", query = "CONSTRUCT { ?s ?p ?o . } WHERE { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/examples/Doc1.docx,docs.table-headers=true> { ?s ?p ?o } }", resource = "https://sparql-anything.cc/examples/Doc1.docx")
@Option(description = "It tells the document triplifier to use the headers of the tables within the document file for minting the properties of the generated triples.", validValues = "true/false")
public final static IRIArgument TABLE_HEADERS = new IRIArgument("docs.table-headers", "false");
private static final Logger logger = LoggerFactory.getLogger(DocxTriplifier.class);
@Override
public void triplify(Properties properties, FacadeXGraphBuilder builder) throws IOException {
URL url = Triplifier.getLocation(properties);
if (url == null) return;
String dataSourceId = SPARQLAnythingConstants.DATA_SOURCE_ID;
String namespace = PropertyUtils.getStringProperty(properties, IRIArgument.NAMESPACE);
boolean mergeParagraphs = PropertyUtils.getBooleanProperty(properties, MERGE_PARAGRAPHS);
boolean headers = PropertyUtils.getBooleanProperty(properties, TABLE_HEADERS);
builder.addRoot(dataSourceId);
try (InputStream is = url.openStream(); XWPFDocument document = new XWPFDocument(is)) {
List<XWPFParagraph> paragraphs = document.getParagraphs();
builder.addType(dataSourceId, SPARQLAnythingConstants.ROOT_ID, "Document");
int count = 1;
if (!mergeParagraphs) count = extractParagraphs(builder, dataSourceId, paragraphs, count);
else count = extractParagraphsAsASingleSlot(builder, dataSourceId, paragraphs, count);
count = extractTables(builder, dataSourceId, namespace, headers, document, count);
extractComments(builder, document, dataSourceId, dataSourceId);
}
}
private static int extractParagraphs(FacadeXGraphBuilder builder, String dataSourceId, List<XWPFParagraph> paragraphs, int count) {
for (XWPFParagraph para : paragraphs) {
logger.trace("Paragraph {} {}", count, para.getText());
String paragraphId;
if (para.getStyle() != null) {
paragraphId = "/".concat(Triplifier.toSafeURIString(para.getStyle())).concat("/").concat(String.valueOf(count));
builder.addType(dataSourceId, paragraphId, Triplifier.toSafeURIString(para.getStyle()));
} else {
paragraphId = "/paragraph/".concat(String.valueOf(count));
builder.addType(dataSourceId, paragraphId, "Paragraph");
}
int paragraphSlots = 1;
builder.addContainer(dataSourceId, SPARQLAnythingConstants.ROOT_ID, count, paragraphId);
builder.addValue(dataSourceId, paragraphId, paragraphSlots, para.getText());
paragraphSlots++;
int commentNumber = 1;
for (CTMarkupRange markup : para.getCTP().getCommentRangeStartList()) {
String commentId = getCommentId(dataSourceId, markup.getId().toString());
// attach comment to the paragraph
builder.addContainer(dataSourceId, paragraphId, paragraphSlots, commentId);
paragraphSlots++;
// add comment number in thread
String commentNumberId = commentId.concat("/ThreadCommentNumber");
builder.addContainer(dataSourceId, commentId, 4, commentNumberId);
builder.addType(dataSourceId, commentNumberId, "ThreadCommentNumber");
builder.addValue(dataSourceId, commentNumberId, 1, commentNumber);
commentNumber++;
}
count++;
}
return count;
}
private static int extractParagraphsAsASingleSlot(FacadeXGraphBuilder builder, String dataSourceId, List<XWPFParagraph> paragraphs, int count) {
StringBuilder sb = new StringBuilder();
for (XWPFParagraph para : paragraphs) {
sb.append(para.getText());
sb.append("\n");
}
builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, count, NodeFactory.createLiteralString(sb.toString()));
count++;
return count;
}
private static int extractTables(FacadeXGraphBuilder builder, String dataSourceId, String namespace, boolean headers, XWPFDocument document, int count) {
for (XWPFTable xwpfTable : document.getTables()) {
String tableId = "Table_".concat(String.valueOf(count));
builder.addContainer(dataSourceId, SPARQLAnythingConstants.ROOT_ID, count, tableId);
LinkedHashMap<Integer, String> headers_map = new LinkedHashMap<>();
int rowNumber = 0;
Iterator<XWPFTableRow> rowIterator = xwpfTable.getRows().iterator();
while (rowIterator.hasNext()) {
// Header
if (headers && rowNumber == 0) {
XWPFTableRow xwpfTableRow = rowIterator.next();
Iterator<XWPFTableCell> cellIterator = xwpfTableRow.getTableCells().iterator();
int columnId = 0;
while (cellIterator.hasNext()) {
columnId++;
XWPFTableCell xwpfTableCell = cellIterator.next();
String columnString = xwpfTableCell.getText();
String columnName = columnString.strip();
int c = 0;
while (headers_map.containsValue(columnName)) {
c++;
columnName += "_".concat(String.valueOf(c));
}
headers_map.put(columnId, columnName);
}
}
// Data
if (rowIterator.hasNext()) {
XWPFTableRow xwpfTableRow = rowIterator.next();
rowNumber++;
String rowId = namespace + "Table_" + count + "_Row_" + rowNumber;
builder.addContainer(dataSourceId, tableId, rowNumber, rowId);
Iterator<XWPFTableCell> cellIterator = xwpfTableRow.getTableCells().iterator();
int columnId = 0;
while (cellIterator.hasNext()) {
columnId++;
XWPFTableCell xwpfTableCell = cellIterator.next();
String value = xwpfTableCell.getText();
if (headers && headers_map.containsKey(columnId)) {
builder.addValue(dataSourceId, rowId, headers_map.get(columnId), value);
} else {
builder.addValue(dataSourceId, rowId, columnId, value);
}
}
}
}
count++;
}
return count;
}
private static void extractComments(FacadeXGraphBuilder builder, XWPFDocument document, String dataSourceId, String documentId) {
for (XWPFComment comment : document.getComments()) {
String commentId = getCommentId(dataSourceId, comment.getId());
builder.addType(dataSourceId, commentId, "Comment");
// add author
String authorCommentId = commentId.concat("/Author");
builder.addType(dataSourceId, authorCommentId, "CommentAuthor");
builder.addContainer(dataSourceId, commentId, 1, authorCommentId);
builder.addValue(dataSourceId, authorCommentId, 1, comment.getAuthor());
// add author
String textCommentId = commentId.concat("/CommentText");
builder.addType(dataSourceId, textCommentId, "CommentText");
builder.addContainer(dataSourceId, commentId, 2, textCommentId);
builder.addValue(dataSourceId, textCommentId, 1, comment.getText());
// add author
String commentIdId = commentId.concat("/CommentId");
builder.addType(dataSourceId, commentIdId, "CommentId");
builder.addContainer(dataSourceId, commentId, 3, commentIdId);
builder.addValue(dataSourceId, commentIdId, 1, comment.getId());
}
}
private static String getCommentId(String dataSourceId, String id) {
return dataSourceId.concat("/Comment_").concat(id);
}
@Override
public Set<String> getMimeTypes() {
return Sets.newHashSet("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
@Override
public Set<String> getExtensions() {
return Sets.newHashSet("docx");
}
}