导入 nltk 无法理解的错误

import nltk ununderstandable error

我刚刚在 CentOS 服务器上安装了 nltk。但是当我进入 python 并尝试导入 nltk 时,出现以下错误。

有什么想法吗?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/nltk/__init__.py", line 114, in <module>
    from nltk.collocations import *
  File "/usr/lib/python2.6/site-packages/nltk/collocations.py", line 38, in <module>
    from nltk.util import ngrams
  File "/usr/lib/python2.6/site-packages/nltk/util.py", line 1361
    d = {k: _default_to_regular(v) for k, v in d.items()}

从 3.1 版开始,NLTK 不再支持 Python < 2.7,从 http://www.nltk.org/news.html:

NLTK 3.1 released : October 2015

Add support for Python 3.5, drop support for Python 2.6, sentiment analysis package and several corpora, improved POS tagger, Twitter package, multi-word expression tokenizer, wrapper for Stanford Neural Dependency Parser, improved translation/alignment module including stack decoder, skipgram and everygram methods, Multext East Corpus and MTECorpusReader, minor bugfixes and enhancements For details see: https://github.com/nltk/nltk/blob/develop/ChangeLog

由于字典理解是Python>=2.7、https://www.python.org/dev/peps/pep-0274/的一个特征,使用NLTK>=3.1会导致字典理解发生错误。

强烈建议升级到 Python >= 2.7:https://github.com/h2oai/h2o-2/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-exactly-for-centos-machine-only

或者使用 conda 也可以简化问题:https://docs.continuum.io/anaconda/install#linux-install

但是如果Python 2.6确实有必要:

pip install -U nltk==3.0.5