use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class ChunkerDemo method main.
public static void main(String[] args) throws Exception {
/*Load data*/
byte[] encoded = Files.readAllBytes(Paths.get(args[0]));
String text = new String(encoded, StandardCharsets.UTF_8);
/*Create textannotation*/
AnnotatorService annotator = CuratorFactory.buildCuratorClient();
TextAnnotation ta = annotator.createBasicTextAnnotation("corpus", "id", text);
/*Add part-of-speech*/
annotator.addView(ta, ViewNames.POS);
/*ChunkerAnnotator*/
ChunkerAnnotator ca = new ChunkerAnnotator(true);
ca.initialize(new ChunkerConfigurator().getDefaultConfig());
ca.addView(ta);
/*Output to file*/
List<String> lines = new ArrayList<>();
lines.add(ta.getView(ViewNames.SHALLOW_PARSE).toString());
lines.add(ta.getView(ViewNames.POS).toString());
Path file = Paths.get(args[1]);
Files.write(file, lines, Charset.forName("UTF-8"));
}
use of edu.illinois.cs.cogcomp.annotation.AnnotatorService in project cogcomp-nlp by CogComp.
the class ExternalAnnotatorsServer 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);
}
Aggregations