【问题标题】:How to use stanford word tokenizer in NLTK?如何在 NLTK 中使用斯坦福词标记器?
【发布时间】:2017-12-04 00:10:31
【问题描述】:

我正在寻找在 nltk 中使用 stanford word tokenizer 的方法,我想使用因为当我比较 stanford 和 nltk word tokenizer 的结果时,它们都是不同的。我知道可能有办法使用 stanford tokenizer,就像我们可以在 NLTK 中使用 stanford POS Tagger 和 NER。

是否可以在不运行服务器的情况下使用斯坦福分词器?

谢谢

【问题讨论】:

标签: python nltk stanford-nlp tokenize


【解决方案1】:

注意:此解决方案仅适用于:

  • NLTK v3.2.5(v3.2.6 的界面会更简单)

  • Stanford CoreNLP(版本 >= 2016-10-31)

首先,您必须先正确安装 Java 8,如果 Stanford CoreNLP 在命令行上工作,NLTK v3.2.5 中的 Stanford CoreNLP API 如下。

注意:在使用 NLTK 中的新 CoreNLP API 之前,您必须在终端中启动 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

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer \
-preload tokenize,ssplit,pos,lemma,parse,depparse \
-status_port 9000 -port 9000 -timeout 15000

在 Python 中:

>>> from nltk.parse.corenlp import CoreNLPParser
>>> st = CoreNLPParser()
>>> tokenized_sent = list(st.tokenize('What is the airspeed of an unladen swallow ?'))
>>> tokenized_sent
['What', 'is', 'the', 'airspeed', 'of', 'an', 'unladen', 'swallow', '?']

【讨论】:

  • 有没有办法使用服务器进行词性标注?我认为普通的nltk.tag.StanfordPOSTagger 不会将服务器地址作为参数。
【解决方案2】:

在 NLTK 之外,您可以使用official Python interface that's recently release by Stanford NLP

安装

cd ~
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
pip3 install -U https://github.com/stanfordnlp/python-stanford-corenlp/archive/master.zip

设置环境

# On Mac
export CORENLP_HOME=/Users/<username>/stanford-corenlp-full-2016-10-31/

# On linux
export CORENLP_HOME=/home/<username>/stanford-corenlp-full-2016-10-31/

在 Python 中

>>> import corenlp
>>> with corenlp.client.CoreNLPClient(annotators="tokenize ssplit".split()) as client:
...     ann = client.annotate(text)
... 
[pool-1-thread-4] INFO CoreNLP - [/0:0:0:0:0:0:0:1:55475] API call w/annotators tokenize,ssplit
Chris wrote a simple sentence that he parsed with Stanford CoreNLP.
>>> sentence = ann.sentence[0]
>>> 
>>> [token.word for token in sentence.token]
['Chris', 'wrote', 'a', 'simple', 'sentence', 'that', 'he', 'parsed', 'with', 'Stanford', 'CoreNLP', '.']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多