2013-05-31 38 views
0

下面的源代码将使用openNLP检测文本文件中的句子。但是我不知道如何计算和打印文本文件中的句子数量?使用java计算文本文件中的句子

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(); 
    } 
    } 
    } 
+2

我会担心你正在阅读的文件,但这只是我 – MadProgrammer

+0

谢谢你,我会努力工作 – xera

回答

1

我有点困惑你的问题是什么。句子的数量是

sentences.length 

,你把它打印出来是这样的:

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

我缺少的东西?