Counting sentence in text file using java

吃可爱长大的小学妹 提交于 2020-01-25 10:29:26

问题


The source code below is about to detect the sentences in the textfile using openNLP. However I don't know how to count and print the number of sentences in text file?

    package com.mycompany.app;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;

    import opennlp.tools.sentdetect.SentenceDetectorME;
    import opennlp.tools.sentdetect.SentenceModel;
    import opennlp.tools.util.InvalidFormatException;

    public class SentenceDetector {

    public static void main(String[] args) throws InvalidFormatException,IOException {
    try
    {
    File file = new File("D:/NetBeansProjects/my-app/textfile.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
    String word2 = br.readLine();

InputStream is = new FileInputStream("D:/NetBeansProjects/my-app/src/main/resources
    /en-sent.zip");
SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(word2);

for (String str 
    :sentences){                                                                
    System.out.println(str);
    }

br.close();  
    is.close(); 
    } 
    catch (IOException e)
    {
    // File not found
    e.printStackTrace();
    }
    }
    }

回答1:


I'm kind of puzzled about what your problem is. The number of sentences is

sentences.length

and you print it out like this:

System.out.println("the document contains", sentences.length, "sentences.");

Am I missing something?



来源:https://stackoverflow.com/questions/16853112/counting-sentence-in-text-file-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!