from string import *
def removePunctuation(sentence):
sentence = lower(sentence)
new_sentence = ""
for char in sentence:
if char not in punctuation:
new_sentence = new_sentence + char
return new_sentence
def wordFrequences(sentence):
wordCounts = {}
split_sentence = new_sentence.split()
print split_sentence
for entry in split_sentence:
for word in entry:
wordCounts[entry] = wordCounts.get (entry,0) + 1
wordCounts.items()
return wordCounts
sentence = "This is a test sentence, to test the function."
new_sentence = removePunctuation(sentence)
wordFrequences(sentence)
Hi I am trying to write a program which calculates how many times a certain word appears in a string.
Could someone help me how to do this, i.e. is there something similar instead of using .get which counts the characters.
At the moment i get the output of:
{'a': 1, 'function': 8, 'sentence': 8, 'this': 4, 'is': 2, 'to': 2, 'test': 8, 'the': 3}
I am trying to get the following:
{'this': 1, 'a': 1, 'is': 1, 'test': 2, ...}