我和nltk有错误,我不能再打开以前的项目了:
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
example_sent = "This is a sample sentence, showing off the stop words filtration."
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)
当我运行这个时我得到了这个错误
File "/Users/nicolas/Desktop/part 1 natural lenguage/stopwords.py", line 1, in <module>
from nltk.tokenize import sent_tokenize, word_tokenize
File "/Library/Python/2.7/site-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/Library/Python/2.7/site-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/Library/Python/2.7/site-packages/nltk/probability.py", line 47, in <module>
from collections import defaultdict, Counter
File "/Library/Python/2.7/site-packages/nltk/collections.py", line 13, in <module>
import pydoc
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.py", line 56, in <module>
import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 39, in <module>
import tokenize
File "/Users/nicolas/Desktop/part 1 natural lenguage/tokenize.py", line 1, in <module>
from nltk.corpus import stopwords
File "/Library/Python/2.7/site-packages/nltk/corpus/__init__.py", line 64, in <module>
from nltk.tokenize import RegexpTokenizer
File "/Library/Python/2.7/site-packages/nltk/tokenize/__init__.py", line 67, in <module>
from nltk.tokenize.mwe import MWETokenizer
File "/Library/Python/2.7/site-packages/nltk/tokenize/mwe.py", line 31, in <module>
from nltk.util import Trie
File "/Library/Python/2.7/site-packages/nltk/util.py", line 21, in <module>
from collections import defaultdict, deque
ImportError: cannot import name defaultdict
iMac-di-Monica:part 1 natural lenguage nicolas$
错误并没有在这里结束它也会影响我使用nltk的所有其他项目以及当我运行第二个代码时
from nltk.tokenize import sent_tokenize, word_tokenize
EXAMPLE_TEXT = "Hello Mr. Smith, how are you doing today? The weather is great, and Python is awesome. The sky is pinkish-blue. You shouldn't eat cardboard."
print(sent_tokenize(EXAMPLE_TEXT))
print(word_tokenize(EXAMPLE_TEXT))
for i in word_tokenize(EXAMPLE_TEXT) :
print (i)
我得到了相同的上一个错误,第二个代码在之前完美运行,