python - 使用 NLTK 为中文运行 StanfordPOSTagger 时出现意外格式

标签 python python-3.x nlp nltk stanford-nlp

我已经安装了 Python 3.6.0、NLTK 3.2.4,并下载了 Stanford POS Tagger 3.8.0。

然后我尝试运行以下脚本:

#!/usr/bin/env python3

from nltk.tag import StanfordPOSTagger


st = StanfordPOSTagger('chinese-distsim.tagger')
print(st.tag('这 是 斯坦福 中文 分词器 测试'.split()))

并且输出格式不正确:

[('', '这#PN'), ('', '是#VC'), ('', '斯坦福#NR'), ('', '中文#NN'), ('', '分词器#NN'), ('', '测试#NN')]

标注器确实完成了它的工作,但是单词和它们的词性不是成对分开的,而是用“#”连接形成单个字符串。这是专门针对中文的格式,还是哪里有问题?

最佳答案

长话短说

设置不同的_SEPARATOR:

from nltk.tag import StanfordPOSTagger

st = StanfordPOSTagger('chinese-distsim.tagger')
st._SEPARATOR = '#'
print(st.tag('这 是 斯坦福 中文 分词器 测试'.split()))

更好的解决方案

稍等片刻,等待 NLTK v3.2.5,届时将有一个非常简单的 Stanford 分词器接口(interface),这些分词器是跨不同语言标准化的。

不会涉及分隔符,因为标签和 token 是通过来自 REST 接口(interface)的 json 传输的 =)

此外,StanfordSegmenterStanfordTokenizer 类将在 v3.2.5 中弃用,请参阅

首先升级你的nltk版本:

pip install -U nltk

下载并启动 Stanford CoreNLP 服务器:

wget http://nlp.stanford.edu/software/stanford-corenlp-full-2016-10-31.zip
unzip stanford-corenlp-full-2016-10-31.zip && cd stanford-corenlp-full-2016-10-31
wget http://nlp.stanford.edu/software/stanford-chinese-corenlp-2016-10-31-models.jar
wget https://raw.githubusercontent.com/stanfordnlp/CoreNLP/master/src/edu/stanford/nlp/pipeline/StanfordCoreNLP-chinese.properties 

java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer \
-serverProperties StanfordCoreNLP-chinese.properties \
-preload tokenize,ssplit,pos,lemma,ner,parse \
-status_port 9001  -port 9001 -timeout 15000

然后在 NLTK v3.2.5 中:

>>> from nltk.tag.stanford import CoreNLPPOSTagger, CoreNLPNERTagger
>>> from nltk.tokenize.stanford import CoreNLPTokenizer
>>> stpos, stner = CoreNLPPOSTagger('http://localhost:9001'), CoreNLPNERTagger('http://localhost:9001')
>>> sttok = CoreNLPTokenizer('http://localhost:9001')

>>> sttok.tokenize(u'我家没有电脑。')
['我家', '没有', '电脑', '。']

# Without segmentation (input to`raw_string_parse()` is a list of single char strings)
>>> stpos.tag(u'我家没有电脑。')
[('我', 'PN'), ('家', 'NN'), ('没', 'AD'), ('有', 'VV'), ('电', 'NN'), ('脑', 'NN'), ('。', 'PU')]
# With segmentation
>>> stpos.tag(sttok.tokenize(u'我家没有电脑。'))
[('我家', 'NN'), ('没有', 'VE'), ('电脑', 'NN'), ('。', 'PU')]

# Without segmentation (input to`raw_string_parse()` is a list of single char strings)
>>> stner.tag(u'奥巴马与迈克尔·杰克逊一起去杂货店购物。')
[('奥', 'GPE'), ('巴', 'GPE'), ('马', 'GPE'), ('与', 'O'), ('迈', 'O'), ('克', 'PERSON'), ('尔', 'PERSON'), ('·', 'O'), ('杰', 'O'), ('克', 'O'), ('逊', 'O'), ('一', 'NUMBER'), ('起', 'O'), ('去', 'O'), ('杂', 'O'), ('货', 'O'), ('店', 'O'), ('购', 'O'), ('物', 'O'), ('。', 'O')]
# With segmentation
>>> stner.tag(sttok.tokenize(u'奥巴马与迈克尔·杰克逊一起去杂货店购物。'))
[('奥巴马', 'PERSON'), ('与', 'O'), ('迈克尔·杰克逊', 'PERSON'), ('一起', 'O'), ('去', 'O'), ('杂货店', 'O'), ('购物', 'O'), ('。', 'O')]

关于python - 使用 NLTK 为中文运行 StanfordPOSTagger 时出现意外格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45550167/

相关文章:

python - 无法在 screen 上运行 .py 脚本

python - 关闭 gspread 上的连接

python - Sqlalchemy 超出最大递归深度

python - 在 python 3 中,什么时候 type() 可以返回类以外的任何东西?

python - 缩进错误: unindent does not match any outer indentation level python

java - 什么是用于词性标记的好的 Java 库?

python - 让 turtle 保持在一个正方形内,没有输出,也没有错误

python - 初始化 SQLite3 数据库的多行

python - 查找句子中代词和名词之间的关系

php - 优化 PHP 中的递归方法