Advertisement
slavi146

FinalProject

May 28th, 2025
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. def word_counter(text):
  2.     word_count = {}
  3.     textwords = text.lower().split()
  4.     for word in textwords:
  5.         word = "".join(char for char in word if char.isalpha())
  6.         if word.isalpha():
  7.             if word in word_count:
  8.                 word_count[word] += 1
  9.             else:
  10.                 word_count[word] = 1
  11.         else:
  12.             try:
  13.                 raise ValueError("Input must be only string!")
  14.             except ValueError:
  15.                 print("Everything is fine.")
  16.     return word_count
  17. text = input()
  18.  
  19. dict = word_counter(text)
  20. for i, j in dict.items():
  21.     print(f'{i}:{j}')
  22.  
  23. with open('rechnik s dumi i tehniq broi', 'w') as f:
  24.     for i, j in dict.items():
  25.         f.write(f'{i}:{j}\n')
  26.  
  27. setwithvalues = set(dict.values())
  28. print(setwithvalues)
  29.  
  30.  
  31. list_words = list(dict.keys())
  32. print(list_words)
  33.  
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement