迟到的回应,但对于像我这样的未来访客和经过长时间的搜索。使用 OpenNlP 模型,这是我的最佳选择,它适用于此处的所有文本示例,包括@nbz 在评论中提到的关键示例,
My friend, Mr. Jones, has a new dog. This is a test. This is a T.L.A. test. Now with a Dr. in it."
用行空间分隔:
My friend, Mr. Jones, has a new dog.
This is a test.
This is a T.L.A. test.
Now with a Dr. in it.
您需要将.jar
库以及经过训练的模型导入到您的项目中en-sent.bin
。
这是一个教程,可以轻松地将您集成到快速高效的运行中:
https://www.tutorialkart.com/opennlp/sentence-detection-example-in-opennlp/
还有一个用于在 Eclipse 中进行设置:
https://www.tutorialkart.com/opennlp/how-to-setup-opennlp-java-project/
这是代码的样子:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
/**
* Sentence Detection Example in openNLP using Java
* @author tutorialkart
*/
public class SentenceDetectExample {
public static void main(String[] args) {
try {
new SentenceDetectExample().sentenceDetect();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method is used to detect sentences in a paragraph/string
* @throws InvalidFormatException
* @throws IOException
*/
public void sentenceDetect() throws InvalidFormatException, IOException {
String paragraph = "This is a statement. This is another statement. Now is an abstract word for time, that is always flying.";
// refer to model file "en-sent,bin", available at link http://opennlp.sourceforge.net/models-1.5/
InputStream is = new FileInputStream("en-sent.bin");
SentenceModel model = new SentenceModel(is);
// feed the model to SentenceDetectorME class
SentenceDetectorME sdetector = new SentenceDetectorME(model);
// detect sentences in the paragraph
String sentences[] = sdetector.sentDetect(paragraph);
// print the sentences detected, to console
for(int i=0;i<sentences.length;i++){
System.out.println(sentences[i]);
}
is.close();
}
}
由于您实现了库,它也可以离线工作,这是一个很大的优势,因为@Julien Silland 的正确答案说这不是一个直接的过程,让一个训练有素的模型为您做这件事是最好的选择。