Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #read in a txt x
  2. #change txt input to list x
  3. #take out all punctuations x
  4. #split into words x
  5. #def that can tell what paragraph each word is in
  6. #puts the words into alphabetical order and prints out what paragrah it was in
  7. #def that prints how often each word comes up into a top 10 list
  8. #top 20 list
  9.  
  10. def open_file(filename): #opens a txt
  11. file_object = open(filename, "r")
  12. return file_object
  13.  
  14. def list_object(file_object):#turns txt into a list
  15. lists = []
  16. for word in file_object:
  17. lists.append(word.replace(",", "").replace(".", ""))
  18. return lists
  19.  
  20. def words_object(lists): #split list into words
  21. words = []
  22. for word in lists:
  23. words.append(word.split())
  24. return words
  25.  
  26. def alphab_object(words): #puts the list into a alphabetical order
  27. words.sort()
  28. return words
  29.  
  30. def main():
  31. try:
  32. filename = input("Enter name of file: ")
  33. file_object = open_file(filename)
  34. lists = list_object(file_object)
  35. words = words_object(lists)
  36. words = alphab_object(words)
  37. print(words)
  38. except FileNotFoundError:
  39. print(" File " + filename + " not found!")
  40.  
  41. main ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement