Search in sources :

Example 6 with AnnotatorService

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"));
}
Also used : AnnotatorService(edu.illinois.cs.cogcomp.annotation.AnnotatorService) Path(java.nio.file.Path) ArrayList(java.util.ArrayList) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)

Example 7 with AnnotatorService

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);
}
Also used : AnnotatorService(edu.illinois.cs.cogcomp.annotation.AnnotatorService) HelpScreenException(net.sourceforge.argparse4j.internal.HelpScreenException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Aggregations

AnnotatorService (edu.illinois.cs.cogcomp.annotation.AnnotatorService)7 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)3 Namespace (net.sourceforge.argparse4j.inf.Namespace)3 HelpScreenException (net.sourceforge.argparse4j.internal.HelpScreenException)3 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)2 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1