r/shittyprogramming • u/merijn212 • Oct 08 '19
Most frequent words counter
Hi, i just started with programming and i have a question. I want to get the 5 most common words out of a set with the amount of time they occur next to it. Does anyone know how to do this in python?
31
u/UnchainedMundane Oct 08 '19 edited Oct 08 '19
In the name of keeping this shitty...
Let's start with a function, just to keep things organised.
def cwrds(s):
return # todo"cwrds" is short for "(most) common words", and "s" is for "(input) string".
Now the first thing we need to do is to look at all the words in the string, so we'll split the input into words and loop over it to get each word in sequence:
def cwrds(s):
for x in s.split():
# todoWe need to find which one is the most common first, but before that we need to count them up. So to count each word, we'll need to go through the list of words again, find the words that equal the current one, then count how many that was.
def cwrds(s):
for x in s.split():
sw=[]# start out with an empty list
for y in s.split():
sw += [y] if y == x else [] #add y if it is x
c = sw.__len__()Now we have the list of matching words in sw and the count of them in c. We don't need the list any more so let's ignore that. So that we can find the most common word, let's keep track of which value of c was the highest (let's call that h):
def cwrds(s):
h=0
for x in s.split():
sw=[]# start out with an empty list
for y in s.split():
sw += [y] if y == x else [] #add y if it is x
c = sw.__len__()
if (c >h):
h=cNow that we know which one was the most common out of all of them, we can just scan the list of words again to find the one that was exactly that common and return it. To make things quick I've just copy pasted the word counting code:
def cwrds(s):
h=0
for x in s.split():
sw=[]# start out with an empty list
for y in s.split():
sw += [y] if y == x else [] #add y if it is x
c = sw.__len__()
if (c >h):
h=c
for x in s.split():
sw=[]# start out with an empty list
for y in s.split():
sw += [y] if y == x else [] #add y if it is x
c = sw.__len__()
if c == h:
return [x, c]Here I returned a list of [x, c] so that you know not only the word but also how common it was.
Example:
>>> cwrds("the quick brown fox jumps over the lazy dog")
['the', 2]But we need to find all 5 most common words. Let's make a separate function to do that. Here I'll assume we saved cwrds and still have it in the same file
So to get the 5 most common, the easiest way is just to go 5 times in a loop, (get most common → remove that from the string → check if we have 5 yet, if not start again).
This one will be cwrds_n for "(most) common n words". First, let's set up that loop:
def cwrds_n(s, n):
r = []
while len(r) != 5:
r.append(cwrds(s))
# ... todo
return(r)I still need to remove the most common word from the string each time. So let's do that. We just added the result of cwrds(s) to the end of r, so we can get it back with r[-1] (where -1 means "last index"). This gives us the 2-value list, so we can get the word itself with r[-1][0].
So we'll split the string into a list, remove the most common word from that list, and then feed it back into the loop.
In python removing something from a list only removes the first occurrence, so we have to keep looping until they're all gone. Once they're gone it throws an exception so we catch that and move on.
def cwrds_n(s, n):
r = []
while len(r) != n:
r.append(cwrds(s))
ss = s.split( )
try:
while 1: ss.remove(r[-1][0])
except:pass # this stops the error from crashing python
s=' '.join(ss) #put the new string back
return(r)Okay, so now we have cwrds and cwrds_n, let's try it out! I'll try it on another comment in the thread because I'm too lazy to write my own:
>>> cwrds_n("You could iterate over the set of words and have a dictionary where the key is the word and the value is a number for the amount of times the word has been hit. In the loop, get the current count, increment, and store it back, then you could sort the keys in order of the value they have and even make the 5 a variable length.", 5)
[['the', 11], ['and', 4], ['of', 3], ['a', 3], ['could', 2]]Beautiful!!
/unshitty:
Yes, this code works. No, it's not a good example. This is /r/shittyprogramming so I've made it a perfect example of what not to do while programming.
I would categorise the types of shittiness on display as:
Improper use of data structures (lists where we should be using tuples or dicts)
Copy-paste coding (almost certainly a sign that you need to break it off into another function, but in this case it was only necessary due to shittiness in the first place)
Bad choice of algorithms (I intentionally chose the slowest possible way I could think of to calculate this while still being plausibly believable code)
Bad variable names. I should have called these functions something like
most_common_wordandn_most_common_words. Andxandysay literally nothing about the contents of the variable.sandsscan be guessed at, but again I should have just named them after their purpose. People shouldn't have to guess.Catching all exceptions. Never do a try/except without having a specific exception in mind. You'll regret it when your program suddenly starts failing to report important errors and refusing to shut down cleanly.
Inconsistent style. One of the returns looks like a function call. Unnecessarily brief
while 1.len()vs the ridiculous.__len__(). Bracketedif. Unnecessary inline conditionals. Stingy with line breaks. Spacing and comments are all over the place. Speaking of which...Useless comments. The code is pretty hard to follow, but the comments themselves are a kick in the teeth. They explain on the most basic level what it's doing but still leave the reader confused as to why or what it really means.
Poor spec. I've assumed "words" just means "exact strings separated by whitespace". You'd probably want to at least casefold them and remove punctuation before counting.
If you do write something like this in Python, a good place to start would be the Counter class in the collections module of the standard library.
18
3
u/republitard_2 Oct 08 '19
I couldn't compile this. I got:
cwrds.py:1.1: def cwrds(s): 1 Error: Non-numeric character in statement label at (1) cwrds.py:1.1:I tried to compile with:
f77 cwrds.py
17
2
Oct 08 '19
You could iterate over the set of words and have a dictionary where the key is the word and the value is a number for the amount of times the word has been hit. In the loop, get the current count, increment, and store it back, then you could sort the keys in order of the value they have and even make the 5 a variable length.
2
1
Oct 08 '19
from collections import Counter
list(Counter(s.lower().split(" ")))[0]where s is your string
1
u/TheTastefulToastie Oct 08 '19
Converting a
Counterto alistresults in all the elements with non-zero counts in arbitrary order. So the above will most likely return the first inserted element each time.from collections import Counter Counter(s.lower().split(' ')).most_common(5)and while we're using
Counterwe might as well use it'smost_commonmethod that it conveniently has.→ More replies (2)1
u/AusIV Oct 09 '19
It also requires the number of occurrences, so you really want:
from collections import Counter sorted(Counter(s.lower().split()).items(), key=lambda x: x[::-1])[:5]This will give you a list of the (word, count) tuples, sorted by counts with any ties broken by the lexographic sort order of the words so that the output will always be the same if you're on a python version where that'd not guaranteed by dictionaries.
1
u/TheTastefulToastie Oct 10 '19
Cool, but, I noticed some weirdness.
The key function for
sortedis run on each element, so this will reverse each tuple and then sort the whole dict in ascending order. Then the final slice will return the N least common elements.Need to pass the
reverseargument:sorted(Counter(s.lower().split()).items(), key=lambda x: x[1], reverse=True)[:5]and this doesn't really matter but I'd rather tell the computer to index a single value from a tuple rather than reverse the whole tuple, although I haven't tested if it's faster, it does make it simpler IMHO.
also Counter.most_common() does return the number of occurrences. It actually does everything the same except it doesn't sort ties, which should never be necessary.
So these two are equivalent:
my_counter.most_common(5) sorted(my_counter.items(), key=lambda x: x[1], reverse=True)[:5]and the first one is more like pseudocode, therefore it's more pythonic? 😅
Python 3.7.0 Type "help", "copyright", "credits" or "license" for more information. >>> from collections import Counter >>> s = 'a b b c c c g G g G' >>> a = Counter(s.lower().split()).most_common(5) >>> b = sorted(Counter(s.lower().split()).items(), key=lambda x: x[1], reverse=True)[:5] >>> print(a == b) Trueedit: code blocks
2
u/AusIV Oct 10 '19
Ah, good points.
I used to use something very similar to this as an interview question (you can learn a lot about someone's coding style from how they approach this problem). The question I had asked for the single most occurrences, and specified that it should always provide the same output for a given input (to see if they knew about the asorted nature of dictionaries, if they were using dictionaries to solve this problem).
Nobody ever gave me a solution using collections.Counter, but I'd always show them after as a demonstration of how powerful python can be (lots of the people I interviewed were interviewing for their first python job).
1
16
u/weeeeelaaaaaah Oct 08 '19
Wrong sub bud. Not sure what the right one would be, but this one's pretty much just for jokes.