-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-pundit.js
executable file
·241 lines (202 loc) · 8.67 KB
/
get-pundit.js
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
var annotationServerApi = "http://as.thepund.it:8080/annotationserver/api/";
var decodedSearch = decodeURI(window.location.search);
var sources = decodedSearch.split("source={")[decodedSearch.split("source={").length -1].split("}")[0].split(",");
var notebookIds = decodedSearch.split("nbs={")[decodedSearch.split("nbs={").length -1].split("}")[0].split(",");
var nbs, source;
if (nbs === "undefined") {
nbs='c4a2729c';
}
if (source === "undefined") {
source='pundit';
}
var numberOfAnnotationsInCurrentNotebook = 0;
var annotationsReadFromNotebook = 0;
if (sources.indexOf("freebase") == -1) {
// clear all the relations between philosophers...
for(p in phils.ph) {
phils.ph[p].to = {};
phils.ph[p].fr = {};
phils.ph[p].to_count = 0;
phils.ph[p].fr_count = 0;
}
phils.fr_max = 0;
phils.to_max = 0;
}
if (sources.indexOf("pundit") !== -1) {
//get annotations from notebooks
for (i in notebookIds) {
getNotebookAnnotations(notebookIds[i]);
}
}
function addNotebook(id) {
var newSearch = window.location.search.replace("nbs={","nbs={" + id + ",");
var newLocation = location.origin + location.pathname + newSearch + location.hash;
location.href = newLocation;
}
function showFromFreebaseAndPundit() {
var pieces = decodeURI(window.location.search).split("source=");
pieces[1] = "source={freebase,pundit}";
var newLocation = location.origin + location.pathname + pieces.join("") + location.hash;
location.href = newLocation;
}
function showFromPunditOnly() {
var pieces = decodeURI(window.location.search).split("source=");
pieces[1] = "source={pundit}";
var newLocation = location.origin + location.pathname + pieces.join("") + location.hash;
location.href = newLocation;
}
function handleNotebookAnnotationsMetadata(annotations) {
var cont = 0;
for (ann in annotations) {
cont ++;
}
numberOfAnnotationsInCurrentNotebook = cont;
for (url in annotations) {
getAnnotationGraph(annotations[url], annotations[url]["http://purl.org/pundit/ont/ao#id"][0].value);
}
}
function handleAnnotationMetadataAndGraphAndItems(metadata, graph, items) {
var annotator = metadata["http://purl.org/dc/elements/1.1/creator"][0].value;
var annotationId = metadata["http://purl.org/pundit/ont/ao#id"][0].value;
var annotatedPage = "";
var annotatedTargetPage = "";
var citedId = "";
var citingId = "";
var citedSentence = "";
var citingSentence = "";
var predicate = ""
// for all the subjects of triples in the annotation graph ...
for(subj in graph) {
var screator;
// scan all the predicates used in combination with the given subject to find the dcterms:creator, which is the author of the annotated sentence
for (pred in graph[subj]) {
if (pred == "http://purl.org/dc/terms/creator") {
screator = graph[subj]["http://purl.org/dc/terms/creator"][0].value;
}
}
if (typeof(screator) === 'undefined') {
continue;
}
for (pred in graph[subj]) {
if (pred.indexOf("http://purl.org/spar/cito/") !== -1) {
predicate = items[pred]["http://www.w3.org/2000/01/rdf-schema#label"][0].value;
// if the subject of the triple is a sentence (a text-fragment)...
if (items[subj]["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"][0].value == "http://purl.org/pundit/ont/ao#text-fragment"
|| items[subj]["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"][0].value == "http://purl.org/pundit/ont/ao#fragment-text") {
// get the ID of the author of the sentence...
citingId = "/" + screator.split("/")[screator.split("/").length - 1].replace(".","/");
// get the text of the sentence ...
citingSentence = items[subj]["http://purl.org/dc/elements/1.1/description"][0].value;
// get the page that was annotated ...
annotatedPage = items[subj]["http://purl.org/pundit/ont/ao#hasPageContext"][0].value;
}
// Get the object of the triple ...
obj = graph[subj][pred][0].value;
// if the object is a sentence...
if (items[obj]["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"][0].value == "http://purl.org/pundit/ont/ao#text-fragment"
|| items[obj]["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"][0].value == "http://purl.org/pundit/ont/ao#fragment-text") {
// get the text of the sentence ...
citedSentence = items[obj]["http://purl.org/dc/elements/1.1/description"][0].value;
// get the page where the sentence belongs to ...
annotatedTargetPage = items[obj]["http://purl.org/pundit/ont/ao#hasPageContext"][0].value;
// get the author of the sentence ...
var ocreators = graph[obj]["http://purl.org/dc/terms/creator"];
// if the author is not specified, doscard the annotation ...
if (typeof(ocreators) === 'undefined') {
continue;
}
citedUrl = ocreators[0].value;
// get the id of the author
citedId = "/" + citedUrl.split("/")[citedUrl.split("/").length - 1].replace(".","/");
// if the object is not a sentence, it should be a philosopher ...
} else {
citedUrl = obj;
citedId = "/" + citedUrl.split("/")[citedUrl.split("/").length - 1].replace(".","/");
}
}
}
}
self.updateEdgemapsData(annotationId, annotator, annotatedPage, annotatedTargetPage, citedId, citingId, citedSentence, citingSentence, predicate);
annotationsReadFromNotebook ++;
if (annotationsReadFromNotebook == numberOfAnnotationsInCurrentNotebook) {
init();
annotationsReadFromNotebook = 0;
}
}
function updateEdgemapsData(annotationId, annotator, annotatedPage, annotatedTargetPage, citedId, citingId, citedSentence, citingSentence, predicate) {
//Annotations are taken into consideration only if cited and citing philosophers are already present in the JSON data
if (phils.ph[citedId] == undefined || phils.ph[citingId] == undefined) {
return;
}
if (phils.ph[citedId].to[citingId] == undefined) {
phils.ph[citedId].to[citingId] = 1;
phils.ph[citedId].to_count ++;
phils.to_max ++;
}
if (phils.ph[citingId].fr[citedId] == undefined) {
phils.ph[citingId].fr[citedId] = 1;
phils.ph[citingId].fr_count ++;
phils.fr_max ++;
}
if (phils.ph[citedId].annotations == undefined) {
phils.ph[citedId].annotations = {};
}
if (phils.ph[citedId].annotations[citingId] == undefined) {
phils.ph[citedId].annotations[citingId] = {};
}
phils.ph[citedId].annotations[citingId][annotationId] = {};
phils.ph[citedId].annotations[citingId][annotationId].annotator = annotator;
phils.ph[citedId].annotations[citingId][annotationId].annotated_sentence = citingSentence;
phils.ph[citedId].annotations[citingId][annotationId].page = annotatedPage;
phils.ph[citedId].annotations[citingId][annotationId].rel = predicate;
if (annotatedTargetPage !== undefined && annotatedTargetPage !== "") {
phils.ph[citedId].annotations[citingId][annotationId].target_page = annotatedTargetPage;
phils.ph[citedId].annotations[citingId][annotationId].annotated_target_sentence = citedSentence;
}
};
function handleAnnotationGraph(annotationId, annotationMetadata, graph) {
var self = this;
self.getAnnotationItems(annotationId, annotationMetadata, graph);
}
function getAnnotationGraph(annotationMetadata, annotationId) {
var self = this,
args = {
url: annotationServerApi + "open/annotations/" + annotationId + "/graph",
headers : {"Accept": "application/json"},
handleAs: "json",
load: function(r) {
self.handleAnnotationGraph(annotationId, annotationMetadata, r);
},
error: function(error) {
}
};
dojo.xhrGet(args);
}
function getNotebookAnnotations(notebookId) {
var self = this,
args = {
url: annotationServerApi + "open/notebooks/" + notebookId + "/annotations/metadata",
headers : {"Accept": "application/json"},
handleAs: "json",
load: function(r) {
self.handleNotebookAnnotationsMetadata(r);
},
error: function(error) {
}
};
dojo.xhrGet(args);
}
function getAnnotationItems(annotationId, annotationMetadata, graph) {
var self = this,
args = {
url: annotationServerApi + "open/annotations/" + annotationId + "/items",
headers : {"Accept": "application/json"},
handleAs: "json",
load: function(r) {
self.handleAnnotationMetadataAndGraphAndItems(annotationMetadata, graph,r);
},
error: function(error) {
}
};
dojo.xhrGet(args);
}