CAU-IR-Spring-2019-Project-1/program.py

59 lines
1.3 KiB
Python
Raw Normal View History

#!/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])