Read, get words, get frequency and sorted by frequency

This commit is contained in:
Mathieu Sanchez 2019-03-18 16:34:33 +09:00
parent a57007caf6
commit 1ac8de02cf

58
program.py Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/python
import sys
def sort(dictionnary):
new_dic = {}
while len(dictionnary) > 0:
tmp_val = 0
tmp_key = ""
for key, val in dictionnary.items():
if val > tmp_val:
tmp_key = key
tmp_val = val
new_dic[tmp_key] = tmp_val
dictionnary.__delitem__(tmp_key)
return new_dic
def get_words(script):
words = []
file = open(script, "r")
file_content = file.read()
file_content_split = file_content.split(" ")
for word_split_1 in file_content_split:
if word_split_1 != "":
remove_back_to_line = word_split_1.split('\n')
for word in remove_back_to_line:
if word != "":
words.append(word)
file.close()
return words
def get_frequency(words):
freq = {}
for word in words:
if freq.get(word) is not None:
freq[word] = freq[word] + 1
else:
freq[word] = 1
return sort(freq)
def main(script):
words = get_words(script)
sorted_words = get_frequency(words)
print(sorted_words)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please enter one movie parameter")
elif len(sys.argv) > 2:
print("Please enter only one argument")
else:
main(sys.argv[1])