Add star wars 1 and exception

This commit is contained in:
Mathieu Sanchez 2019-03-19 18:53:01 +09:00
parent 1ac8de02cf
commit e72120bd14
2 changed files with 4270 additions and 15 deletions

View File

@ -1,38 +1,54 @@
#!/usr/bin/python
import sys
import matplotlib.pyplot as plt
def sort(dictionnary):
def sort(dic):
new_dic = {}
while len(dictionnary) > 0:
while len(dic) > 0:
tmp_val = 0
tmp_key = ""
for key, val in dictionnary.items():
for key, val in dic.items():
if val > tmp_val:
tmp_key = key
tmp_val = val
new_dic[tmp_key] = tmp_val
dictionnary.__delitem__(tmp_key)
dic.__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()
try:
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()
except FileNotFoundError:
sys.stderr.write("Error: " + script + " does not exist!")
sys.exit(1)
return words
def get_movie_name(file_path):
files_name = file_path.split("/")
file_name = files_name.pop()
movie_name = (file_name.split('.')[0]).replace('_', ' ')
return movie_name
def get_frequency(words):
freq = {}
for word in words:
@ -43,10 +59,19 @@ def get_frequency(words):
return sort(freq)
def display_fig(words, movie_name):
plt.title("Word frequencies in " + movie_name)
plt.ylabel("Number of Occurrences")
plt.xlabel("Words")
plt.show()
def main(script):
print("File loading and sorting ...")
words = get_words(script)
sorted_words = get_frequency(words)
print(sorted_words)
print("Display result")
display_fig(sorted_words, get_movie_name(script))
if __name__ == "__main__":

File diff suppressed because it is too large Load Diff