As of right now, I have a function to replace the countChars function,
def countWords(lines):
wordDict = {}
for line in lines:
wordList = lines.split
If you need to count a number of words in a passage, then it is better to use regex.
Let's start with a simple example:
import re
my_string = "Wow! Is this true? Really!?!? This is crazy!"
words = re.findall(r'\w+', my_string) #This finds words in the document
Result:
>>> words
['Wow', 'Is', 'this', 'true', 'Really', 'This', 'is', 'crazy']
Note that "Is" and "is" are two different words. My guess is that you want the to count them the same, so we can just capitalize all the words, and then count them.
from collections import Counter
cap_words = [word.upper() for word in words] #capitalizes all the words
word_counts = Counter(cap_words) #counts the number each time a word appears
Result:
>>> word_counts
Counter({'THIS': 2, 'IS': 2, 'CRAZY': 1, 'WOW': 1, 'TRUE': 1, 'REALLY': 1})
Are you good up to here?
Now we need to do exactly the same thing we did above just this time we are reading a file.
import re
from collections import Counter
with open('your_file.txt') as f:
passage = f.read()
words = re.findall(r'\w+', passage)
cap_words = [word.upper() for word in words]
word_counts = Counter(cap_words)