onet-summarizer.js 4.74 KB
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.");
}