我有python 2.7。我试图导入word_tokenize()并从nltk.tokenize()发送tokenise()。这显示错误。
from collections import defaultdict, deque
ImportError: cannot import name defaultdict
这似乎是名称冲突。我分别导入了集合并打印出来。它来自python库而不是来自nltk。我该如何解决这个冲突?
P.S。
完整代码
from nltk.tokenize import sent_tokenize, word_tokenize
s = "nltk is for natural language processing.?"
print sent_tokenize(s)
print word_tokenize(s)
错误:
Traceback (most recent call last):
File "tokenize.py", line 1, in <module>
from nltk.tokenize import sent_tokenize, word_tokenize
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/probability.py", line 47, in <module>
from collections import defaultdict, Counter
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/collections.py", line 13, in <module>
import pydoc
File "/home/atul/miniconda2/lib/python2.7/pydoc.py", line 56, in <module>
import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
File "/home/atul/miniconda2/lib/python2.7/inspect.py", line 39, in <module>
import tokenize
File "/media/atul/New Volume1/project/Sentiment Analysis/tokenize.py", line 1, in <module>
from nltk.tokenize import sent_tokenize, word_tokenize
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/tokenize/__init__.py", line 67, in <module>
from nltk.tokenize.mwe import MWETokenizer
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/tokenize/mwe.py", line 31, in <module>
from nltk.util import Trie
File "/home/atul/miniconda2/lib/python2.7/site-packages/nltk/util.py", line 21, in <module>
from collections import defaultdict, deque
ImportError: cannot import name defaultdict