git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@164451 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
073b4ecfcb
commit
2098082241
|
@ -1,6 +1,12 @@
|
|||
package org.gcube.nlphub;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
@ -13,6 +19,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import org.apache.log4j.Logger;
|
||||
import org.gcube.nlphub.nlp.NlpNerRunner;
|
||||
import org.gcube.nlphub.mapper.JsonMapper;
|
||||
import org.gcube.nlphub.legacy.JsonManager;
|
||||
import org.gcube.nlphub.mapper.DefaultMapper;
|
||||
|
||||
/**
|
||||
|
@ -51,27 +58,75 @@ public class NLPMapper extends HttpServlet {
|
|||
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
// response.getWriter().append("Served at:
|
||||
// ").append(request.getContextPath());
|
||||
String documentLink = request.getParameter("dlink"); // link al testo (sul workspace)
|
||||
String documentLink = request.getParameter("plink"); // link al testo
|
||||
// (sul
|
||||
// workspace)
|
||||
String toBeMap = request.getParameter("tobemap");
|
||||
String[] tokens = toBeMap.split("|");
|
||||
String annotations = request.getParameter("annotations");
|
||||
String language = request.getParameter("language");
|
||||
|
||||
for (String token : tokens) {
|
||||
String[] t = token.split(":::");
|
||||
try {
|
||||
String json = ((JsonMapper)(getMapper(t[0]).newInstance())).getJson(t[0], t[1]);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage());
|
||||
}
|
||||
System.out.println("tobemap: " + toBeMap);
|
||||
String[] tokens;
|
||||
if(toBeMap.indexOf("|") > 0)
|
||||
tokens = toBeMap.split("|");
|
||||
else {
|
||||
tokens = new String[1];
|
||||
tokens[0] = toBeMap;
|
||||
}
|
||||
|
||||
//response.getWriter().write(json);
|
||||
System.out.println("token nr. : " + tokens.length);
|
||||
String annotations = request.getParameter("annotations");
|
||||
String language = request.getParameter("lang");
|
||||
PrintWriter writer = response.getWriter();
|
||||
try {
|
||||
String text = getDocument(documentLink);
|
||||
String out = "{";
|
||||
out += "\"text\":\"" + text + "\",";
|
||||
out += "\"annotations\":\"" + annotations + "\",";
|
||||
out += "\"language\":\"" + language + "\",";
|
||||
out += "\"result\": [";
|
||||
|
||||
for (int i = 0; i < tokens.length; i++) {
|
||||
String token = tokens[i];
|
||||
String[] t = token.split(":::");
|
||||
System.out.println(t[0]);
|
||||
System.out.println(t[1]);
|
||||
try {
|
||||
String json = ((JsonMapper) (getMapper(t[0]).newInstance())).getJson(t[0], t[1]);
|
||||
System.out.println("" + json);
|
||||
out += json;
|
||||
if (i < tokens.length - 1)
|
||||
out += ",";
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
out += "]}";
|
||||
|
||||
writer.append(out);
|
||||
|
||||
} catch (Exception x) {
|
||||
logger.error(x.getLocalizedMessage());
|
||||
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage()));
|
||||
}
|
||||
|
||||
// response.getWriter().write(json);
|
||||
}
|
||||
|
||||
private Class getMapper(String algId) throws Exception {
|
||||
return Class.forName("org.gcube.nlphub.mapper.DefaultMapper");
|
||||
}
|
||||
|
||||
private String getDocument(String plink) throws Exception {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(plink).openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
char[] readBuffer = new char[1024];
|
||||
int byteRead = 0;
|
||||
String content = "";
|
||||
while (byteRead > -1) {
|
||||
byteRead = reader.read(readBuffer, 0, 1024);
|
||||
if (byteRead > 0) {
|
||||
String bString = new String(readBuffer, 0, byteRead);
|
||||
content += bString;
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ public class DefaultMapper implements JsonMapper {
|
|||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(link).openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
System.out.println("Content-Type: " + connection.getContentType());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JsonObject jsonRoot = (JsonObject) new JsonParser().parse(reader);
|
||||
JsonElement entities = jsonRoot.get("entities");
|
||||
|
|
Loading…
Reference in New Issue