Integrated automatic language recognition algorithm.

Some other minor change.

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@164886 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Erik Perrone 2018-03-09 13:40:21 +00:00
parent 7f660e7377
commit 07fb54148b
10 changed files with 947 additions and 866 deletions

View File

@ -36,7 +36,7 @@ import org.gcube.nlphub.nlp.NlpNerRunner;
public class NLPHub extends HttpServlet {
private Logger logger = Logger.getLogger(NLPHub.class.getSimpleName());
private static final long serialVersionUID = 1L;
private String service = "http://dataminer-prototypes.d4science.org/wps/";
public static final String service = "http://dataminer-prototypes.d4science.org/wps/";
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private boolean devMode = true;
@ -77,8 +77,6 @@ public class NLPHub extends HttpServlet {
if (algs.length >= 1) {
// single algorithm execution
System.out.println("algs[0]: " + algs[0]);
NlpNerRunner runner = new NlpNerRunner(service, algs, token, response);
runner.run(request.getParameter("plink"), request.getParameter("annotations"),
request.getParameter("lang"));

View File

@ -25,6 +25,8 @@ import org.apache.log4j.Logger;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.JsonManager;
import org.gcube.nlphub.legacy.NlpHubException;
import org.gcube.nlphub.nlp.NLpLanguageRecognizer;
import org.gcube.nlphub.nlp.NlpUtils;
import org.gcube.nlphub.workspace.WorkspaceManager;
/**
@ -141,7 +143,12 @@ public class NLPUploader extends HttpServlet {
}
String link = ws.getPublicLink(fileName, token);
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
String sentence = NlpUtils.getLanguageRecognizerDigest(stringContent);
System.out.println(sentence);
NLpLanguageRecognizer recognizer = new NLpLanguageRecognizer(NLPHub.service, token, sentence, link, response);
recognizer.run();
//writer.println(new JsonManager().getSuccessJsonResponse("" + link));
} catch (Exception x) {
x.printStackTrace();
logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage());

View File

@ -27,12 +27,4 @@ public class Constants {
}
return new String(hexChars);
}
/*
public static void main(String[] args) {
String text = "I am here in\nPisa.";
System.out.println(text);
System.out.println(hexDump(text.getBytes()));
text = text.replaceAll("\n", "\\\\n");
System.out.println(hexDump(text.getBytes()));
}*/
}

View File

@ -51,6 +51,7 @@ public class JsonManager {
return jsonObjectRoot.toString();
}
public String getSuccessJsonResponse(String message) {
return getResponse(true, message);
}
@ -58,6 +59,10 @@ public class JsonManager {
public String getErrorJsonResponse(String message) {
return getResponse(false, message);
}
public String getSuccessJsonResponse(String language, String message) {
return getResponse(true, language, message);
}
private String getResponse(boolean success, String message) {
jsonObjectRoot.addProperty(RESPONSE, (success ? OK : ERROR));
@ -65,6 +70,13 @@ public class JsonManager {
return jsonObjectRoot.toString();
}
private String getResponse(boolean success, String language, String message) {
jsonObjectRoot.addProperty(RESPONSE, (success ? OK : ERROR));
jsonObjectRoot.addProperty(LANGUAGE, language);
jsonObjectRoot.addProperty(MESSAGE, message);
return jsonObjectRoot.toString();
}
/*
public void buildNerOutputJson(NerOutput nerOut) {
// build the root json object initial property fields

View File

@ -1,8 +1,14 @@
package org.gcube.nlphub.nlp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.dataminermanagercl.server.dmservice.SClient;
import org.gcube.data.analysis.dataminermanagercl.shared.data.OutputData;
@ -15,14 +21,17 @@ import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ListParamete
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ParameterType;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.DataminerClient;
import org.gcube.nlphub.legacy.JsonManager;
import org.gcube.nlphub.legacy.NlpHubException;
public class NLpLanguageRecognizer extends DataminerClient {
private HttpServletResponse response;
private Logger logger = Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName());
private String sentence;
private String sentence, publicLink;
public final static String RECOGNIZER_ID = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.LANGUAGE_RECOGNIZER";
// private String service = "http://dataminer-prototypes.d4science.org/wps/";
// private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
@ -30,8 +39,15 @@ public class NLpLanguageRecognizer extends DataminerClient {
public NLpLanguageRecognizer(String service, String token, String sentence) {
super(service, "", token);
this.sentence = sentence;
response = null;
}
public NLpLanguageRecognizer(String service, String token, String sentence, String publicLink, HttpServletResponse response) {
super(service, "", token);
this.sentence = sentence;
this.response = response;
this.publicLink = publicLink;
}
public void run() throws NlpHubException {
try {
@ -61,13 +77,14 @@ public class NLpLanguageRecognizer extends DataminerClient {
Resource r = mapResource.getMap().get(key);
if (r.isFile()) {
FileResource f = (FileResource) r;
String mimeType = f.getMimeType();
if (mimeType.equalsIgnoreCase("application/d4science")) {
String link = f.getUrl();
System.out.println("url: " + link);
String op = computationId.getOperatorId();
op = op.substring(op.lastIndexOf(".") + 1);
//testEndOfProcess(op + ":::" + link);
String name = f.getName();
String link = f.getUrl();
if(name.equalsIgnoreCase("outfile")) {
String content = readFileContent(link);
System.out.println(content + ".");
if(response != null) {
response.getWriter().println(new JsonManager().getSuccessJsonResponse(content, publicLink));
}
}
}
}
@ -78,23 +95,41 @@ public class NLpLanguageRecognizer extends DataminerClient {
}
}
public static void main(String[] args) {
// String service = "http://dataminer-prototypes.d4science.org/wps/";
// String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
// String sentence = "Per me si va nella città dolente";
//
// NLpLanguageRecognizer recognizer = new NLpLanguageRecognizer(service, token, sentence);
// try {
// recognizer.run();
// } catch (Exception x) {
// x.printStackTrace();
// }
/*
String test = "Anch'io ho voglia di dare il mio contributo\n alla causa";
String regularized = test.replaceAll("[\\s]+", " ");
System.out.println("Before: " + test + "\n" + "After: " + regularized);
regularized = test.replaceAll("[\\n]+", " ");
System.out.println("After: " + regularized);
*/
private String readFileContent(String link) throws Exception {
URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty(Constants.TOKEN_PARAMETER, super.getToken());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = r.readLine()) != null) {
response.append(inputLine);
}
String out = response.toString();
return out;
}
/*
public static void main(String[] args) {
String service = "http://dataminer-prototypes.d4science.org/wps/";
String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
String sentence = "Per me si va nella città dolente";
sentence = "Querido amigo, te escribo, así que me distraigo un poco.";
sentence = "Per me si va in città";
NLpLanguageRecognizer recognizer = new NLpLanguageRecognizer(service, token, sentence);
try {
recognizer.run();
} catch (Exception x) {
x.printStackTrace();
}
}*/
}

View File

@ -80,7 +80,6 @@ public class NlpNerRunner extends DataminerClient {
for (String key : mapResource.getMap().keySet()) {
Resource r = mapResource.getMap().get(key);
if (r.isFile()) {
FileResource f = (FileResource) r;
String mimeType = f.getMimeType();
if (mimeType.equalsIgnoreCase("application/d4science")) {
@ -104,9 +103,11 @@ public class NlpNerRunner extends DataminerClient {
for (String id : identifiers) {
try {
super.identifier = id;
System.out.println("Running: " + id);
super.init();
List<Parameter> parameters = mapParameters(filePublicLink, annotations);
super.execute(parameters);
System.out.println("Runned: " + id);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
@ -123,7 +124,7 @@ public class NlpNerRunner extends DataminerClient {
if (outputLinks.size() == identifiers.length) {
String[] links = new String[outputLinks.size()];
links = outputLinks.toArray(links);
System.out.println("testEndOfProcess");
//System.out.println("testEndOfProcess");
writeResponse(links);
}
}

View File

@ -0,0 +1,53 @@
package org.gcube.nlphub.nlp;
import java.util.ArrayList;
public class NlpUtils {
public static String getLanguageRecognizerDigest(String content) {
int minToken = 10;
content = content.trim();
String[] tokens = content.split("\\.");
if(tokens.length == 1)
tokens = content.split(";");
if(tokens.length == 1)
return content;
ArrayList<String> list = new ArrayList<>();
for(int i=0; i<tokens.length; i++) {
int n = countTokens(tokens[i]);
if(n >= minToken) {
list.add(tokens[i]);
}
}
if(list.isEmpty())
return content;
String digest = list.get(0);
for(String s : list) {
if(s.length() < digest.length())
digest = s;
}
return digest;
}
public static int countTokens(String content) {
return content.split("\\s").length;
}
/*
public static void main(String[] args) {
String text = "Per me si va nella Città dolente.\n Per me si va tra la perduta Gente";
text = "North Korea has agreed to send a delegation to next month's Winter Olympics in South Korea, the first notable breakthrough to come out of a face-to-face meeting Tuesday between the neighboring nations.";
text += "In talks, held at the border village of Panmunjom or \"truce village,\" in the Korean peninsula\'s heavily fortified demilitarized zone, North Korea negotiators agreed to send a \"high-level delegation\" comprising athletes, a cheering squad, an art troupe, a visitors\' group, a Taekwondo demonstration team and a press corps, South Korea\'s Unification Ministry told reporters in Seoul.";
text += "Unification Vice Minister Chun Hae-sung also announced that both sides plan to re-open a military hotline on the western Korean Peninsula.";
text += "The hotline was one of many that were closed as inter-Korean relations soured.";
System.out.println(getLanguageRecognizerDigest(text));
}
*/
}

View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Colors</title>
<script type="text/javascript">
var hexLetters = '0123456789ABCDEF';
enhanceColor = function(color) {
var c = eval("0x" + color);
var hi = Math.round(c / 16);
if (hi < 15) {
hi += Math.round((15 - hi) / 1.5);
}
if (hi > 15)
hi = 15;
return '' + hexLetters[hi] + hexLetters[c % 16];
}
randomRGB = function() {
var color = '';
var couple = '';
for (var i = 0; i < 3; i++) {
//do {
couple = '' + hexLetters[Math.floor(Math.random() * 10)]
+ hexLetters[Math.floor(Math.random() * 16)];
// for (j = 0; j < 2; j++) {
// couple += letters[Math.floor(Math.random() * 16)];
// }
//} while ((255 - parseInt("0x" + couple)) < 128)
color += couple;
}
return color;
}
changeColor = function() {
var color = randomRGB();
var complemetar = 0xFFFFFF - eval("0x" + color.substring(1));
var complement = complemetar.toString(16);
var R = enhanceColor(complement.substring(0, 2));
var G = enhanceColor(complement.substring(2, 4));
var B = enhanceColor(complement.substring(4));
complement = "#" + R + G + B;
var span = document.getElementsByTagName("SPAN");
span[0].setAttribute("style", "color:#" + color + "; font-weight:bold;");
span[1].setAttribute("style", "color:#" + color + "; background:"
+ complement + "; font-weight:bold;");
}
</script>
</head>
<body onload="changeColor()">
<div>
<span>TEST COLORE</span>
</div>
<div>
<span>TEST COLORE + SFONDO</span>
</div>
</body>
</html>

View File

@ -17,692 +17,8 @@
<script type="text/javascript" src="js/materialize.min.js"></script>
<script type="text/javascript" src="js/jquery.simple.websocket.min.js"></script>
<script type="text/javascript" src="js/jquery.uploadfile.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/merge.js"></script>
<!-- <script type="text/javascript" src="js/main.js"></script> -->
<!-- scripting code -->
<script type="text/javascript">
var tableRawSize = 5;
var annotations = [];
var algorithms = [];
var languages = [];
var publicLink = null;
var pageGreen = "#4CAF50";
var pageGray = "#A0A0A0";
var savedAnnotations = "";
var jsonContent = null;
var named = null;
var resultText = "";
// ------------------------------------------------------------------------------------------------------
// Starting point: get the csv file with algorithm parameters and set the page with valid algs info.
// ------------------------------------------------------------------------------------------------------
$
.ajax({
url : "https://data.d4science.org/bnp4UDNyb21lSURkQXdDUnlSS0JkVWgzWk1KMUxWVGZHbWJQNStIS0N6Yz0",
type : "GET",
async : true,
contentType : "text/plain; charset=utf-8",
success : function(data, stato) {
$("#ner-result-container").hide();
var lineTokens = manageCsvData(data);
getAlgorithms(lineTokens);
buildLanguageSelection();
buildLanguageList();
buildAnnotations();
resizeTable();
resizeLogo();
$("#back-ner-ui-button").click(function() {
$("#ner-result-container").hide();
$("#ner-ui").show();
$("#input-textarea").val("");
publicLink = null;
jsonContent = null;
});
$("#execute-button").css("background-color", pageGray);
$("#upload-button")
.uploadFile(
{
url : "/nlphub/nlphub-uploader-servlet",
fileName : "mytxtfile",
maxFileCount : 100,
multiple : false,
maxFileSize : 1024 * 1000 * 6.14,
showFileCounter : false,
showCancel : true,
dragDropStr : "<img class='img-prompt' src='img/upload.png' width='60px' height='60px' style='display:block; margin:auto; padding: 10px'>",
extErrorStr : "Error. Text file only",
sizeErrorStr : "Error. Max size: 6 Mb",
onLoad : function(obj) {
$("#file-info").remove();
$("#reset-upload").remove();
$("#fu-container")
.append(
"<div id=\"right-child\" class=\"float-right-div\"></div>");
$("#right-child")
.append(
"<div id=\"file-info\" class=\"centered\"></div>");
$("#right-child")
.append(
"<div id=\"reset-upload\" class=\"centered\">Cancel</div>");
$("#reset-upload").hide();
$("#reset-upload")
.click(
function() {
resetExecuteButton();
$("#file-info")
.empty();
$(
"#reset-upload")
.css(
"display",
"none");
textAreaEnable(true);
});
savedAnnotations = "";
setEventListeners();
},
onSelect : function(files) {
showProgress();
textAreaEnable(false);
$("#input-textarea").val("");
},
onSuccess : function(files, data, xhr) {
hideProgress();
$("#reset-upload").css("display",
"inline");
console.log("files: " + files);
$("#file-info").empty();
$("#file-info")
.append(
"Uploaded: <span style=\"color:#4CAF50; letter-spacing: 1px; font-weight: bold;\">"
+ files
+ "</span>");
publicLink = data.message;
$("#execute-button").css(
"background-color",
pageGreen);
if (publicLink == null) {
alert("Error uploading file.");
}
},
onError : function(files, status,
errMsg, pd) {
hideProgress();
textAreaEnable(true);
alert(errMsg);
}
});
},
error : function(richiesta, stato, errori) {
alert("Unexpected Error. Status: " + richiesta.status);
}
});
manageCsvData = function(data) {
// first of all: split on the "newline" character in order to
// get all lines.
var lines = data.split("\n");
var tokens = [], lineTokens = [];
// second: parse each line
for (i in lines) {
var line = ',' + lines[i];
var tokenStart = -1;
for (var j = 0; j < line.length; j++) {
if (line.charAt(j) == '\"') {
j = line.indexOf('\"', j + 1);
if (j < 0)
return;
continue;
}
if (line.charAt(j) == ',') {
if (tokenStart == -1) {
tokenStart = j;
} else {
tokens[tokens.length] = line.substring(tokenStart + 1,
j);
tokenStart = j;
}
}
}
// get the new token and push it in the array
tokens[tokens.length] = line.substring(tokenStart + 1);
lineTokens[lineTokens.length] = tokens;
tokens = [];
}
return lineTokens;
}
getAlgorithms = function(lineTokens) {
// build the list of supported algorithms
for (i = 0; i < lineTokens.length; i++) {
if ((i == 0) || (lineTokens[i].length != 8))
continue; // skip the header
if (lineTokens[i][7].trim().toUpperCase() == "OK") {
var algName = lineTokens[i][0].trim();
var algDescription = lineTokens[i][1].trim();
var algId = lineTokens[i][2].trim();
var annotationList = lineTokens[i][3].replace(/"/g, " ").trim();
var language = lineTokens[i][4].replace(/"/g, " ").trim();
algorithms[algorithms.length] = {
"name" : algName,
"description" : algDescription,
"id" : algId,
"lang" : language,
"annotations" : annotationList
};
}
}
}
/*
buildAnnotationsAndLanguages = function() {
// extract data about languages and annotations in order to populate the proper
// controls in the page
buildLanguageSelection();
buildAnnotations();
}*/
buildAnnotations = function() {
annotations = [];
var language = $("#language-select").val();
for (i in algorithms) {
if(algorithms[i].lang.toLowerCase() != language.toLowerCase())
continue;
var annotationList = algorithms[i].annotations;
var a = annotationList.split(/\s|,/);
for (j in a) {
a[j] = a[j].trim();
if (a[j].length > 0) {
var thereIs = false;
for (k in annotations) {
if (annotations[k].toUpperCase() == a[j].toUpperCase()) {
thereIs = true;
break;
}
}
if (!thereIs)
annotations[annotations.length] = a[j];
}
}
}
}
buildLanguageSelection = function() {
for (i in algorithms) {
var languageList = algorithms[i].lang;
var langs = languageList.split(/\s|,/);
for (j in langs) {
langs[j] = langs[j].trim();
if (langs[j].length > 0) {
var thereIs = false;
for (k in languages) {
if (languages[k].toUpperCase() == langs[j].toUpperCase()) {
thereIs = true;
break;
}
}
if (!thereIs)
languages[languages.length] = langs[j];
}
}
}
}
setEventListeners = function() {
$("#input-textarea").on("keyup", function() {
if ($("#input-textarea").val() == "")
$("#execute-button").css("background-color", pageGray);
else
$("#execute-button").css("background-color", pageGreen);
});
$("#execute-button")
.click(
function() {
if ((publicLink == null)
&& ($("#input-textarea").val() == "")) {
alert("You must upload a file or write some text in the text area before submit a request.");
return;
}
showProgress();
// if some text has been written in the text area, then a corresponding text file
// must be written in the workspace and the public link is set.
if ($("#input-textarea").val().length > 0) {
freeText = $("#input-textarea").val();
$
.ajax({
url : "/nlphub/nlphub-uploader-servlet?freetext="
+ encodeURI(freeText),
type : "POST",
async : true,
contentType : "text/plain; charset=utf-8",
success : function(data, stato) {
publicLink = data.message;
if (publicLink == null) {
alert("Error uploading file.");
}
launchAlgorithm();
},
error : function(richiesta, stato,
errori) {
alert("Unexpected Error. Status: "
+ richiesta.status);
hideProgress();
textAreaEnable(true);
$("#file-info").empty();
}
});
}
else {
launchAlgorithm();
}
});
}
showProgress = function() {
var width = $(".progress-circular-div").width();
var height = $(".progress-circular-div").height();
var left = parseInt((window.innerWidth - width) / 2);
var top = parseInt((window.innerHeight - height) / 2);
$(".progress-circular-div").css("left", left);
$(".progress-circular-div").css("top", top);
$(".hidden-div").css("display", "block");
}
hideProgress = function() {
$(".hidden-div").css("display", "none");
}
launchAlgorithm = function() {
if (publicLink == null) {
alert("No input text available for the service.");
return;
}
var list = buildInputAnnotationList();
var annList = "";
for (i in list) {
annList += list[i] + ",";
}
annList = annList.substring(0, annList.length - 1);
savedAnnotations = annList;
var algList = "";
for (j in algorithms) {
if(algorithms[j].lang.toLowerCase().indexOf($("#language-select").val().toLowerCase()) >= 0) {
algList += encodeURI(algorithms[j].id) + ",";
}
}
if(algList.length == 0) {
alert("Warning. No algorithm matching with selected language.");
return;
}
algList = algList.substring(0, algList.length - 1);
var parameters = "annotations=" + annList;
parameters += "&lang=" + $("#language-select").val();
parameters += "&plink=" + encodeURI(publicLink);
parameters += "&algs=" + algList;
$.ajax({
url : "/nlphub/nlphub-servlet?" + parameters,
type : "POST",
async : true,
success : function(data, stato) {
textAreaEnable(true);
$("#file-info").empty();
if (typeof (data.response) != 'undefined') {
var jsonOut = getOutputJson(data.message);
} else if (typeof (data.error) != 'undefined') {
hideProgress();
alert(data.message);
} else {
hideProgress();
alert("Unexpected response");
}
resetExecuteButton();
},
error : function(richiesta, stato, errori) {
hideProgress();
textAreaEnable(true);
alert("Unexpected Error. Status: " + richiesta.status);
resetExecuteButton();
}
});
}
getOutputJson = function(message) {
var tobemap = "";
for (var i = 0; i < message.length; i++) {
tobemap += message[i] + "|";
}
tobemap = tobemap.substring(0, tobemap.length - 1);
var parameters = "annotations=" + savedAnnotations;
parameters += "&plink=" + encodeURI(publicLink);
parameters += "&lang=" + $("#language-select").val();
parameters += "&tobemap=" + encodeURI(tobemap);
console.log(parameters);
$.ajax({
url : "/nlphub/nlphub-mapper-servlet?" + parameters,
type : "POST",
async : true,
success : function(data, stato) {
hideProgress();
console.log(data);
if((typeof (data.response) != "undefined") && (data.response.trim().toUpperCase() == "ERROR")) {
alert ("ERROR\n" + data.message);
}
else {
$("#reset-upload").hide();
savedAnnotations = "";
publicLink = null;
showResult(data);
}
},
error : function(richiesta, stato, errori) {
hideProgress();
$("#reset-upload").hide();
savedAnnotations = "";
publicLink = null;
alert("Unexpected Error. Status: " + richiesta.status);
}
});
}
showResult = function(data) {
$("#ner-ui").hide();
$("#ner-result-container").show();
jsonContent = data;
//jsonContent = JSON.parse(purgeJson(data));
//resultText = jsonToText(jsonContent.output.text);
resultText = jsonContent.output.text;
$("#result-header").empty();
$("#result-params-div").empty();
$("#result-text-div").empty();
$("#result-header")
.append(
"<p>You can download the overall result as a JSON file <a href='" + jsonContent.link +"'>here</a></p>");
var localText = resultText;
localText = resultText.replace(/\n/g, "<br>");
$("#result-text-div").empty();
$("#result-text-div").append("<p>" + localText + "</p>");
showAnnotationList(jsonContent.output.annotations);
}
showAnnotationList = function(list) {
var colorDisabled = "CCCCCC";
var color;
var colors = [];
var annotations = list.split(",");
for (var i = 0; i < annotations.length; i++) {
do {
color = randomRGB();
}
while(color == colorDisabled);
colors[colors.length] = color;
}
$("#result-params-div")
.append("<form id='colored-annotations'></form>");
for (var i = 0; i < annotations.length; i++) {
var cb = "<input type='radio' name='foo' ";
if(!checkAnnotation(annotations[i])) {
cb += "disabled ";
colors[i] = colorDisabled;
}
cb += "named='" + annotations[i] + "' value='" + colors[i] + "' id='color-" + i + "' class='filled-in'>";
cb += "<label for='color-" + i + "'><span style='font-weight:bold; color:#" + colors[i] + "'>"
+ annotations[i] + "</span></label><br>";
$("#colored-annotations").append(cb);
}
$("#colored-annotations :radio").change(function() {
if (this.checked) {
rewriteText(this.getAttribute("named"), "#" + this.value);
}
});
}
countSubstringOccurrencies = function(string, substring) {
var occurrencies = 0;
var index = 0;
var s = string;
while(index >= 0) {
index = s.indexOf(substring);
if(index >= 0) {
occurrencies++;
s = s.substring(index + 1);
}
}
return occurrencies;
}
rewriteText = function(annotation, color) {
$("#result-text-div").empty();
console.log(annotation + " " + color);
var complement = "#"
+ (0xFFFFFF - eval("0x" + color.substring(1))).toString(16);
complement = "#F0F0F0";
var indices = getIndices(annotation);
var indexedText = "";
if ((typeof (indices) == 'undefined' ) || (indices.length == 0)) {
indexedText = resultText;
indexedText = indexedText.replace(/\n/g, "<br>");
$("#result-text-div").append("<p>" + indexedText + "</p>");
return;
}
var t = 0;
var offset = 0;
for (var i = 0; i < indices.length; i++) {
var index = indices[i];
var start = index[0];
var end = index[1];
indexedText += resultText.substring(t, start);
offset += countSubstringOccurrencies(resultText.substring(t, start), "\n");
offset += countSubstringOccurrencies(resultText.substring(t, start), "\r");
offset += countSubstringOccurrencies(resultText.substring(t, start), "\t");
start += offset;
end += offset;
var colored = "<span style='color:" + color + "; background:" + complement + "; font-weight:bold;'>"
+ resultText.substring(start, end) + "</span>";
indexedText += colored;
t = end;
}
if (t < resultText.length)
indexedText += resultText.substring(t);
indexedText = indexedText.replace(/\n/g, "<br>");
$("#result-text-div").append("<p>" + indexedText + "</p>");
}
checkAnnotation = function(annotation) {
for (var i = 0; i < jsonContent.output.result.length; i++) {
var entities = jsonContent.output.result[i].entities;
for (var j = 0; j < entities.length; j++) {
a = entities[j][annotation];
if (typeof a != 'undefined') {
return true;
}
}
}
return false;
}
getIndices = function(annotation) {
var indices = [];
// get indices
for (var i = 0; i < jsonContent.output.result.length; i++) {
var entities = jsonContent.output.result[i].entities;
for (var j = 0; j < entities.length; j++) {
a = entities[j][annotation];
if (typeof a != 'undefined') {
indices[i] = [];
for (var k = 0; k < a.length; k++) {
var index = a[k].indices;
indices[i][k] = index;
}
}
}
}
var indices2 = [];
for(var i=0; i<indices.length; i++) {
if(typeof (indices[i]) != 'undefined')
indices2[indices2.length] = indices[i];
}
return mergeIndices(indices2);
}
mergeIndices = function(indices) {
var newIndices = []
if (indices.length <= 1)
newIndices = indices[0];
else
newIndices = mergeAll(indices);
return newIndices;
}
resetExecuteButton = function() {
publicLink = null;
$("#execute-button").css("background-color", pageGray);
}
resizeTable = function() {
// resize the annotation table
$("#annotations-table").empty();
var rowId = "";
var ratio = window.innerWidth / window.innerHeight;
if (ratio <= 0.35) {
tableRawSize = 1;
} else if ((ratio > 0.35) && (ratio <= 0.75)) {
tableRawSize = 2;
} else if ((ratio > 0.75) && (ratio <= 1)) {
tableRawSize = 3;
} else if ((ratio > 1) && (ratio <= 1.5)) {
tableRawSize = 4;
} else {
tableRawSize = 5;
}
for (var i = 0; i < annotations.length; i++) {
if (i % tableRawSize == 0) {
rowId = "row-" + i;
$("#annotations-table").append("<tr id=\"" + rowId +"\"></tr>");
}
var annotationElement = "<input type=\"checkbox\" id=\"check-" + i + "\" value=\"" + annotations[i] + "\" checked=\"checked\"></input> <label for=\"check-" + i + "\">"
+ annotations[i] + "</label>";
$("#" + rowId).append("<td>" + annotationElement + "</td>");
}
}
resizeLogo = function() {
if (window.innerWidth < 200)
$("#logo-image").width(window.innerWidth - 10);
}
buildInputAnnotationList = function() {
var list = [];
for (var i = 0; i < annotations.length; i++) {
if ($("#check-" + i).is(":Checked"))
list[list.length] = $("#check-" + i).val();
}
return list;
}
// event handlers
window.onresize = function(event) {
resizeTable();
resizeLogo();
};
$(".ajax-file-upload-abort").on("DOMAttrModified", function(event) {
$(".ajax-file-upload-abort").css("display", "none");
if (event.attributeName == "display") { // which attribute you want to watch for changes
$(".ajax-file-upload-abort").css("display", "none");
}
});
buildLanguageList = function() {
// build the language selection input control
for (i in languages) {
var opt = "<option value=\"" + languages[i] + "\" selected=\"selected\">"
+ languages[i] + "</option>";
if (i > 1)
opt = "<option value=\"" + languages[i] + "\">" + languages[i]
+ "</option>";
$("#language-select").append(opt);
}
$("#language-select").on("change", function() {
selectAnnotationsByLanguage();
});
}
selectAnnotationsByLanguage = function() {
buildAnnotations();
resizeTable();
}
textAreaEnable = function(enable) {
if (enable == true)
$("#input-textarea").prop('disabled', false);
else
$("#input-textarea").prop('disabled', true);
}
/*
purgeJson = function(json) {
console.log("purgeJson:before: " + json.length);
var purged = json.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r")
.replace(/\t/g, "\\\\t");
console.log("purgeJson:after : " + purged.length);
return purged;
}
jsonToText = function(json) {
console.log("jsonToText:before: " + json.length);
var purged = json.replace(/\\n/g, "");
console.log("jsonToText:after : " + purged.length);
return purged;
//return json.replace(/\\n/g, "\n");
}
*/
randomRGB = function() {
var letters = '0123456789ABCDEF';
var color = '';
var couple = '';
for(var i=0; i < 3; i++) {
do {
couple = '';
for(j=0; j<2; j++) {
couple += letters[Math.floor(Math.random() * 16)];
}
} while((255 - parseInt("0x" + couple)) < 48)
color += couple;
}
return color;
}
</script>
</head>
<body style="padding: 0 15px;">

View File

@ -1,156 +1,763 @@
$(document).ready(function(){
var tableRawSize = 5;
var annotations = [];
var algorithms = [];
var languages = [];
var publicLink = null;
var pageGreen = "#4CAF50";
var pageGray = "#A0A0A0";
var savedAnnotations = "";
var jsonContent = null;
var named = null;
var resultText = "";
var hexLetters = '0123456789ABCDEF';
// $("#upload-button").uploadFile({
// url : "uploader.php",
// fileName : "myfile",
// maxFileCount : 100,
// multiple : false,
// maxFileSize : 1024 * 1000 * 1,
// showFileCounter : false,
// showCancel : true,
// allowedTypes : "txt",
// //dragDropStr : "<img class='img-prompt' src='images/upload.png' width='60px' height='60px' style='display:block; margin:auto;'>",
// doneStr : "Done",
// abortStr : "End",
// cancelStr : "Cancel",
// extErrorStr : "Only text file (.txt), please",
// sizeErrorStr : "Max size: 1 Mb",
// onLoad : function(obj) {
// //resizeTutor();
// },
// onSuccess : function(files, data, xhr) {
// alert("Success");
// /*$(".ajax-file-upload-statusbar")
// .hide();
// $(".obfuscating").css('opacity', '0.6');
// $(".obfuscating").show();
// $("#uploadedimg").attr("src", "images/" + uName + "/" + files[0]);
// source = $("#uploadedimg").attr("src");
// window.setTimeout(function(){ sizePopupUpload(); $("#popup-1-section").show();}, 500);*/
// },
// onError : function(files, status,
// errMsg, pd) {
// alert(errMsg);
// }
// });
// ------------------------------------------------------------------------------------------------------
// Starting point: get the csv file with algorithm parameters and set the page
// with valid algs info.
// ------------------------------------------------------------------------------------------------------
$( "#execute-button" ).click(function() {
var firstnameBox = $.trim( $('#input-textarea').val() )
if (firstnameBox == "") {
$('#input-textarea').css("border-color","red");
$('#input-textarea').attr("placeholder", "Paste your text here!");
}
else {
$('#input-textarea').css("border-color","#555");
doStartComputation();
}
});
$
.ajax({
url : "https://data.d4science.org/bnp4UDNyb21lSURkQXdDUnlSS0JkVWgzWk1KMUxWVGZHbWJQNStIS0N6Yz0",
type : "GET",
async : true,
contentType : "text/plain; charset=utf-8",
success : function(data, stato) {
$("#ner-result-container").hide();
var lineTokens = manageCsvData(data);
getAlgorithms(lineTokens);
buildLanguageSelection();
buildLanguageList();
buildAnnotations();
resizeTable();
resizeLogo();
doCallback = function(uri) {
$.ajax({
url : "nlphub-servlet",
type : 'GET',
datatype : 'json',
data : {
uri : uri
$("#back-ner-ui-button").click(function() {
$("#ner-result-container").hide();
$("#ner-ui").show();
$("#input-textarea").val("");
publicLink = null;
jsonContent = null;
});
$("#execute-button").css("background-color", pageGray);
$("#upload-button")
.uploadFile(
{
url : "/nlphub/nlphub-uploader-servlet",
fileName : "mytxtfile",
maxFileCount : 100,
multiple : false,
maxFileSize : 1024 * 1000 * 6.14,
showFileCounter : false,
showCancel : true,
dragDropStr : "<img class='img-prompt' src='img/upload.png' width='60px' height='60px' style='display:block; margin:auto; padding: 10px'>",
extErrorStr : "Error. Text file only",
sizeErrorStr : "Error. Max size: 6 Mb",
onLoad : function(obj) {
$("#file-info").remove();
$("#reset-upload").remove();
$("#fu-container")
.append(
"<div id=\"right-child\" class=\"float-right-div\"></div>");
$("#right-child")
.append(
"<div id=\"file-info\" class=\"centered\"></div>");
$("#right-child")
.append(
"<div id=\"reset-upload\" class=\"centered\">Cancel</div>");
$("#reset-upload").hide();
$("#reset-upload").click(
function() {
resetExecuteButton();
$("#file-info").empty();
$("#reset-upload").css(
"display", "none");
textAreaEnable(true);
});
savedAnnotations = "";
setEventListeners();
},
onSelect : function(files) {
showProgress();
textAreaEnable(false);
$("#input-textarea").val("");
},
onSuccess : function(files, data, xhr) {
hideProgress();
console.log('' + data.language);
checkLanguage(data.language);
selectAnnotationsByLanguage();
$("#reset-upload").css("display",
"inline");
console.log("files: " + files);
$("#file-info").empty();
$("#file-info")
.append(
"Uploaded: <span style=\"color:#4CAF50; letter-spacing: 1px; font-weight: bold;\">"
+ files
+ "</span>");
publicLink = data.message;
$("#execute-button").css(
"background-color", pageGreen);
if (publicLink == null) {
alert("Error uploading file.");
}
},
onError : function(files, status, errMsg,
pd) {
hideProgress();
textAreaEnable(true);
alert(errMsg);
}
});
},
success : function(data) {
var obj = eval("(" + data + ")");
var str = JSON.stringify(obj, undefined, 4);
$('#result').html(syntaxHighlight(str));
error : function(richiesta, stato, errori) {
alert("Unexpected Error. Status: " + richiesta.status);
}
});
}
doStartComputation = function() {
var options = "default";
options = $("input[type=checkbox]:checked").map(
function () {return this.value;}).get().join("|");
$('#result').html("");
//var token2Send = getUrlParameter("token") == null ? "" : getUrlParameter("token");
var tokenParam = getUrlParameter("token");
var token2Send = ((tokenParam == null) || (tokenParam.length == 0)) ? "18fed2d9-030b-4c77-93af-af2015d945f7-843339462" : getUrlParameter("token");
webSocket.send({
'action': 'start',
'text' : $("#input-textarea").val(),
'options' : options,
'token' : token2Send
}).done(function() {
showProgressBar(true);
enableCommands(false);
}).fail(function(e) {
console.log("failed");
});
}
doHandleResponse = function(message) {
if (message.response == "error") {
$('#result').html("<span style='color: red;'>"+message.value+"</span>");
enableCommands(true);
showProgressBar(false);
}
else if (message.response == "computing") {
showProgressBar(true);
enableCommands(false);
}
else if (message.response == "computed") {
showProgressBar(false);
enableCommands(true);
console.log("message="+message.value);
if (message.value.startsWith("http")) {
$('#downloadLink').html("Result:&nbsp;<a class=\"waves-effect waves-light btn red darken-1\" href=\""+message.value+"\">Download</a>&nbsp;" +
"<a class=\"waves-effect waves-light btn blue darken-1\" href=\"Javascript:doCallback(encodeURI('"+message.value.trim()+"'));\">View</a>");
}
/*
* Utility function processing the language indication returned by the language recognition service
*/
checkLanguage = function(lang) {
var options = $("#language-select option");
for(var i=0; i<options.length; i++) {
if(options[i].innerText.toLowerCase() == lang.toLowerCase()) {
$("#language-select").val(options[i].innerText);
buildAnnotations();
return;
}
}
alert("The uploaded file seems to be written in " + lang + ", but this language is not supported by listed algorithms. Select the language you want, or try with another text.");
}
/*
* CSV parser
*/
manageCsvData = function(data) {
// first of all: split on the "newline" character in order to
// get all lines.
var lines = data.split("\n");
var tokens = [], lineTokens = [];
// second: parse each line
for (i in lines) {
var line = ',' + lines[i];
var tokenStart = -1;
for (var j = 0; j < line.length; j++) {
if (line.charAt(j) == '\"') {
j = line.indexOf('\"', j + 1);
if (j < 0)
return;
continue;
}
if (line.charAt(j) == ',') {
if (tokenStart == -1) {
tokenStart = j;
} else {
tokens[tokens.length] = line.substring(tokenStart + 1, j);
tokenStart = j;
}
}
}
// get the new token and push it in the array
tokens[tokens.length] = line.substring(tokenStart + 1);
lineTokens[lineTokens.length] = tokens;
tokens = [];
}
return lineTokens;
}
/*
* Map the supported algorithms (from CSV) in the internal global algorithm array
*/
getAlgorithms = function(lineTokens) {
// build the list of supported algorithms
for (i = 0; i < lineTokens.length; i++) {
if ((i == 0) || (lineTokens[i].length != 8))
continue; // skip the header
if (lineTokens[i][7].trim().toUpperCase() == "OK") {
var algName = lineTokens[i][0].trim();
var algDescription = lineTokens[i][1].trim();
var algId = lineTokens[i][2].trim();
var annotationList = lineTokens[i][3].replace(/"/g, " ").trim();
var language = lineTokens[i][4].replace(/"/g, " ").trim();
algorithms[algorithms.length] = {
"name" : algName,
"description" : algDescription,
"id" : algId,
"lang" : language,
"annotations" : annotationList
};
}
}
}
/*
* Build the annotation list based on the language selected
*/
buildAnnotations = function() {
annotations = [];
var language = $("#language-select").val();
for (i in algorithms) {
if (algorithms[i].lang.toLowerCase() != language.toLowerCase())
continue;
var annotationList = algorithms[i].annotations;
var a = annotationList.split(/\s|,/);
for (j in a) {
a[j] = a[j].trim();
if (a[j].length > 0) {
var thereIs = false;
for (k in annotations) {
if (annotations[k].toUpperCase() == a[j].toUpperCase()) {
thereIs = true;
break;
}
}
if (!thereIs)
annotations[annotations.length] = a[j];
}
}
}
}
/*
* Build the language array
*/
buildLanguageSelection = function() {
for (i in algorithms) {
var languageList = algorithms[i].lang;
var langs = languageList.split(/\s|,/);
for (j in langs) {
langs[j] = langs[j].trim();
if (langs[j].length > 0) {
var thereIs = false;
for (k in languages) {
if (languages[k].toUpperCase() == langs[j].toUpperCase()) {
thereIs = true;
break;
}
}
if (!thereIs)
languages[languages.length] = langs[j];
}
}
}
}
/*
* Set the listeners on the text area and the execute button
*/
setEventListeners = function() {
$("#input-textarea").on("keyup", function() {
if ($("#input-textarea").val() == "")
$("#execute-button").css("background-color", pageGray);
else
$("#execute-button").css("background-color", pageGreen);
});
$("#execute-button")
.click(
function() {
if ((publicLink == null)
&& ($("#input-textarea").val() == "")) {
alert("You must upload a file or write some text in the text area before submit a request.");
return;
}
showProgress();
// if some text has been written in the text area, then
// a corresponding text file
// must be written in the workspace and the public link
// is set.
if ($("#input-textarea").val().length > 0) {
freeText = $("#input-textarea").val();
$
.ajax({
url : "/nlphub/nlphub-uploader-servlet?freetext="
+ encodeURI(freeText),
type : "POST",
async : true,
contentType : "text/plain; charset=utf-8",
success : function(data, stato) {
publicLink = data.message;
// console.log(data.language);
if (publicLink == null) {
alert("Error uploading file.");
}
launchAlgorithm();
},
error : function(richiesta, stato,
errori) {
alert("Unexpected Error. Status: "
+ richiesta.status);
hideProgress();
textAreaEnable(true);
$("#file-info").empty();
}
});
}
else {
launchAlgorithm();
}
});
}
/*
* show the in-progress popup
*/
showProgress = function() {
var width = $(".progress-circular-div").width();
var height = $(".progress-circular-div").height();
var left = parseInt((window.innerWidth - width) / 2);
var top = parseInt((window.innerHeight - height) / 2);
$(".progress-circular-div").css("left", left);
$(".progress-circular-div").css("top", top);
$(".hidden-div").css("display", "block");
}
/*
* hide the in-progress popup
*/
hideProgress = function() {
$(".hidden-div").css("display", "none");
}
/*
* run the algorithms selected by the chosen language
*/
launchAlgorithm = function() {
if (publicLink == null) {
alert("No input text available for the service.");
return;
}
var list = buildInputAnnotationList();
var annList = "";
for (i in list) {
annList += list[i] + ",";
}
annList = annList.substring(0, annList.length - 1);
savedAnnotations = annList;
var algList = "";
for (j in algorithms) {
if (algorithms[j].lang.toLowerCase().indexOf(
$("#language-select").val().toLowerCase()) >= 0) {
algList += encodeURI(algorithms[j].id) + ",";
}
}
if (algList.length == 0) {
alert("Warning. No algorithm matching with selected language.");
return;
}
algList = algList.substring(0, algList.length - 1);
var parameters = "annotations=" + annList;
parameters += "&lang=" + $("#language-select").val();
parameters += "&plink=" + encodeURI(publicLink);
parameters += "&algs=" + algList;
$.ajax({
url : "/nlphub/nlphub-servlet?" + parameters,
type : "POST",
async : true,
success : function(data, stato) {
textAreaEnable(true);
$("#file-info").empty();
if (typeof (data.response) != 'undefined') {
var jsonOut = getOutputJson(data.message);
} else if (typeof (data.error) != 'undefined') {
hideProgress();
alert(data.message);
} else {
hideProgress();
alert("Unexpected response");
}
resetExecuteButton();
},
error : function(richiesta, stato, errori) {
hideProgress();
textAreaEnable(true);
alert("Unexpected Error. Status: " + richiesta.status);
resetExecuteButton();
}
});
}
/*
* Get the final json returned by the server
*/
getOutputJson = function(message) {
var tobemap = "";
for (var i = 0; i < message.length; i++) {
tobemap += message[i] + "|";
}
tobemap = tobemap.substring(0, tobemap.length - 1);
var parameters = "annotations=" + savedAnnotations;
parameters += "&plink=" + encodeURI(publicLink);
parameters += "&lang=" + $("#language-select").val();
parameters += "&tobemap=" + encodeURI(tobemap);
console.log(parameters);
$.ajax({
url : "/nlphub/nlphub-mapper-servlet?" + parameters,
type : "POST",
async : true,
success : function(data, stato) {
hideProgress();
console.log(data);
if ((typeof (data.response) != "undefined")
&& (data.response.trim().toUpperCase() == "ERROR")) {
alert("ERROR\n" + data.message);
} else {
$("#reset-upload").hide();
savedAnnotations = "";
publicLink = null;
showResult(data);
}
},
error : function(richiesta, stato, errori) {
hideProgress();
$("#reset-upload").hide();
savedAnnotations = "";
publicLink = null;
alert("Unexpected Error. Status: " + richiesta.status);
}
});
}
/*
* Show the computation result
*/
showResult = function(data) {
$("#ner-ui").hide();
$("#ner-result-container").show();
jsonContent = data;
resultText = jsonContent.output.text;
$("#result-header").empty();
$("#result-params-div").empty();
$("#result-text-div").empty();
$("#result-header").append(
"<p>You can download the overall result as a JSON file <a href='"
+ jsonContent.link + "'>here</a></p>");
var localText = resultText;
localText = resultText.replace(/\n/g, "<br>");
$("#result-text-div").empty();
$("#result-text-div").append("<p>" + localText + "</p>");
showAnnotationList(jsonContent.output.annotations);
}
showAnnotationList = function(list) {
var colorDisabled = "CCCCCC";
var color;
var colors = [];
var annotations = list.split(",");
for (var i = 0; i < annotations.length; i++) {
do {
color = randomRGB();
} while (color == colorDisabled);
colors[colors.length] = color;
}
$("#result-params-div").append("<form id='colored-annotations'></form>");
for (var i = 0; i < annotations.length; i++) {
var cb = "<input type='radio' name='foo' ";
if (!checkAnnotation(annotations[i])) {
cb += "disabled ";
colors[i] = colorDisabled;
}
cb += "named='" + annotations[i] + "' value='" + colors[i]
+ "' id='color-" + i + "' class='filled-in'>";
cb += "<label for='color-" + i
+ "'><span style='font-weight:bold; color:#" + colors[i] + "'>"
+ annotations[i] + "</span></label><br>";
$("#colored-annotations").append(cb);
}
$("#colored-annotations :radio").change(function() {
if (this.checked) {
rewriteText(this.getAttribute("named"), "#" + this.value);
}
});
}
/*
* Utility function
*/
countSubstringOccurrencies = function(string, substring) {
var occurrencies = 0;
var index = 0;
var s = string;
while (index >= 0) {
index = s.indexOf(substring);
if (index >= 0) {
occurrencies++;
s = s.substring(index + 1);
}
}
return occurrencies;
}
/*
* Utility
*/
enhanceColor = function(color) {
var c = eval("0x" + color);
var hi = Math.round(c / 16);
if(hi < 15) {
hi += Math.round((15 - hi) / 1.5);
}
if(hi > 15) hi = 15;
return '' + hexLetters[hi] + hexLetters[c%16];
}
/*
* Write the html paragraph <p> containing the text highlighted on annotation value
*/
rewriteText = function(annotation, color) {
$("#result-text-div").empty();
var complemetar = 0xFFFFFF - eval("0x" + color.substring(1));
var complement = complemetar.toString(16);
var R = enhanceColor(complement.substring(0,2));
var G = enhanceColor(complement.substring(2,4));
var B = enhanceColor(complement.substring(4));
complement = "#" + R + G + B;
var indices = getIndices(annotation);
var indexedText = "";
if ((typeof (indices) == 'undefined') || (indices.length == 0)) {
indexedText = resultText;
indexedText = indexedText.replace(/\n/g, "<br>");
$("#result-text-div").append("<p>" + indexedText + "</p>");
return;
}
var t = 0;
var offset = 0;
for (var i = 0; i < indices.length; i++) {
var index = indices[i];
var start = index[0];
var end = index[1];
indexedText += resultText.substring(t, start);
offset += countSubstringOccurrencies(resultText.substring(t, start),
"\n");
offset += countSubstringOccurrencies(resultText.substring(t, start),
"\r");
offset += countSubstringOccurrencies(resultText.substring(t, start),
"\t");
start += offset;
end += offset;
var colored = "<span style='color:" + color + "; background:"
+ complement + "; font-weight:bold;'>"
+ resultText.substring(start, end) + "</span>";
indexedText += colored;
t = end;
}
if (t < resultText.length)
indexedText += resultText.substring(t);
indexedText = indexedText.replace(/\n/g, "<br>");
$("#result-text-div").append("<p>" + indexedText + "</p>");
}
/*
* Find if the annotation is presente in the json
*/
checkAnnotation = function(annotation) {
for (var i = 0; i < jsonContent.output.result.length; i++) {
var entities = jsonContent.output.result[i].entities;
for (var j = 0; j < entities.length; j++) {
a = entities[j][annotation];
if (typeof a != 'undefined') {
return true;
}
}
}
return false;
}
/*
* Retrieve the annotation indices from the json
*/
getIndices = function(annotation) {
var indices = [];
// get indices
for (var i = 0; i < jsonContent.output.result.length; i++) {
var entities = jsonContent.output.result[i].entities;
for (var j = 0; j < entities.length; j++) {
a = entities[j][annotation];
if (typeof a != 'undefined') {
indices[i] = [];
for (var k = 0; k < a.length; k++) {
var index = a[k].indices;
indices[i][k] = index;
}
}
}
}
var indices2 = [];
for (var i = 0; i < indices.length; i++) {
if (typeof (indices[i]) != 'undefined')
indices2[indices2.length] = indices[i];
}
return mergeIndices(indices2);
}
/*
* Merge the indices
*/
mergeIndices = function(indices) {
var newIndices = []
if (indices.length <= 1)
newIndices = indices[0];
else
newIndices = mergeAll(indices);
return newIndices;
}
/*
* Utility function
*/
resetExecuteButton = function() {
publicLink = null;
$("#execute-button").css("background-color", pageGray);
}
/*
* Resize the annotation table based on the screen geometry
*/
resizeTable = function() {
// resize the annotation table
$("#annotations-table").empty();
var rowId = "";
var ratio = window.innerWidth / window.innerHeight;
if (ratio <= 0.35) {
tableRawSize = 1;
} else if ((ratio > 0.35) && (ratio <= 0.75)) {
tableRawSize = 2;
} else if ((ratio > 0.75) && (ratio <= 1)) {
tableRawSize = 3;
} else if ((ratio > 1) && (ratio <= 1.5)) {
tableRawSize = 4;
} else {
tableRawSize = 5;
}
for (var i = 0; i < annotations.length; i++) {
if (i % tableRawSize == 0) {
rowId = "row-" + i;
$("#annotations-table").append("<tr id=\"" + rowId + "\"></tr>");
}
var annotationElement = "<input type=\"checkbox\" id=\"check-" + i
+ "\" value=\"" + annotations[i]
+ "\" checked=\"checked\"></input> <label for=\"check-" + i
+ "\">" + annotations[i] + "</label>";
$("#" + rowId).append("<td>" + annotationElement + "</td>");
}
}
/*
* Resize the page logo image based on the screen geometry
*/
resizeLogo = function() {
if (window.innerWidth < 200)
$("#logo-image").width(window.innerWidth - 10);
}
/*
* Build the annotation list for the service invocation
*/
buildInputAnnotationList = function() {
var list = [];
for (var i = 0; i < annotations.length; i++) {
if ($("#check-" + i).is(":Checked"))
list[list.length] = $("#check-" + i).val();
}
return list;
}
/*
* Handler for the window resize event
*/
window.onresize = function(event) {
resizeTable();
resizeLogo();
};
/*
* Handler for the file upload control
*/
$(".ajax-file-upload-abort").on("DOMAttrModified", function(event) {
$(".ajax-file-upload-abort").css("display", "none");
if (event.attributeName == "display") { // which attribute you want to watch
// for changes
$(".ajax-file-upload-abort").css("display", "none");
}
});
function showProgressBar(show) {
var display = (show) ? "visible" : "hidden";
$('#progressBar').css('visibility', display);
}
function enableCommands(enable) {
if (enable) {
$('#execute-button').removeAttr("disabled");
$('#execute-button').text('Execute');
$('input[type=checkbox]').removeAttr("disabled");
} else {
$('#execute-button').text('Computing ...');
$('input[type=checkbox]').attr('disabled', 'true');
$('#execute-button').attr('disabled', 'disabled');
$('#downloadLink').html("");
/*
* Build the option list of supported language
*/
buildLanguageList = function() {
// build the language selection input control
for (i in languages) {
var opt = "<option value=\"" + languages[i]
+ "\" selected=\"selected\">" + languages[i] + "</option>";
if (i > 1)
opt = "<option value=\"" + languages[i] + "\">" + languages[i]
+ "</option>";
$("#language-select").append(opt);
}
}
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 1);
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
$("#language-select").on("change", function() {
selectAnnotationsByLanguage();
});
}
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
/*
* Utility
*/
selectAnnotationsByLanguage = function() {
buildAnnotations();
resizeTable();
}
/*
* Enable/disable the input text area
*/
textAreaEnable = function(enable) {
if (enable == true)
$("#input-textarea").prop('disabled', false);
else
$("#input-textarea").prop('disabled', true);
}
/*
* Get a random RGB color
*/
randomRGB = function() {
var color = '';
var couple = '';
for (var i = 0; i < 3; i++) {
couple = '' + hexLetters[Math.floor(Math.random() * 10)] + hexLetters[Math.floor(Math.random() * 16)];
color += couple;
}
return color;
}