biswasrohit20

abc

Apr 16th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. def default_input(message, default_value):
  2. text = input(message)
  3. if text == "":
  4. return default_value
  5. else:
  6. return text
  7.  
  8.  
  9. in_file = default_input("Enter the input file name: [Press ENTER for waltham.txt]", "waltham.txt")
  10. line_len = default_input("Enter the line length: [Press ENTER for 50]", "50")
  11. search_word = default_input("Enter a search word: [Press ENTER for watch]", "watch")
  12. justification = default_input("Output justification (L/C/R): [Press ENTER for r]", "r")
  13. print("File Written")
  14.  
  15. out_file = open(in_file.split(".")[0] + "_output.txt", "w")
  16. with open(in_file) as file:
  17. total_sent = 0
  18. total_word = 0
  19. total_char = 0
  20. total_digit = 0
  21. total_year = 0
  22. total_searched_word = 0
  23. lines = []
  24. for line in file:
  25. total_sent += len(line.split(". "))
  26. total_word += len(line.split(" "))
  27. total_char += len(line)
  28. for l in line.split(". "):
  29. if len(l) <= int(line_len):
  30. lines.append(l)
  31. for ch in line:
  32. if ch.isdigit():
  33. total_digit += 1
  34. for w in line.split(" "):
  35. if search_word in w:
  36. total_searched_word += 1
  37. if w.isdigit():
  38. total_year += 1
  39. avg_sent_len = total_char/total_sent
  40.  
  41.  
  42. out_file.writelines("File Analysis" + "\n\n")
  43. out_file.writelines(f'Sentences: {total_sent} Words: {total_word}\n')
  44. out_file.writelines(f'Characters: {total_char} Digits: {total_digit}\n')
  45. out_file.writelines(f'Years: {total_year} Average Sentence Length: {avg_sent_len}\n')
  46. out_file.writelines("Original Text:\n")
  47. ln = 1
  48. new_list = []
  49. for l in lines:
  50. out_file.writelines(f'{ln} {l}.\n')
  51. temp = l.split(" ")
  52. t = ""
  53. for e in temp:
  54. if e[0] != "[" and e[-1] != "]":
  55. t = t + e + " "
  56. new_list.append(t)
  57. ln += 1
  58.  
  59.  
  60. out_file.writelines("\n\nNew Text:\n")
  61. lno = 1
  62. for l in new_list:
  63. out_file.writelines(f'{lno} {l}.\n')
  64. lno += 1
  65.  
  66.  
  67. out_file.close()
Add Comment
Please, Sign In to add comment