![](/img/trans.png)
[英]Use Python to print sentences belonging to most common words in a document
[英]Remove words and print most common in Python
我正在使用Python
Counter
和nltk
。 我正在嘗試搜索外部文檔上的單詞,用stopwrods
刪除一些單詞,然后顯示最常見的單詞。 但是出現一個錯誤,我不知道如何解決:
AttributeError: 'Counter' object has no attribute 'write'
知道如何打開 go 嗎?
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.