【问题标题】:Problems while calling NLTK with Python 3.5.2_2?使用 Python 3.5.2_2 调用 NLTK 时出现问题?
【发布时间】:2017-02-18 04:00:00
【问题描述】:

当我尝试在 python 3.5.2_2 中导入 nltk 时,我收到以下消息:

>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/nltk/__init__.py", line 117, in <module>
    from nltk.align import *
  File "/usr/local/lib/python3.5/site-packages/nltk/align/__init__.py", line 15, in <module>
    from nltk.align.ibm1 import IBMModel1
  File "/usr/local/lib/python3.5/site-packages/nltk/align/ibm1.py", line 18, in <module>
    from nltk.corpus import comtrans
  File "/usr/local/lib/python3.5/site-packages/nltk/corpus/__init__.py", line 64, in <module>
    from nltk.tokenize import RegexpTokenizer
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/__init__.py", line 65, in <module>
    from nltk.tokenize.regexp   import (RegexpTokenizer, WhitespaceTokenizer,
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 201, in <module>
    blankline_tokenize = BlanklineTokenizer().tokenize
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 172, in __init__
    RegexpTokenizer.__init__(self, r'\s*\n\s*\n\s*', gaps=True)
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 119, in __init__
    self._regexp = compile_regexp_to_noncapturing(pattern, flags)
  File "/usr/local/lib/python3.5/site-packages/nltk/internals.py", line 54, in compile_regexp_to_noncapturing
    return sre_compile.compile(convert_regexp_to_noncapturing_parsed(sre_parse.parse(pattern)), flags=flags)
  File "/usr/local/lib/python3.5/site-packages/nltk/internals.py", line 50, in convert_regexp_to_noncapturing_parsed
    parsed_pattern.pattern.groups = 1
AttributeError: can't set attribute

我尝试使用 pip3 重新安装它,但我仍然遇到同样的问题...知道如何在 OSX 中正确安装 NLTK 吗?以及如何解决这个问题?

【问题讨论】:

标签: python python-3.x nltk python-3.5


【解决方案1】:

这可能有效

  1. 转到http://www.lfd.uci.edu/~gohlke/pythonlibs/
  2. 下载nltk-3.2.1-py2.py3-none-any.whl
  3. 将命令行中的文件夹更改为您下载该 whl 文件的位置
  4. 现在写C:/windows/python35/scripts/pip install nltk-3.2.1-py2.py3-none-any.whl

如果它们不匹配,当然要更改这些路径

【讨论】:

  • 您能解释一下为什么您认为nltk 的非官方版本可能会解决 OP 的问题吗?
【解决方案2】:

我通过升级 pip3 nltk 和 sklearn 解决了这个问题:

user@MacBook-Pro-de-User:~$ pip3 install nltk --upgrade
Collecting nltk
  Downloading nltk-3.2.1.tar.gz (1.1MB)
    100% |████████████████████████████████| 1.1MB 675kB/s
Building wheels for collected packages: nltk
  Running setup.py bdist_wheel for nltk ... done
  Stored in directory: /Users/user/Library/Caches/pip/wheels/55/0b/ce/960dcdaec7c9af5b1f81d471a90c8dae88374386efe6e54a50
Successfully built nltk
Installing collected packages: nltk
  Found existing installation: nltk 3.0.0
    Uninstalling nltk-3.0.0:
      Successfully uninstalled nltk-3.0.0
Successfully installed nltk-3.2.1
user@MacBook-Pro-de-User:~$ python3
Python 3.5.2 (default, Sep 28 2016, 18:08:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
/usr/local/lib/python3.5/site-packages/sklearn/utils/fixes.py:55: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
  if 'order' in inspect.getargspec(np.copy)[0]:
>>>
user@MacBook-Pro-de-User:~$ clear
user@MacBook-Pro-de-User:~$ pip3 install sklearn --upgrade
Requirement already up-to-date: sklearn in /usr/local/lib/python3.5/site-packages
Collecting scikit-learn (from sklearn)
  Downloading scikit_learn-0.18-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 202kB/s
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.15.2
    Uninstalling scikit-learn-0.15.2:
      Successfully uninstalled scikit-learn-0.15.2
Successfully installed scikit-learn-0.18
user@MacBook-Pro-de-User:~$ clear
user@MacBook-Pro-de-User:~$ python3
Python 3.5.2 (default, Sep 28 2016, 18:08:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多