Python Forum
how to calculate a maximum frequency of theterm in docuemnt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to calculate a maximum frequency of theterm in docuemnt
#1
Dear ALL,

how to retrieve  most occurred  term in the document 


words= [word for word in document ]

word_count = counter(words)

then what I need to do.....




thank you in advance........
Reply
#2
https://docs.python.org/3/library/collec...ost_common
Reply
#3
(Feb-27-2017, 08:47 AM)desul Wrote: Dear ALL,

how to retrieve  most occurred  term in the document 

from collections import Counter 
    
    terms= list(Counter(word for word in d).most_common()[0])
    
    for k, v in terms:
        return v

too many values to unpack



thank you in advance........
Reply
#4
You have selected most common word with [0] in your statement, so terms is list with just two items - word and its count. When you iterate over it, you are getting word first and count second - you cant unpack it.

If you remove your [0], it will be possible to iterate over it ( k, v will be word, wordcount ). But if you want only count of most common word, just use terms[1] with your original terms.
Reply
#5
(Feb-27-2017, 11:01 AM)zivoni Wrote: But if you want only count of most common word, just use terms[1] with your original terms.

As you say, with his original code, he only has the first item of the most_common list. But that's in the form (word, count), so he wants terms[0] of his original terms to get the most common word.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
It's mention to get first most common,you don't need to iterate one more time.

>>> from collections import Counter

>>> s = 'hello car hello a new car hello hello'
>>> terms = Counter(s.split())
>>> terms.most_common(1)
[('hello', 4)]

>>> # Unpack
>>> word, count = terms.most_common()[0]
>>> # If 36 f-string is nice
>>> print(f'The most common word is <{word}> with a count of {count}')
The most common word is <hello> with a count of 4
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to calculate frequency of word in list desul 4 4,328 Feb-28-2017, 03:05 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020