Created
July 1, 2017 05:39
-
-
Save anonymous/f0179a6549b87667c3448fa75d12bad1 to your computer and use it in GitHub Desktop.
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
My last version what I got was | |
filename = 'abc.txt' | |
new_dict ={} | |
total_count = 0 | |
# opening the filename in read mode | |
with open(filename, "r") as fp: | |
# iterating through each line in file | |
for line in fp: | |
# splitting the words in each line | |
words = line.split() | |
# Iterating through each word to store in dictionary | |
for word in words: | |
word = word.lower() | |
# if we are seeing a word first time assign the value 1 | |
if not word in new_dict: | |
new_dict[word] = 1 | |
# if the word already exists in dictionary increment the frequency of word | |
else: | |
new_dict[word] +=1 | |
total_count +=1 | |
for word in new_dict: | |
probab = new_dict[word]/ total_count | |
print('probability of word '+ word +': ' + str(probab)) | |
output_file = 'x.txt' | |
with open(output_file, "w") as fs: | |
for word in new_dict: | |
fs.write('probability for word '+ word +'is: ' + str(probab) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment