简体   繁体   English

使用Java计算文本文件中的句子

[英]Counting sentence in text file using java

The source code below is about to detect the sentences in the textfile using openNLP. 下面的源代码将使用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();
    }
    }
    }

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? 我想念什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM