如何将 PTTBokenizer 的结果拆分成句子?

How to split the result of PTBTokenizer into sentences?

我知道我可以使用 DocumentPreprocessor 将文本拆分成句子。但是如果想要将标记化文本转换回原始文本,它并不能提供足够的信息。所以我必须使用 PTBTokenizer,它有一个 invertible 选项。

然而,PTBTokenizer 只是 returns 文档中所有标记 (CoreLabels) 的迭代器。它不会将文档拆分成句子。

The documentation 说:

The output of PTBTokenizer can be post-processed to divide a text into sentences.

但这显然不是小事。

斯坦福 NLP 库中是否有一个 class 可以将 CoreLabel 序列作为输入,并输出句子?这就是我的意思:

List<List<CoreLabel>> split(List<CoreLabel> documentTokens);

我建议您使用 StanfordCoreNLP class。这是一些示例代码:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.*;

public class PipelineExample {

    public static void main (String[] args) throws IOException {
        // build pipeline                                                                                                                                         
        Properties props = new Properties();
        props.setProperty("annotators","tokenize, ssplit, pos");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        String text = " I am a sentence.  I am another sentence.";
        Annotation annotation = new Annotation(text);
        pipeline.annotate(annotation);
        System.out.println(annotation.get(TextAnnotation.class));
        List<CoreMap> sentences = annotation.get(SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            System.out.println(sentence.get(TokensAnnotation.class));
            for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
                System.out.println(token.after() != null);
                System.out.println(token.before() != null);
                System.out.println(token.beginPosition());
                System.out.println(token.endPosition());
            }
        }
    }

}