use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class ExternalAnnotatorsServer method main.
public static void main(String[] args) throws IOException, AnnotatorException {
logger.info("Starting create the externals annotators pipeline . . . ");
AnnotatorService service = ExternalAnnotatorServiceFactory.buildPipeline();
logger.info("Setting the service . . . ");
MainServer.setAnnotatorService(service);
logger.info("Start the server . . . ");
MainServer.startServer(args, logger);
}
use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class MainServer method startServer.
public static void startServer(String[] args, Logger logger) {
Namespace parseResults;
try {
parseResults = argumentParser.parseArgs(args);
} catch (HelpScreenException ex) {
return;
} catch (ArgumentParserException ex) {
logger.error("Exception while parsing arguments", ex);
return;
}
port(parseResults.getInt("port"));
// create a hashmap to keep track of client ip addresses and their
int rate = parseResults.getInt("rate");
if (rate > 0) {
clients = new HashMap<String, Integer>();
}
AnnotatorService finalPipeline = pipeline;
get("/annotate", "application/json", (request, response) -> {
logger.info("GET request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
String text = request.queryParams("text");
String views = request.queryParams("views");
return annotateText(finalPipeline, text, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
post("/annotate", (request, response) -> {
logger.info("POST request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
Map<String, String> map = splitQuery(request.body());
System.out.println("POST body parameters parsed: " + map);
String text = map.get("text");
String views = map.get("views");
return annotateText(finalPipeline, text, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
// api to get name of the available views
String viewsString = "";
for (String view : pipeline.getAvailableViews()) {
viewsString += ", " + view;
}
String finalViewsString = viewsString;
enableCORS("*", "*", "*");
get("/viewNames", (req, res) -> finalViewsString);
post("/viewNames", (req, res) -> finalViewsString);
}
use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class MainServer method startServer.
public static void startServer(String[] args) {
Namespace parseResults;
try {
parseResults = argumentParser.parseArgs(args);
} catch (HelpScreenException ex) {
return;
} catch (ArgumentParserException ex) {
logger.error("Exception while parsing arguments", ex);
return;
}
port(parseResults.getInt("port"));
// create a hashmap to keep track of client ip addresses and their
int rate = parseResults.getInt("rate");
if (rate > 0) {
clients = new HashMap<String, Integer>();
}
AnnotatorService finalPipeline = pipeline;
get("/annotate", "application/json", (request, response) -> {
logger.info("GET request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
String text = request.queryParams("text");
String views = request.queryParams("views");
return annotateText(finalPipeline, text, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
get("/addviews", "application/json", (request, response) -> {
logger.info("GET request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
String jsonStrTA = request.queryParams("jsonstr");
String views = request.queryParams("views");
return addAdditionalViewToTA(finalPipeline, jsonStrTA, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
post("/annotate", (request, response) -> {
logger.info("POST request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
Map<String, String> map = splitQuery(request.body());
System.out.println("POST body parameters parsed: " + map);
String text = map.get("text");
String views = map.get("views");
return annotateText(finalPipeline, text, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
post("/addviews", (request, response) -> {
logger.info("POST request . . . ");
boolean canServe = true;
if (rate > 0) {
resetServer();
String ip = request.ip();
int callsSofar = (Integer) clients.getOrDefault(ip, 0);
if (callsSofar > rate)
canServe = false;
clients.put(ip, callsSofar + 1);
}
if (canServe) {
logger.info("request.body(): " + request.body());
Map<String, String> map = splitQuery(request.body());
System.out.println("POST body parameters parsed: " + map);
String jsonStrTA = map.get("jsonstr");
String views = map.get("views");
return addAdditionalViewToTA(finalPipeline, jsonStrTA, views, logger);
} else {
response.status(429);
return "You have reached your maximum daily query limit :-/ ";
}
});
// api to get name of the available views
String viewsString = "";
for (String view : pipeline.getAvailableViews()) {
viewsString += ", " + view;
}
String finalViewsString = viewsString;
enableCORS("*", "*", "*");
get("/viewNames", (req, res) -> finalViewsString);
post("/viewNames", (req, res) -> finalViewsString);
get("/version", "application/json", (request, response) -> {
logger.info("GET request to retrieve version numbers . . . ");
final Properties properties = new Properties();
properties.load(pipeline.getClass().getClassLoader().getResourceAsStream("project.properties"));
System.out.println(properties.getProperty("version"));
System.out.println(properties.getProperty("artifactId"));
return properties.getProperty("version");
});
}
use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class ViewConstructorPipelineTest method main.
public static void main(String[] args) {
String input = null;
try {
input = LineIO.slurp(textFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
}
System.out.println("input from " + textFile + " is " + input.length() + " characters long.");
AnnotatorService as = null;
try {
as = PipelineFactory.buildPipeline(ViewNames.POS);
} catch (IOException | AnnotatorException e) {
e.printStackTrace();
System.exit(-1);
}
TextAnnotation ta = null;
try {
ta = as.createAnnotatedTextAnnotation("test", "test", input);
} catch (AnnotatorException e) {
e.printStackTrace();
System.exit(-1);
}
System.out.println("found " + ta.getView(ViewNames.POS).getConstituents() + " POS constituents.");
}
use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class ViewConstructorPipelineTest method testPosPipeline.
/**
* NOTE: this test cannot be run as part of test suite as it tries to
* open a default cache already used elsewhere by other tests, which will
* not be closed properly. This test is useful to verify that the particular
* factory method works, but must be run separately.
*/
// @Test
public void testPosPipeline() {
String input = null;
try {
input = LineIO.slurp(textFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
fail(e.getMessage());
}
assertFalse(input.length() == 0);
AnnotatorService as = null;
try {
as = PipelineFactory.buildPipeline(ViewNames.POS);
} catch (IOException | AnnotatorException e) {
e.printStackTrace();
fail(e.getMessage());
}
try {
as.createAnnotatedTextAnnotation("test", "test", input);
} catch (AnnotatorException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations