如何解决删除单词并打印Python中最常见的单词
我正在使用Python
Counter
和nltk
。我正在尝试搜索外部文档中的单词,并用stopwrods
删除一些单词,然后显示最常用的单词。但是出现了一个我不知道如何解决的错误:
AttributeError: 'Counter' object has no attribute 'write'
有什么想法吗?
from collections import Counter
import io
from nltk.corpus import stopwords
import nltk
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
#word_tokenize accepts a string as an input,not a file.
stop_words = set(stopwords.words('spanish'))
file1 = open("labs.txt")
line = file1.read()# Use this to read file content as a stream:
words = line.split()
for r in words:
if not r in stop_words:
with open("labs.txt") as input_file:
vonCount = Counter(word for line in input_file for word in line.split())
vonCount.write(" "+r)
print(vonCount.most_common(70))
解决方法
我先谢谢了。
from collections import Counter
import io
from nltk.corpus import stopwords
import nltk
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
#word_tokenize accepts a string as an input,not a file.
stop_words = set(stopwords.words('spanish'))
file1 = open("labs.txt")
line = file1.read()# Use this to read file content as a stream:
words = line.split()
for r in words:
if not r in stop_words:
appendFile = open('cambio.txt','a')
appendFile.write(" "+r)
appendFile.close()
with open("cambio.txt") as input_file:
vonCount = Counter(word for line in input_file for word in line.split())
print(vonCount.most_common(7))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。