Advertisement
UndetectedButterFly

Counting the number of word repetitions

Apr 26th, 2023
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. with open(r'File directory', 'r') as file:
  2.     f_input = file.readlines()
  3.  
  4.  
  5. def word_count(user_input):
  6.     output = {}
  7.     for word in user_input:
  8.         if word not in output:
  9.             output[word] = 1
  10.         else:
  11.             output[word] += 1
  12.     return output
  13.  
  14.  
  15. output = {}
  16. for i in f_input:
  17.     for key, value in word_count(user_input=[str(i).lower() for i in i.split()]).items():
  18.         if key in output:
  19.             output[key] += value
  20.         else:
  21.             output[key] = value
  22. for key, value in output.items():
  23.     if value > 2: #Write repeat number that u want
  24.         print(key, value)
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement