Created
June 26, 2020 17:14
-
-
Save HauptJ/7c38a82b07f3ae5bc06206496a59032b to your computer and use it in GitHub Desktop.
Python Word Count using Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Find repeating words within text file | |
return: | |
word and count | |
Sentence is here. Sentence two is here. | |
""" | |
def main() | |
try: | |
textFile = "test.txt" | |
fs = open(test.txt) | |
fileText = fs | |
wordDict = {} | |
# strip periods | |
fileText.strip(".", "") | |
# load hash table words split by space delimiter | |
wordList = fileText.split("") | |
for word in wordList: | |
if wordDict[word]: | |
wordDict[word] +=1 | |
else: | |
wordDict[word] = 1 | |
for each k, v in wordDict: | |
print("word: " + k + " count: " + v) | |
catch IOExption e: | |
print("Failed to read file: "+ str(e)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment