简体   繁体   中英

Remove words and print most common in Python

I'm working with Python Counter and nltk . I'm trying to search words on an external document erase some words with stopwrods , and then show the most common ones. But an error appear that I don't know how to solve it:

AttributeError: 'Counter' object has no attribute 'write'

Any idea how to go on?

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))

I got it.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM