216 lines
7.9 KiB
Java
216 lines
7.9 KiB
Java
package org.gcube.nlphub.nlp;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
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.Constants;
|
|
import org.gcube.nlphub.legacy.DataminerClient;
|
|
import org.gcube.nlphub.legacy.JsonManager;
|
|
import org.gcube.nlphub.legacy.NlpHubException;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.NodeList;
|
|
import org.w3c.dom.Node;
|
|
import org.xml.sax.InputSource;
|
|
|
|
public class NLpLanguageRecognizer extends DataminerClient {
|
|
private HttpServletResponse response;
|
|
private Logger logger = Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName());
|
|
private String sentence, publicLink;
|
|
public final static String RECOGNIZER_ID = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.LANGUAGE_RECOGNIZER";
|
|
private String dataMiner = null;
|
|
|
|
public NLpLanguageRecognizer(String service, String token, String sentence) {
|
|
super(service, "", token);
|
|
this.sentence = sentence;
|
|
response = null;
|
|
}
|
|
|
|
public NLpLanguageRecognizer(String dataMiner, String service, String token, String sentence, String publicLink,
|
|
HttpServletResponse response) {
|
|
super(service, "", token);
|
|
this.sentence = sentence;
|
|
this.response = response;
|
|
this.publicLink = publicLink;
|
|
this.dataMiner = dataMiner;
|
|
}
|
|
|
|
public static void run(String dataMiner, String sentence, String token, String publicLink, HttpServletResponse response) throws NlpHubException {
|
|
try {
|
|
if(dataMiner == null)
|
|
dataMiner = Constants.DATAMINER_URL;
|
|
else
|
|
dataMiner = "http://" + dataMiner;
|
|
String urlService = dataMiner + "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0";
|
|
urlService += "&gcube-token=" + token;
|
|
urlService += "&lang=en-US";
|
|
urlService += "&Identifier=" + RECOGNIZER_ID;
|
|
urlService += "&DataInputs=sentence=" + URLEncoder.encode(sentence, "UTF-8");
|
|
URL url = new URL(urlService);
|
|
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()));
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
|
|
doc.getDocumentElement().normalize();
|
|
NodeList nListData = doc.getElementsByTagName("d4science:Data");
|
|
NodeList nListDesc = doc.getElementsByTagName("d4science:Description");
|
|
|
|
int len = nListData.getLength();
|
|
for(int i=0; i<len; i++) {
|
|
Node data = nListData.item(i);
|
|
Node description = nListDesc.item(i);
|
|
String link = data.getTextContent();
|
|
String type = description.getTextContent();
|
|
if(type.equals("outfile")) {
|
|
//System.out.println(link);
|
|
String content = readFileContent(link, token);
|
|
if (response != null) {
|
|
response.getWriter()
|
|
.println(new JsonManager().getSuccessJsonResponse(content, publicLink));
|
|
}
|
|
else {
|
|
Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName()).debug(new JsonManager().getSuccessJsonResponse(content, publicLink));
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName()).error(e.getLocalizedMessage());
|
|
throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
}
|
|
|
|
}
|
|
|
|
public void run() throws NlpHubException {
|
|
runUsingClientLibrary();
|
|
}
|
|
|
|
@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 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));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
// writeResponse(e.getLocalizedMessage(), false);
|
|
}
|
|
}
|
|
|
|
private void runUsingClientLibrary() 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);
|
|
}
|
|
}
|
|
|
|
|
|
private static String readFileContent(String link, String token) throws Exception {
|
|
URL url = new URL(link);
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
|
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);
|
|
}
|
|
connection.disconnect();
|
|
String out = response.toString();
|
|
return out;
|
|
}
|
|
|
|
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);
|
|
}
|
|
connection.disconnect();
|
|
String out = response.toString();
|
|
return out;
|
|
}
|
|
|
|
/*
|
|
public static void main(String[] args) {
|
|
//String pLink = "http://data.d4science.org/RkNBSmNFRG9MOHFLSWsrWUNQdHk3NTU0UC85ekRnSXNHbWJQNStIS0N6Yz0";
|
|
String token = Constants.TEST_TOKEN;
|
|
String sentence = "Questa mattina mi sono alzato ed ho trovato l'invasore.";
|
|
|
|
try {
|
|
//String sentence, String token, String publicLink, HttpServletResponse response
|
|
NLpLanguageRecognizer.run(sentence, token, "http://cazziemazzi", null);
|
|
} catch (NlpHubException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
*/
|
|
|
|
}
|