onet-summarizer.js
4.74 KB
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
var URL = "http://ws.multiservice.nlp.ipipan.waw.pl/WebService-1.0/ClarinWS";
var COOLDOWN = 5000;
var PROCESSING_CHAIN = createProcessingChain();
function createProcessingChain() {
var tagger = new Array();
tagger["elemName"]="part";
tagger["serviceName"]="Concraft";
var parser = new Array();
parser["elemName"]="part";
parser["serviceName"]="Spejd";
var ner = new Array();
ner["elemName"]="part";
ner["serviceName"]="Nerf";
var md = new Array();
md["elemName"]="part";
md["serviceName"]="MentionDetector";
var coref = new Array();
coref["elemName"]="part";
coref["serviceName"]="Bartek";
var summarizator = new Array();
summarizator["elemName"]="part";
summarizator["serviceName"]="NicolasSummarizer";
var property = new Array();
property["elemName"] = "property";
property["key"] = "length";
property["value"] = 60;
var properties = new Array();
properties["elemName"]="properties";
properties["property"] = property;
summarizator["properties"]= properties;
return new Array(tagger, parser, ner, md, coref, summarizator);
}
$(document).ready(function() {
$("a > span.title").bind("contextmenu",function(e){
summarize($(this));
return false;
});
});
function summarize(link_element) {
if (link_element.find(".loading_icon").length != 0) {
console.log("Already done! Skipping.");
return;
}
var link = link_element.parent().attr("href");
link_element.prepend("<img class=\"loading_icon\" src=\""+ chrome.extension.getURL("/loading_icon.gif")+"\"/>");
link_element.parent().parent().css("overflow","visible");
link_element.parent().css("overflow","visible");
$.get(link, function(link_html) {getSummary(link_html, link_element);}, 'html')
.fail(function() {
setError(link_element);
});
}
function getSummary(html, link_element) {
var title_text = extractText(html);
var title = title_text[0];
var text = title_text[1];
var pl = new SOAPClientParameters();
pl.add("text", text);
pl.add("parts", PROCESSING_CHAIN);
pl.add("inputFormat", "TEXT");
pl.add("outputFormat", "TEXT");
try {
SOAPClient.invoke(URL, "analyzeChain", pl, true, function(response) {analyze_chain_callback(response, link_element, title);});
} catch (err) {
setError(link_element);
}
}
function extractText(html) {
body = '<div id="body-mock">' + html.replace(/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/ig, '') + '</div>';
var $body = $(body);
var title = $body.find("h1").filter(function(index) {return $(this).text().trim().length > 0;}).first().text();
$body.find("a").remove();
var pars = "";
$body.find("p").each(function(index) {
partext = $(this).text().trim();
if (partext.length > 0)
pars = pars+"\n\n"+partext;
});
return [title, pars];
}
function analyze_chain_callback(response, link_element, title) {
check_request_status(response.token, link_element, title);
}
function check_request_status(token, link_element, title) {
var pl = new SOAPClientParameters();
pl.add("token", token);
response = SOAPClient.invoke(URL, "getStatus", pl, true, function(response) {
console.log(response.status);
if (response.status == "DONE") {
retrieve_summary(token, link_element, title);
} else if (response.status == "FAILED") {
setError(link_element);
} else {
setTimeout(function() {check_request_status(token, link_element, title);}, COOLDOWN);
}
});
}
function retrieve_summary(token, link_element, title) {
var pl = new SOAPClientParameters();
pl.add("token", token);
SOAPClient.invoke(URL, "getResult", pl, true, function(response) {show_summary(response, link_element, title);});
}
function show_summary(response, link_element, title) {
var paragraphs = response.result.split("\n\n");
if (paragraphs == "Parsing of input failed") {
setError(link_element);
return;
}
var text = "<p>"+paragraphs.join("</p><p>")+"</p>";
setTooltip(link_element, title, text);
setLoaded(link_element);
}
function setTooltip(link_element, title, text) {
link_element.qtip({
content: {
title: title,
text: text
},
position: {
my: 'top center',
at: 'bottom center',
},
style: {
classes: 'qtip-rounded qtip-blue ui-tooltip-content',
width: "400px"
}
});
}
function setLoaded(link_element) {
link_element.find(".loading_icon").attr("src", chrome.extension.getURL("/loaded_icon.gif"));
}
function setError(link_element) {
link_element.find(".loading_icon").attr("src", chrome.extension.getURL("/error_icon.gif"));
setTooltip(link_element, "Error", "Unable to summarize text.");
}