Example usage for opennlp.tools.sentdetect SentenceDetector sentDetect

List of usage examples for opennlp.tools.sentdetect SentenceDetector sentDetect

Introduction

In this page you can find the example usage for opennlp.tools.sentdetect SentenceDetector sentDetect.

Prototype

String[] sentDetect(String s);

Source Link

Document

Sentence detect a string.

Usage

From source file:org.sglover.nlp.CoreNLPEntityTagger.java

@Override
protected Entities getEntitiesImpl(String content) {
    Entities namedEntities = Entities.empty();

    SentenceModel sentenceModel = sentenceModels.get("en");
    SentenceDetector sentenceDetector = new SentenceDetectorME(sentenceModel);
    String[] sentences = sentenceDetector.sentDetect(content);

    TokenizerModel tm = tokenizerModels.get("en");
    TokenizerME wordBreaker = new TokenizerME(tm);

    for (String sentence : sentences) {
        String[] tokens = wordBreaker.tokenize(sentence);

        List<TextAnnotation> allTextAnnotations = new LinkedList<TextAnnotation>();

        POSModel posModel = posModels.get("en");
        POSTaggerME posme = new POSTaggerME(posModel);
        String[] posTags = posme.tag(tokens);

        List<String> npTokens = new LinkedList<>();

        ChunkerModel chunkerModel = chunkerModels.get("en");
        ChunkerME chunkerME = new ChunkerME(chunkerModel);
        Span[] chunks = chunkerME.chunkAsSpans(tokens, posTags);
        String[] chunkStrings = Span.spansToStrings(chunks, tokens);
        for (int i = 0; i < chunks.length; i++) {
            String chunkString = chunkStrings[i];
            logger.info("Chunk = " + chunkString + ", type = " + chunks[i].getType());
            if (chunks[i].getType().equals("NP")) {
                npTokens.add(chunkString);
            }/*  w ww.  j a v  a 2  s.com*/
        }

        // findEntities(namedEntities, allTextAnnotations,
        // npTokens.toArray(new String[0]));
        findEntities(namedEntities, allTextAnnotations, tokens);
    }

    return namedEntities;
}

From source file:org.esipfed.eskg.nlp.OpenIE.java

public static void main(String[] args) throws IOException {

    SentenceDetector sentenceDetector = null;
    try {//from ww  w.  j av  a 2s  .  com
        // need to change this to the resource folder
        InputStream modelIn = OpenIE.class.getClassLoader().getResourceAsStream("en-sent.bin");
        final SentenceModel sentenceModel = new SentenceModel(modelIn);
        modelIn.close();
        sentenceDetector = new SentenceDetectorME(sentenceModel);
    } catch (IOException ioe) {
        LOG.error("Error either reading 'en-sent.bin' file or creating SentanceModel: ", ioe);
        throw new IOException(ioe);
    }
    edu.knowitall.openie.OpenIE openIE = new edu.knowitall.openie.OpenIE(
            new ClearParser(new ClearPostagger(new ClearTokenizer())), new ClearSrl(), false, false);

    // any text file that contains English sentences would work
    File file = FileUtils.toFile(OpenIE.class.getClassLoader().getResource("test.txt"));
    String text = readFile(file.getAbsolutePath(), StandardCharsets.UTF_8);

    if (sentenceDetector != null) {
        String[] sentences = sentenceDetector.sentDetect(text);
        for (int i = 0; i < sentences.length; i++) {

            Seq<Instance> extractions = openIE.extract(sentences[i]);

            List<Instance> listExtractions = JavaConversions.seqAsJavaList(extractions);

            for (Instance instance : listExtractions) {
                StringBuilder sb = new StringBuilder();

                sb.append(instance.confidence()).append('\t').append(instance.extr().context()).append('\t')
                        .append(instance.extr().arg1().text()).append('\t').append(instance.extr().rel().text())
                        .append('\t');

                List<Argument> listArg2s = JavaConversions.seqAsJavaList(instance.extr().arg2s());
                for (Argument argument : listArg2s) {
                    sb.append(argument.text()).append("; ");
                }

                LOG.info(sb.toString());
            }
        }
    }

}