Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import requests
  2. import string
  3. from collections import Counter
  4.  
  5. URL = "http://s3.zylowski.net/public/input/7.txt"
  6. FILENAME = "file.txt"
  7. PUNCTUATION = ".,!?:;()[]\"\'"
  8.  
  9.  
  10. def menu():
  11. print("1. Download file",
  12. "2. Count letters",
  13. "3. Count words",
  14. "4. Count punctuation marks",
  15. "5. Count sentences",
  16. "6. Generate report",
  17. "7. Save statistics to file",
  18. "8. Close the application",
  19. sep="\n")
  20. return input("Select option: ")
  21.  
  22.  
  23. def download_file(url):
  24. r = requests.get(url)
  25. with open(FILENAME, 'w', encoding='utf8') as f:
  26. f.write(r.text)
  27.  
  28.  
  29. def count_letters(text):
  30. num_letters = 0
  31. for char in text:
  32. if char.isalpha():
  33. num_letters +=1
  34. return num_letters
  35.  
  36.  
  37. def count_words(text):
  38. num_words = sum([i.strip(string.punctuation).isalpha() for i in text.split()]) # list comprehension
  39. return num_words
  40.  
  41.  
  42. def count_punctuation(text):
  43. num_punctuation = 0
  44. for char in text:
  45. if char in PUNCTUATION:
  46. num_punctuation += 1
  47. return num_punctuation
  48.  
  49.  
  50. def get_report(text):
  51. for char in string.ascii_uppercase:
  52. counter = Counter(text.upper())
  53. print("%s: %i" % (char, counter[char]))
  54.  
  55.  
  56. if __name__ == "__main__":
  57. while True:
  58. try:
  59. choice = int(menu())
  60. if choice < 1 or choice > 8:
  61. print("ERROR: You typed incorrect number. Select option by typing 1-8.")
  62. continue
  63. except ValueError:
  64. print("ERROR: You didn't type number. Select option by typing 1-8.")
  65. continue
  66. if choice == 1:
  67. download_file(URL)
  68. print("File downloaded.")
  69. elif 2 <= choice <= 6:
  70. try:
  71. with open(FILENAME, 'r') as file:
  72. text_from_file = file.read()
  73. except FileNotFoundError:
  74. print("ERROR: File %s not found. Please download the file first." % FILENAME)
  75. continue
  76. if choice == 2:
  77. letters = count_letters(text_from_file)
  78. print("Number of letters in the file: %i" % letters)
  79. elif choice == 3:
  80. words = count_words(text_from_file)
  81. print("Number of words in the file: %i" % words)
  82. elif choice == 4:
  83. punctuations = count_punctuation(text_from_file)
  84. print("Number of punctuation marks in the file: %i" % punctuations)
  85. elif choice == 5:
  86. print("Selected option %s not yet implemented" % choice) # TO DO, REPLACE AFTER IMPLEMENTATION
  87. elif choice == 6:
  88. print("Number of occurrences of each letter in the text:")
  89. get_report(text_from_file)
  90. elif choice == 7:
  91. print("Selected option %s not yet implemented" % choice) # TO DO, REPLACE AFTER IMPLEMENTATION
  92. elif choice == 8:
  93. print("Selected option %s not yet implemented" % choice) # TO DO, REPLACE AFTER IMPLEMENTATION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement