Python OpenNLP包装器–标记器停止在\n

2024-09-27 09:34:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我(在OSX上工作)有一个关于python的OpenNLP包装器的问题:https://github.com/rohithb/openNLP-python-wrapper

由于某些原因,使用这个包装器的句子检测器不起作用。我没问题,只是换成了NLTK提供的句子检测器。当我将输出反馈回OpenNLP标记器时,问题就开始了。下面是一些示例代码:

import opennlp
import nltk

token = opennlp.OpenNLP("/Users/sven/apache-opennlp-1.6.0", "TokenizerME", "en-token.bin")
pos = opennlp.OpenNLP("/Users/sven/apache-opennlp-1.6.0", "POSTagger", "en-pos-maxent.bin")

def pipeline(start_with, str):
if start_with == "token":
    return pos.parse(token.parse(str).decode('utf-8')).decode('utf-8')
elif start_with == "pos":
        return pos.parse(str).decode('utf-8')
else:
    str = '\n'.join(nltk.sent_tokenize(str))
    return pos.parse(token.parse(str).decode('utf-8')).decode('utf-8')

如您所见,在最后一个“else”语句下,我使用\n作为分隔符来合并每个句子。我这样做是为了模仿OpenNLP语句拆分器的输出格式,如下所述:http://opennlp.apache.org/documentation/1.6.0/manual/opennlp.html#tools.sentdetect.detection

问题是,OpenNLP标记器在第一句话之后停止工作,只给出这一句的结果。示例:

^{pr2}$

输出:

'This_DT is_VBZ a_DT sentecene_NN ._.'

你知道为什么会发生这种情况吗?有什么可能的解决办法吗?谢谢!在


Tags: 标记postokenreturnparseapachewithstart
1条回答
网友
1楼 · 发布于 2024-09-27 09:34:48

Any Idea why this happens

来自OpenNLP docs

The parser expect a whitespace tokenized sentence.

句子检测器命令行工具的输出是每行一个句子。语句检测器API的输出是一个字符串数组,每个字符串一个句子,这更加合理。

要解析每个句子,不要串联,只需循环。

相关问题 更多 >

    热门问题