使用斯坦福分词器

Using stanford tokenizer

我正在使用 Stanford CoreNlp 工具对文本进行标记,其中引入的每个标记的偏移量非常重要(我需要每个标记的偏移量以便稍后在 Brat 中使用它)。我程序的相关部分如下:

pipeline.annotate(annotation);

        List<CoreMap> sentences =annotation.get(CoreAnnotations.SentencesAnnotation.class);
        if (sentences != null && !sentences.isEmpty()) {
            for (CoreMap sentence : sentences) {
                // CoreMap sentence = sentences.get(0);
                for (CoreMap token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                    // out.println(token+"\t"+token.get(NamedEntityTagAnnotation.class));

                    words = token + "\t" + token.get(NamedEntityTagAnnotation.class);
                    String word_offset = token.toShorterString().toString();
                    wordsId.add(words);
                    wordsId1.add(words.substring(0, words.indexOf("-")).trim());
                    wordsId2.add(word_offset);
              System.Out.Println("Text_woffset.txt",word_offset+"\n" );
                }

输入= “D:太棒了!

CM:丹妮拉,你好吗? {BR}

{NS}

D:除了听到一点回声外,我还不错。

厘米:哦。 {LG} 该死的。

D:等一下。

厘米:好的。

我使用以下代码读取输入:

Text = new Scanner(new File(Input)).useDelimiter("\A").next();

使用此输入,我得到了错误的偏移量。例如,对于标记 "Daniella",偏移量应为 [28 36],但工具显示 [27, 35] 或在文本中间,标记有 10 到 30 个错误的偏移量。你能告诉我如何使用分词器来处理这样的对话文本吗?我将实际文本作为输入(以确保问题不是因为使用扫描仪)但问题仍然存在。

您需要的是附加到每个标记的 CharacterOffsetBegin 和 CharacterOffsetEnd 注释。 shorthand 是 CoreLabel.begin()CoreLabel.end()。对您的代码进行细微调整:标记可以是 CoreLabels(CoreMap 的子class)——CoreLabel class 有很多实用程序使使用它们更容易的方法。

作为一般规则,虽然在 class 层次结构中 CoreLabel 和 Annotation 都是 CoreMap 的子class,语义上 Annotation 几乎总是一个文档,CoreMap 几乎总是一个句子,而 CoreLabel 几乎总是一个标记。