-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdbpedia-spotlight-0.2.js
134 lines (118 loc) · 5.05 KB
/
dbpedia-spotlight-0.2.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
/**
* 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 ]
*
* @author pablomendes
*
*/
(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://spotlight.dbpedia.org/rest',
'confidence' : 0.4,
'support' : 20,
'powered_by': 'yes'
};
var methods = {
init : function( options ) {
// If options exist, lets merge them with our default settings
if ( options ) {
$.extend( settings, options );
}
},
annotate: function( options ) {
function update(response) {
console.log($(response));
var content = $(response).find("div"); //the div with the annotated text
if (settings.powered_by == 'yes') {
$(content).append($(powered_by));
};
//var entities = $(content).find("a/[about]");
$(this).html(content);
}
return this.each(function() {
console.log($.quoteString($(this).text()));
var params = {'text': $(this).text(), 'confidence': settings.confidence, 'support': settings.support };
if("types" in settings){
params["types"] = settings.types;
}
$.ajax({ 'url': settings.endpoint+"/annotate",
'data': params,
'context': this,
'headers': {'Accept': 'application/xhtml+xml'},
'success': update
});
});
},
candidates: function( options ) {
function getSelectBox(resources) {
var snippet = "<ul class='candidates'>";
console.log(resources);
var options = ""; $.each(resources, function(i, r) {
options += "<li class='candidate-" + i + "'><a href='" + r["@uri"] + "' about='" + r["@uri"] + "'>" + r["@label"] + "</a>";
//TODO settings.showScores = ["finalScore"] foreach showscores, add k=v
if (settings.showScores == 'yes') options += " <span>(" + parseFloat(r["@finalScore"]).toPrecision(3) +")</span>";
options += "</li>";
});
snippet += options;
snippet += "</ul>"
return snippet;
}
function parseCandidates(response) {
var json = $.parseJSON(response);
var text = json["annotation"]["@text"];
var start = 0;
var annotatedText = json.annotation.surfaceForm.map(function(e) {
var name = e["@name"];
var offset = parseInt(e["@offset"]);
var sfLength = parseInt(name.length);
var snippet = text.substring(start, offset)
var surfaceForm = text.substring(offset,offset+sfLength);
start = offset+sfLength;
snippet += "<div id='"+(name+offset)+"' class='annotation'><a class='surfaceForm'>" + name + "</a>";
//TODO instead of showing directly the select box, it would be cuter to just show a span, and onClick on that span, build the select box.
snippet += getSelectBox($(e.resource));
snippet += "</div>";
return snippet;
}).join("");
//snippet after last surface form
annotatedText += text.substring(start, text.length);
console.log(annotatedText);
return annotatedText;
}
function update(response) {
var content = "<div>" + parseCandidates(response) + "</div>";
if (settings.powered_by == 'yes') {
$(content).append($(powered_by));
};
$(this).html(content);
}
return this.each(function() {
var params = {'text': $(this).text(), 'confidence': settings.confidence, 'support': settings.support };
if("types" in settings && settings["types"] != undefined){
params['types'] = settings.types;
}
$.ajax({ 'url': settings.endpoint+"/candidates",
'data': params,
'context': this,
'headers': {'Accept': 'application/json'},
'success': update
});
});
}
};
$.fn.runDBpediaSpotlight = function(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.spotlight' );
}
};
})( jQuery );