Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python word count and rank

Working on a word occurrence count application in a Python 3.2 / Windows environment.

Can anyone please help to tell me why the following isn't working?

from string import punctuation
from operator import itemgetter

N = 100
words = {}

words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
                                         for word in line.split())

for word in words_gen:
    words[word] = words.get(word, 0) + 1

top_words = (words.iteritems(), key=itemgetter(1), reverse=True)[:N]

for word, frequency in top_words:
    print ("%s %d") % (word, frequency)

The trace back error is:

Message File Name   Line    Position    
Traceback               
    <module>    C:\Users\will\Desktop\word_count.py 13      
AttributeError: 'dict' object has no attribute 'iteritems'              

Thanks

n.b.

Fully working code:

from string import punctuation
from operator import itemgetter

N = 100
words = {}

words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
                                         for word in line.split())

for word in words_gen:
    words[word] = words.get(word, 0) + 1

top_words = sorted(words.items(), key=itemgetter(1), reverse=True)[:N]

for word, frequency in top_words:
    print ("%s %d" % (word, frequency))

Thanks again guys

like image 767
Fruitful Avatar asked Oct 25 '11 11:10

Fruitful


People also ask

How to count the number of words in a Python string?

Another simple way to count the number of words in a Python string is to use the regular expressions library, re. The library comes with a function, findall (), which lets you search for different patterns of strings.

How do you count the frequency of a string in Python?

Count frequency of words in string in Python Using Count () Count () can be used to count the number of times a word occurs in a string or in other words it is used to tell the frequency of a word in a string. We just need to pass the word in the argument .

How many words does'Python'have in its total words?

' 'Python is an interpreted, high-level, general-purpose programming language' has total words: 8'' Hi. My name is Ashwini ' has total words: 5

How to get words out of a list in Python?

split function is quite useful and usually quite generic method to get words out of the list, but this approach fails once we introduce special characters in the list. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.


1 Answers

In Python 3, use just items where you'd previously use iteritems.

The new items() returns a dictionary view object that supports iteration as well as len and in.

And of course, in top_words = (words.iteritems(), ... you forgot to call the sorted function.


Edit: Please see my other answer for a better solution.

like image 79
Petr Viktorin Avatar answered Sep 19 '22 18:09

Petr Viktorin