0

Counting occurence of words in a text

def wordcount(string): dictionary = {} words =string.split() if "and" in words: words.remove("and") words.remove("the") else: for word in words: word.lower() if word not in words: dictionary[word]=0 dictionary[word]+=1 return dictionary print(wordcount("The bat smashed into my face with such force that it crushed my nose into a distorted U-shape. The collision sent the soft tissue of my brain slamming into the inside of my skull. Immediately, a wave of swelling surged throughout my head. In a fraction of a second, I had a broken nose, multiple skull fractures, and two shattered eye sockets.")) Can someone pls help me find the mistake in this code???

14th Feb 2022, 6:25 AM
Amanillah Mansuri
Amanillah Mansuri - avatar
8 Answers
+ 5
i used the code from Rik Wittkopp and modified some parts: (see comments) story = "The bat smashed into my face with such force that it crushed my nose into a distorted U-shape. The collision sent the soft tissue of my brain slamming into the inside of my skull. Immediately, a wave of swelling surged throughout my head. In a fraction of a second, I had a broken nose, multiple skull fractures, and two shattered eye sockets." txt = story.lower().split() # <<< uses lower() to normalize text data a = {key:txt.count(key) for key in txt if key not in "and the"} # <<< uses if for filtering keys print(a)
14th Feb 2022, 11:22 AM
Lothar
Lothar - avatar
+ 5
Amanillah Mansuri : here is a code that follows your attempt. in some parts it gives the required readability for "beginners": sentence = "The bat smashed into my face with such force that it crushed my nose into a distorted U-shape. The collision sent the soft tissue of my brain slamming into the inside of my skull. Immediately, a wave of swelling surged throughout my head. In a fraction of a second, I had a broken nose, multiple skull fractures, and two shattered eye sockets." my_dict = {} def wordcount(string): for word in string.lower().split(): if word not in "and the": # <<< filter out "and" and "the" if word not in my_dict: # <<< if word not in dict: add it to dict my_dict[word] = 1 else: # if word in dict: increment counter my_dict[word] = my_dict[word] +1 return my_dict print(wordcount(sentence))
14th Feb 2022, 11:32 AM
Lothar
Lothar - avatar
+ 1
Amanillah Mansuri Exactly what are you trying to achieve. It looks like you are trying to count the words in the sentence, but are trying to exclude all "the" if any "and" is present. I don't understand why you are trying to use a dictionary as a variable to count the words. May I suggest you rephrase your question with your objective clearly stated, and post your code so we can look at an editable format
14th Feb 2022, 6:55 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Since words list has the word "and", if statement evaluates True and your code never move to else statement.
14th Feb 2022, 6:56 AM
Simba
Simba - avatar
+ 1
Amanillah Mansuri See if this helps you define your dictionary. You will still need to filter out unwanted words story = "The bat smashed into my face with such force that it crushed my nose into a distorted U-shape. The collision sent the soft tissue of my brain slamming into the inside of my skull. Immediately, a wave of swelling surged throughout my head. In a fraction of a second, I had a broken nose, multiple skull fractures, and two shattered eye sockets." txt = story.split() a = {key:txt.count(key) for key in txt} print(a)
14th Feb 2022, 8:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
If i put a continue statement after if will it work
14th Feb 2022, 6:58 AM
Amanillah Mansuri
Amanillah Mansuri - avatar
0
I wanted to make a word cloud for a project and was asked to use a dictionary for counting the occurence of each word in text given
14th Feb 2022, 7:00 AM
Amanillah Mansuri
Amanillah Mansuri - avatar
0
This code above is just for counting the occurence of each word after that i will use certain tools to convert them in to a wordcloud image
14th Feb 2022, 7:01 AM
Amanillah Mansuri
Amanillah Mansuri - avatar