-
Notifications
You must be signed in to change notification settings - Fork 12
/
dbpedia-spotlight-0.3.js
344 lines (299 loc) · 14.4 KB
/
dbpedia-spotlight-0.3.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2011 Pablo Mendes, Max Jakob
*
* 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.
*
* Check our project website for information on how to acknowledge the authors and how to contribute to the project: http://spotlight.dbpedia.org
*/
/**
* If you use this script, please give credit to DBpedia Spotlight by:
* - adding "powered by DBpedia Spotlight" somewhere in the page.
* - add a link to http://spotlight.dbpedia.org.
*
* TODO people can also want to change the boundaries of the spotting
* TODO showScores = [ list of score names to display ]
*
* @author pablomendes
*
*/
// adding this in case we forget to remove some console.log statement that would make it break on firefox without firebug.
if (console===undefined) {
console = { "log": function() {} }
}
//from http://www.wrichards.com/blog/2009/02/jquery-sorting-elements/
jQuery.fn.sort = function() {
return this.pushStack( [].sort.apply( this, arguments ), []);
};
function sortOffset(a,b){
if (a["@offset"] == b["@offset"]){
return 0;
}
return parseInt(a["@offset"]) > parseInt(b["@offset"]) ? 1 : -1;
};
(function( $ ){
var powered_by = "<div style='font-size: 9px; float: right'><a href='http://spotlight.dbpedia.org'>Powered by DBpedia Spotlight</a></div>";
var settings = {
'endpoint' : 'http://localhost:2223/rest',
'confidence' : 0.5, //
'support' : 0,
'powered_by': 'yes', // yes or no
'showScores': 'yes', // yes or no
'types': '',
'policy' : 'whitelist',
'spotter': 'LingPipeSpotter', // one of: LingPipeSpotter,AtLeastOneNounSelector,CoOccurrenceBasedSelector
'disambiguator': 'Default' // one of: LingPipeSpotter,AtLeastOneNounSelector,CoOccurrenceBasedSelector
};
function getScoreDOMElements(data) {
var li = ""
for (var key in data)
li += "<span class='hidden "+ key +"'>" + data[key] +"</span>";
return li;
}
var Parser = {
getSelectBox: function(resources, className) {
var ul = $("<ul class='"+className+"s'></ul>");
$.each(resources, function(i, r) {
var li = "<li class='"+className+" "+className+"-" + i + "'><a href='" + res_prefix + r["@uri"] + "' about='" + r["@uri"] + "'>" + r["@label"] + "</a>";
//TODO settings.showScores = ["finalScore"] foreach showscores, add k=v
if (settings.showScores == 'yes') {
li += " (<span class='finalScoreDisplay'>" + parseFloat(r["@finalScore"]).toPrecision(3) +"</span>)";
}
li += getScoreDOMElements({
"finalScore": parseFloat(r["@finalScore"]).toPrecision(3),
"contextualScore": parseFloat(r["@contextualScore"]),
"percentageOfSecondRank": parseFloat(r["@percentageOfSecondRank"]),
"support": parseFloat(r["@support"]),
"priorScore": parseFloat(r["@priorScore"])
});
li += "</li>";
var opt = $(li);
$(ul).append(opt);
});
return ul;
},
getAnnotatedText: function(response) {
var json;
if (typeof response=='object') {
json = response;
} else {
json = $.parseJSON(response);
}
var text = json["annotation"]["@text"];
var start = 0;
var annotatedText = text;
var annotations = new Array();
if (json.annotation['surfaceForm']!=undefined) {
annotations = annotations.concat(json.annotation.surfaceForm).sort(sortOffset) // deals with the case of only one surfaceFrom returned (ends up not being an array)
} else {
//TODO show a message saying that no annotations were found
}
annotatedText = annotations.map(function(e) {
if (e==undefined) return "";
//console.log(e);
var sfName = e["@name"];
var offset = parseInt(e["@offset"]);
var sfLength = parseInt(sfName.length);
var snippet = text.substring(start, offset)
var surfaceForm = text.substring(offset,offset+sfLength);
start = offset+sfLength;
var classes = "annotation";
snippet += "<div id='"+(sfName+offset)+"' class='" + classes + "'><a class='surfaceForm'>" + sfName + "</a>";
var ul = Parser.getSelectBox($(e.resource),'candidate');
//ul.children().each(function() { console.log($.data($(this),"testProp")); });
snippet += "<ul class='candidates'>"+ul.html()+"</ul>"; //FIXME this wrapper is a repeat from getSelectBox
snippet += "</div>";
return snippet;
}).join("");
//snippet after last surface form
annotatedText += text.substring(start, text.length);
//console.log(annotatedText);
return annotatedText.replace(/\n/g, "<br />\n");
},
getAnnotatedTextFirstBest: function(response) {
var json;
if (typeof response=='object') {
json = response;
} else {
json = $.parseJSON(response);
}
var text = json["@text"];
var start = 0;
var annotatedText = text;
var annotations = new Array();
if (json['Resources']!=undefined) {
annotations = annotations.concat(json['Resources']); // deals with the case of only one surfaceFrom returned (ends up not being an array)
} else {
//TODO show a message saying that no annotations were found
}
annotatedText = annotations.map(function(e) {
if (e==undefined) return "";
var sfName = e["@surfaceForm"];
var offset = parseInt(e["@offset"]);
var uri = e["@URI"];
var sfLength = parseInt(sfName.length);
var snippet = text.substring(start, offset);
var surfaceForm = text.substring(offset,offset+sfLength);
start = offset+sfLength;
var support = parseInt(e["@support"]);
var confidence = 0.5;
if (settings.its == 'yes') {
snippet += "<span id='" + (sfName+offset) +
"' its-ta-ident-ref='" + uri +
"' its-ta-confidence='" + parseFloat(e["@similarityScore"]).toPrecision(3) + "'>" + sfName
snippet += "</span>";
} else {
var classes = "annotation"
snippet += "<a id='"+(sfName+offset)+"' class='" + classes + "' about='" + uri + "' href='" + uri + "' title='" + uri + "'>" + sfName
snippet += getScoreDOMElements({
"finalScore": parseFloat(e["@similarityScore"]).toPrecision(3),
"contextualScore": parseFloat(e["@similarityScore"]).toPrecision(3), //TODO send contextualScore from server and grab here
"percentageOfSecondRank": parseFloat(e["@percentageOfSecondRank"]),
"support": parseFloat(e["@support"])
});
snippet += "</a>";
}
return snippet;
}).join("");
//snippet after last surface form
annotatedText += text.substring(start, text.length);
//console.log(annotatedText);
return annotatedText.replace(/\n/g, "<br />\n");
},
getSuggestions: function(response, targetSurfaceForm) {
var json;
if (typeof response=='object') {
json = response;
} else {
json = $.parseJSON(response);
}
var suggestions = "";
if (json.annotation['surfaceForm']!=undefined) {
var annotations = new Array().concat(json.annotation.surfaceForm) // deals with the case of only one surfaceFrom returned (ends up not being an array)
suggestions = annotations.filter(function(e) {
return (e["@name"]==$.trim(targetSurfaceForm));
}).map(function(e) {
return Parser.getSelectBox($(e.resource),'none');
}).join("");
}
return suggestions;
}
};
var ajaxRequest;
var methods = {
init : function( options ) {
// If options exist, lets merge them with our default settings
if ( options ) {
$.extend( settings, options );
}
},
best: function( options ) {
//init(options);
function update(response) {
var content = "<div>" + Parser.getAnnotatedTextFirstBest(response) + "</div>";
if (settings.powered_by == 'yes') {
$(content).append($(powered_by));
};
$(this).html(content);
if(settings.callback != undefined) {
settings.callback(response);
}
}
return this.each(function() {
//console.log($.quoteString($(this).text()));
var params = {'text': $(this).text(), 'confidence': settings.confidence, 'support': settings.support, 'spotter': settings.spotter, 'disambiguator': settings.disambiguator, 'policy': settings.policy };
if("types" in settings && settings["types"] != undefined)
params["types"] = settings.types;
if("sparql" in settings && settings["sparql"] != undefined)
params["sparql"] = settings.sparql;
ajaxRequest = $.ajax({ 'url': settings.endpoint+"/annotate",
'data': params,
'context': this,
'headers': {'Accept': 'application/json'},
'success': update,
'error': function(response) { if(settings.callback != undefined) { settings.callback(response); } }
});
});
},
candidates: function( options ) {
//init(options);
function update(response) {
var content = "<div>" + Parser.getAnnotatedText(response) + "</div>";
if (settings.powered_by == 'yes') {
$(content).append($(powered_by));
};
$(this).html(content);
if(settings.callback != undefined) {
settings.callback(response);
}
}
return this.each(function() {
var params = {'text': $(this).text(), 'confidence': settings.confidence, 'support': settings.support, 'spotter': settings.spotter, 'disambiguator': settings.disambiguator, 'policy': settings.policy };
if("types" in settings && settings["types"] != undefined)
params['types'] = settings.types;
if("sparql" in settings && settings["sparql"] != undefined)
params["sparql"] = settings.sparql;
ajaxRequest = $.ajax({ 'url': settings.endpoint+"/candidates",
'data': params,
'context': this,
'headers': {'Accept': 'application/json'},
'success': update,
'error': function(response) { if(settings.callback != undefined) { settings.callback(response); } }
});
});
},
suggest: function( options ) {
//init(options);
function update(response) {
var keywords = Kolich.Selector.getSelected();
var suggestion = Parser.getSuggestions(response, keywords);
//console.log('keywords:'+keywords);
//console.log('suggestion:'+suggestion);
var content = "<div style='overflow: auto'>" + suggestion + "</div>";
if (settings.powered_by == 'yes') {
$(content).append($(powered_by));
};
$(this).html(content);
}
return this.each(function() {
return;
//console.log('suggest');
var keywords = $.trim("" + Kolich.Selector.getSelected());
var params = {'text': $(this).text(), 'confidence': settings.confidence, 'support': settings.support, 'spotter': settings.spotter, 'disambiguator': settings.disambiguator, 'surfaceForm': keywords };
if("types" in settings && settings["types"] != undefined){
params['types'] = settings.types;
}
ajaxRequest = $.ajax({ 'url': settings.endpoint+"/candidates",
'data': params,
'context': this,
'headers': {'Accept': 'application/json'},
'success': update,
'error': function(response) { if(settings.callback != undefined) { settings.callback(response); } }
});
});
}
};
$.fn.annotate = function(method) {
//console.log('method:',method);
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.annotate (DBpedia Spotlight)' );
}
};
$.fn.cancelAnnotation = function() {
ajaxRequest.abort();
}
})( jQuery );