This commit is contained in:
Erik Perrone 2018-03-08 13:28:45 +00:00
parent 48edb1d2ee
commit 7f660e7377
5 changed files with 157 additions and 20 deletions

View File

@ -69,10 +69,6 @@ public class NLPHub extends HttpServlet {
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
token = Constants.getToken(request, devMode);
try {
System.out.println("annotations: " + request.getParameter("annotations"));
System.out.println("lang: " + request.getParameter("lang"));
System.out.println("plink: " + request.getParameter("plink"));
System.out.println("algs: " + request.getParameter("algs"));
String[] algs = request.getParameter("algs").split(",");
for(int i=0; i<algs.length; i++) {

View File

@ -79,7 +79,8 @@ public class NLPUploader extends HttpServlet {
private void handleFreeText(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String freeText = request.getParameter("freetext");
System.out.println("freeText: " + freeText);
freeText = freeText.replaceAll("[\\s]+", " ").trim();
byte[] content = freeText.getBytes("UTF-8");
String fileName = generateFileName();
PrintWriter writer = response.getWriter();
@ -116,7 +117,6 @@ public class NLPUploader extends HttpServlet {
byte[] readBuffer = new byte[len];
while (byteRead > -1) {
byteRead = fileContent.read(readBuffer, 0, len);
// System.out.println(byteRead);
if (byteRead > 0) {
System.arraycopy(readBuffer, 0, buffer, offset, byteRead);
offset += byteRead;
@ -129,9 +129,12 @@ public class NLPUploader extends HttpServlet {
} else
bufferedContent = buffer;
String stringContent = new String(bufferedContent);
stringContent = stringContent.replaceAll("[\\s]+", " ").trim();
ws.deleteFile(fileName, token);
if (!ws.uploadFile(bufferedContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
if (!ws.uploadFile(stringContent.getBytes(), fileName, Constants.DEFAULT_DESCRIPTION, token)) {
writer.println(new JsonManager().getErrorJsonResponse(
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
return;

View File

@ -0,0 +1,100 @@
package org.gcube.nlphub.nlp;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.dataminermanagercl.server.dmservice.SClient;
import org.gcube.data.analysis.dataminermanagercl.shared.data.OutputData;
import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.FileResource;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.MapResource;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.FileParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ListParameter;
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.DataminerClient;
import org.gcube.nlphub.legacy.NlpHubException;
public class NLpLanguageRecognizer extends DataminerClient {
private Logger logger = Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName());
private String sentence;
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";
public NLpLanguageRecognizer(String service, String token, String sentence) {
super(service, "", token);
this.sentence = sentence;
}
public void run() throws NlpHubException {
try {
super.identifier = RECOGNIZER_ID;
super.init();
ObjectParameter inputParameter = new ObjectParameter();
inputParameter.setName("sentence");
inputParameter.setValue(sentence);
ArrayList<Parameter> parameters = new ArrayList<>();
parameters.add(inputParameter);
super.execute(parameters);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
}
}
@Override
public void retrieveOutput(ComputationId computationId, SClient sClient) {
try {
OutputData output = sClient.getOutputDataByComputationId(computationId);
Resource resource = output.getResource();
if (resource.isMap()) {
MapResource mapResource = (MapResource) resource;
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")) {
String link = f.getUrl();
System.out.println("url: " + link);
String op = computationId.getOperatorId();
op = op.substring(op.lastIndexOf(".") + 1);
//testEndOfProcess(op + ":::" + link);
}
}
}
}
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
//writeResponse(e.getLocalizedMessage(), false);
}
}
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);
*/
}
}

View File

@ -86,6 +86,7 @@ public class NlpNerRunner extends DataminerClient {
if (mimeType.equalsIgnoreCase("application/d4science")) {
String link = f.getUrl();
System.out.println("url: " + link);
logger.debug("url: " + link);
String op = computationId.getOperatorId();
op = op.substring(op.lastIndexOf(".") + 1);
testEndOfProcess(op + ":::" + link);
@ -177,7 +178,7 @@ public class NlpNerRunner extends DataminerClient {
List<Parameter> parameters = new ArrayList<>();
try {
List<Parameter> inputParameters = super.getOperatorInputParameters();
System.out.println("n. " + inputParameters.size());
//System.out.println("n. " + inputParameters.size());
for (Parameter p : inputParameters) {
switch (p.getTypology()) {
case FILE:

View File

@ -47,10 +47,12 @@
$("#ner-result-container").hide();
var lineTokens = manageCsvData(data);
getAlgorithms(lineTokens);
buildAnnotationsAndLanguages();
buildLanguageSelection();
buildLanguageList();
buildAnnotations();
resizeTable();
resizeLogo();
buildLanguageList();
$("#back-ner-ui-button").click(function() {
$("#ner-result-container").hide();
$("#ner-ui").show();
@ -200,10 +202,21 @@
}
}
/*
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) {
@ -220,7 +233,11 @@
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) {
@ -237,10 +254,9 @@
languages[languages.length] = langs[j];
}
}
}
}
setEventListeners = function() {
$("#input-textarea").on("keyup", function() {
if ($("#input-textarea").val() == "")
@ -323,7 +339,13 @@
var algList = "";
for (j in algorithms) {
algList += encodeURI(algorithms[j].id) + ",";
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);
@ -337,14 +359,15 @@
type : "POST",
async : true,
success : function(data, stato) {
//hideProgress();
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();
@ -425,10 +448,16 @@
}
showAnnotationList = function(list) {
var colorDisabled = "CCCCCC";
var color;
var colors = [];
var annotations = list.split(",");
for (var i = 0; i < annotations.length; i++) {
colors[colors.length] = randomRGB();
do {
color = randomRGB();
}
while(color == colorDisabled);
colors[colors.length] = color;
}
$("#result-params-div")
@ -437,6 +466,7 @@
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] + "'>"
@ -544,7 +574,6 @@
}
mergeIndices = function(indices) {
// GESTIONE MULTIDIMENSIONALE DA FARE !!!!
var newIndices = []
if (indices.length <= 1)
newIndices = indices[0];
@ -624,15 +653,23 @@
+ "</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")
@ -648,7 +685,7 @@
return purged;
//return json.replace(/\\n/g, "\n");
}
*/
randomRGB = function() {
var letters = '0123456789ABCDEF';
var color = '';
@ -680,7 +717,7 @@
<div class="col s12">
<ul class="tabs">
<li class="tab col s3"><a href="#ner">NER</a></li>
<li class="tab col s3"><a href="#other">other</a></li>
<!-- <li class="tab col s3"><a href="#other">other</a></li> -->
</ul>
</div>